Merge pull request #977 from atom/dialog-icon

Add "icon" option for dialog.showMessageBox
This commit is contained in:
Cheng Zhao 2015-01-05 15:50:33 -08:00
commit d0ed681643
8 changed files with 79 additions and 37 deletions

View file

@ -11,6 +11,7 @@
#include "atom/browser/ui/file_dialog.h"
#include "atom/browser/ui/message_box.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "native_mate/callback.h"
#include "native_mate/dictionary.h"
@ -40,21 +41,27 @@ namespace {
void ShowMessageBox(int type,
const std::vector<std::string>& buttons,
const std::string& title,
const std::string& message,
const std::string& detail,
const std::vector<std::string>& texts,
const gfx::ImageSkia& icon,
atom::NativeWindow* window,
mate::Arguments* args) {
// FIXME We are exceeding the parameters limit of base::Bind here, so we have
// to pass some parameters in an array. We should remove this once we have
// variadic template support in base::Bind.
const std::string& title = texts[0];
const std::string& message = texts[1];
const std::string& detail = texts[2];
v8::Handle<v8::Value> peek = args->PeekNext();
atom::MessageBoxCallback callback;
if (mate::Converter<atom::MessageBoxCallback>::FromV8(args->isolate(),
peek,
&callback)) {
atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title,
message, detail, callback);
message, detail, icon, callback);
} else {
int chosen = atom::ShowMessageBox(window, (atom::MessageBoxType)type,
buttons, title, message, detail);
buttons, title, message, detail, icon);
args->Return(chosen);
}
}

View file

@ -91,12 +91,12 @@ module.exports =
options.title ?= ''
options.message ?= ''
options.detail ?= ''
options.icon ?= null
binding.showMessageBox options.type,
options.buttons,
String(options.title),
String(options.message),
String(options.detail),
[options.title, options.message, options.detail],
options.icon,
window,
callback

View file

@ -11,6 +11,10 @@
#include "base/callback_forward.h"
#include "base/strings/string16.h"
namespace gfx {
class ImageSkia;
}
namespace atom {
class NativeWindow;
@ -28,7 +32,8 @@ int ShowMessageBox(NativeWindow* parent_window,
const std::vector<std::string>& buttons,
const std::string& title,
const std::string& message,
const std::string& detail);
const std::string& detail,
const gfx::ImageSkia& icon);
void ShowMessageBox(NativeWindow* parent_window,
MessageBoxType type,
@ -36,6 +41,7 @@ void ShowMessageBox(NativeWindow* parent_window,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback);
// Like ShowMessageBox with simplest settings, but safe to call at very early

View file

@ -96,7 +96,8 @@ int ShowMessageBox(NativeWindow* parent_window,
const std::vector<std::string>& buttons,
const std::string& title,
const std::string& message,
const std::string& detail) {
const std::string& detail,
const gfx::ImageSkia& icon) {
NSAlert* alert = CreateNSAlert(
parent_window, type, buttons, title, message, detail);
@ -127,6 +128,7 @@ void ShowMessageBox(NativeWindow* parent_window,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
NSAlert* alert = CreateNSAlert(
parent_window, type, buttons, title, message, detail);

View file

@ -60,7 +60,8 @@ class MessageDialog : public views::WidgetDelegate,
const std::vector<std::string>& buttons,
const std::string& title,
const std::string& message,
const std::string& detail);
const std::string& detail,
const gfx::ImageSkia& icon);
virtual ~MessageDialog();
void Show(base::RunLoop* run_loop = NULL);
@ -75,24 +76,28 @@ class MessageDialog : public views::WidgetDelegate,
private:
// Overridden from views::WidgetDelegate:
virtual base::string16 GetWindowTitle() const;
virtual views::Widget* GetWidget() OVERRIDE;
virtual const views::Widget* GetWidget() const OVERRIDE;
virtual views::View* GetContentsView() OVERRIDE;
virtual views::View* GetInitiallyFocusedView() OVERRIDE;
virtual ui::ModalType GetModalType() const OVERRIDE;
virtual views::NonClientFrameView* CreateNonClientFrameView(
views::Widget* widget) OVERRIDE;
virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE;
base::string16 GetWindowTitle() const override;
gfx::ImageSkia GetWindowAppIcon() override;
gfx::ImageSkia GetWindowIcon() override;
bool ShouldShowWindowIcon() const override;
views::Widget* GetWidget() override;
const views::Widget* GetWidget() const override;
views::View* GetContentsView() override;
views::View* GetInitiallyFocusedView() override;
ui::ModalType GetModalType() const override;
views::NonClientFrameView* CreateNonClientFrameView(
views::Widget* widget) override;
views::ClientView* CreateClientView(views::Widget* widget) override;
// Overridden from views::View:
virtual gfx::Size GetPreferredSize() const OVERRIDE;
virtual void Layout() OVERRIDE;
virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
gfx::Size GetPreferredSize() const override;
void Layout() override;
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
// Overridden from views::ButtonListener:
virtual void ButtonPressed(views::Button* sender,
const ui::Event& event) OVERRIDE;
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
gfx::ImageSkia icon_;
bool delete_on_close_;
int result_;
@ -118,7 +123,7 @@ class MessageDialogClientView : public views::ClientView {
}
// views::ClientView:
virtual bool CanClose() OVERRIDE {
bool CanClose() override {
dialog_->Close();
return false;
}
@ -137,8 +142,10 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
const std::vector<std::string>& buttons,
const std::string& title,
const std::string& message,
const std::string& detail)
: delete_on_close_(false),
const std::string& detail,
const gfx::ImageSkia& icon)
: icon_(icon),
delete_on_close_(false),
result_(-1),
title_(base::UTF8ToUTF16(title)),
parent_(parent_window),
@ -174,6 +181,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
views::Widget::InitParams params;
params.delegate = this;
params.type = views::Widget::InitParams::TYPE_WINDOW;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
if (parent_) {
params.parent = parent_->GetNativeWindow();
@ -184,6 +192,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
widget_.reset(new views::Widget);
widget_->Init(params);
widget_->UpdateWindowIcon();
// Bind to ESC.
AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
@ -230,6 +239,18 @@ base::string16 MessageDialog::GetWindowTitle() const {
return title_;
}
gfx::ImageSkia MessageDialog::GetWindowAppIcon() {
return icon_;
}
gfx::ImageSkia MessageDialog::GetWindowIcon() {
return icon_;
}
bool MessageDialog::ShouldShowWindowIcon() const {
return true;
}
views::Widget* MessageDialog::GetWidget() {
return widget_.get();
}
@ -338,8 +359,10 @@ int ShowMessageBox(NativeWindow* parent_window,
const std::vector<std::string>& buttons,
const std::string& title,
const std::string& message,
const std::string& detail) {
MessageDialog dialog(parent_window, type, buttons, title, message, detail);
const std::string& detail,
const gfx::ImageSkia& icon) {
MessageDialog dialog(
parent_window, type, buttons, title, message, detail, icon);
{
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoopForUI::current());
@ -357,10 +380,11 @@ void ShowMessageBox(NativeWindow* parent_window,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
// The dialog would be deleted when the dialog is closed.
MessageDialog* dialog = new MessageDialog(
parent_window, type, buttons, title, message, detail);
parent_window, type, buttons, title, message, detail, icon);
dialog->set_callback(callback);
dialog->Show();
}

View file

@ -101,6 +101,9 @@ bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
gfx::ImageSkia* out) {
if (val->IsNull())
return true;
base::FilePath path;
if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
return false;

View file

@ -28,20 +28,19 @@ bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
gfx::ImageSkia* out) {
gfx::Image image;
if (!ConvertFromV8(isolate, val, &image) || image.IsEmpty())
if (!ConvertFromV8(isolate, val, &image))
return false;
gfx::ImageSkia image_skia = image.AsImageSkia();
if (image_skia.isNull())
return false;
*out = image_skia;
*out = image.AsImageSkia();
return true;
}
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
gfx::Image* out) {
if (val->IsNull())
return true;
std::string path;
if (!ConvertFromV8(isolate, val, &path))
return false;

View file

@ -76,6 +76,7 @@ would be passed via `callback(filename)`
* `title` String - Title of the message box, some platforms will not show it
* `message` String - Content of the message box
* `detail` String - Extra information of the message
* `icon` [Image](image.md)
* `callback` Function
Shows a message box, it will block until the message box is closed. It returns