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/file_dialog.h"
#include "atom/browser/ui/message_box.h" #include "atom/browser/ui/message_box.h"
#include "atom/common/native_mate_converters/file_path_converter.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/callback.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -40,21 +41,27 @@ namespace {
void ShowMessageBox(int type, void ShowMessageBox(int type,
const std::vector<std::string>& buttons, const std::vector<std::string>& buttons,
const std::string& title, const std::vector<std::string>& texts,
const std::string& message, const gfx::ImageSkia& icon,
const std::string& detail,
atom::NativeWindow* window, atom::NativeWindow* window,
mate::Arguments* args) { 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(); v8::Handle<v8::Value> peek = args->PeekNext();
atom::MessageBoxCallback callback; atom::MessageBoxCallback callback;
if (mate::Converter<atom::MessageBoxCallback>::FromV8(args->isolate(), if (mate::Converter<atom::MessageBoxCallback>::FromV8(args->isolate(),
peek, peek,
&callback)) { &callback)) {
atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title, atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title,
message, detail, callback); message, detail, icon, callback);
} else { } else {
int chosen = atom::ShowMessageBox(window, (atom::MessageBoxType)type, int chosen = atom::ShowMessageBox(window, (atom::MessageBoxType)type,
buttons, title, message, detail); buttons, title, message, detail, icon);
args->Return(chosen); args->Return(chosen);
} }
} }

View file

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

View file

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

View file

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

View file

@ -28,20 +28,19 @@ bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val, v8::Handle<v8::Value> val,
gfx::ImageSkia* out) { gfx::ImageSkia* out) {
gfx::Image image; gfx::Image image;
if (!ConvertFromV8(isolate, val, &image) || image.IsEmpty()) if (!ConvertFromV8(isolate, val, &image))
return false; return false;
gfx::ImageSkia image_skia = image.AsImageSkia(); *out = image.AsImageSkia();
if (image_skia.isNull())
return false;
*out = image_skia;
return true; return true;
} }
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate, bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val, v8::Handle<v8::Value> val,
gfx::Image* out) { gfx::Image* out) {
if (val->IsNull())
return true;
std::string path; std::string path;
if (!ConvertFromV8(isolate, val, &path)) if (!ConvertFromV8(isolate, val, &path))
return false; 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 * `title` String - Title of the message box, some platforms will not show it
* `message` String - Content of the message box * `message` String - Content of the message box
* `detail` String - Extra information of the message * `detail` String - Extra information of the message
* `icon` [Image](image.md)
* `callback` Function * `callback` Function
Shows a message box, it will block until the message box is closed. It returns Shows a message box, it will block until the message box is closed. It returns