Skip to content

Commit d0ed681

Browse files
committed
Merge pull request electron#977 from atom/dialog-icon
Add "icon" option for dialog.showMessageBox
2 parents d65919d + 5656714 commit d0ed681

File tree

8 files changed

+79
-37
lines changed

8 files changed

+79
-37
lines changed

atom/browser/api/atom_api_dialog.cc

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "atom/browser/ui/file_dialog.h"
1212
#include "atom/browser/ui/message_box.h"
1313
#include "atom/common/native_mate_converters/file_path_converter.h"
14+
#include "atom/common/native_mate_converters/image_converter.h"
1415
#include "native_mate/callback.h"
1516
#include "native_mate/dictionary.h"
1617

@@ -40,21 +41,27 @@ namespace {
4041

4142
void ShowMessageBox(int type,
4243
const std::vector<std::string>& buttons,
43-
const std::string& title,
44-
const std::string& message,
45-
const std::string& detail,
44+
const std::vector<std::string>& texts,
45+
const gfx::ImageSkia& icon,
4646
atom::NativeWindow* window,
4747
mate::Arguments* args) {
48+
// FIXME We are exceeding the parameters limit of base::Bind here, so we have
49+
// to pass some parameters in an array. We should remove this once we have
50+
// variadic template support in base::Bind.
51+
const std::string& title = texts[0];
52+
const std::string& message = texts[1];
53+
const std::string& detail = texts[2];
54+
4855
v8::Handle<v8::Value> peek = args->PeekNext();
4956
atom::MessageBoxCallback callback;
5057
if (mate::Converter<atom::MessageBoxCallback>::FromV8(args->isolate(),
5158
peek,
5259
&callback)) {
5360
atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title,
54-
message, detail, callback);
61+
message, detail, icon, callback);
5562
} else {
5663
int chosen = atom::ShowMessageBox(window, (atom::MessageBoxType)type,
57-
buttons, title, message, detail);
64+
buttons, title, message, detail, icon);
5865
args->Return(chosen);
5966
}
6067
}

atom/browser/api/lib/dialog.coffee

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ module.exports =
9191
options.title ?= ''
9292
options.message ?= ''
9393
options.detail ?= ''
94+
options.icon ?= null
9495

9596
binding.showMessageBox options.type,
9697
options.buttons,
97-
String(options.title),
98-
String(options.message),
99-
String(options.detail),
98+
[options.title, options.message, options.detail],
99+
options.icon,
100100
window,
101101
callback
102102

atom/browser/ui/message_box.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "base/callback_forward.h"
1212
#include "base/strings/string16.h"
1313

14+
namespace gfx {
15+
class ImageSkia;
16+
}
17+
1418
namespace atom {
1519

1620
class NativeWindow;
@@ -28,14 +32,16 @@ int ShowMessageBox(NativeWindow* parent_window,
2832
const std::vector<std::string>& buttons,
2933
const std::string& title,
3034
const std::string& message,
31-
const std::string& detail);
35+
const std::string& detail,
36+
const gfx::ImageSkia& icon);
3237

3338
void ShowMessageBox(NativeWindow* parent_window,
3439
MessageBoxType type,
3540
const std::vector<std::string>& buttons,
3641
const std::string& title,
3742
const std::string& message,
3843
const std::string& detail,
44+
const gfx::ImageSkia& icon,
3945
const MessageBoxCallback& callback);
4046

4147
// Like ShowMessageBox with simplest settings, but safe to call at very early

atom/browser/ui/message_box_mac.mm

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ int ShowMessageBox(NativeWindow* parent_window,
9696
const std::vector<std::string>& buttons,
9797
const std::string& title,
9898
const std::string& message,
99-
const std::string& detail) {
99+
const std::string& detail,
100+
const gfx::ImageSkia& icon) {
100101
NSAlert* alert = CreateNSAlert(
101102
parent_window, type, buttons, title, message, detail);
102103

@@ -127,6 +128,7 @@ void ShowMessageBox(NativeWindow* parent_window,
127128
const std::string& title,
128129
const std::string& message,
129130
const std::string& detail,
131+
const gfx::ImageSkia& icon,
130132
const MessageBoxCallback& callback) {
131133
NSAlert* alert = CreateNSAlert(
132134
parent_window, type, buttons, title, message, detail);

atom/browser/ui/message_box_views.cc

+45-21
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class MessageDialog : public views::WidgetDelegate,
6060
const std::vector<std::string>& buttons,
6161
const std::string& title,
6262
const std::string& message,
63-
const std::string& detail);
63+
const std::string& detail,
64+
const gfx::ImageSkia& icon);
6465
virtual ~MessageDialog();
6566

6667
void Show(base::RunLoop* run_loop = NULL);
@@ -75,24 +76,28 @@ class MessageDialog : public views::WidgetDelegate,
7576

7677
private:
7778
// Overridden from views::WidgetDelegate:
78-
virtual base::string16 GetWindowTitle() const;
79-
virtual views::Widget* GetWidget() OVERRIDE;
80-
virtual const views::Widget* GetWidget() const OVERRIDE;
81-
virtual views::View* GetContentsView() OVERRIDE;
82-
virtual views::View* GetInitiallyFocusedView() OVERRIDE;
83-
virtual ui::ModalType GetModalType() const OVERRIDE;
84-
virtual views::NonClientFrameView* CreateNonClientFrameView(
85-
views::Widget* widget) OVERRIDE;
86-
virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE;
79+
base::string16 GetWindowTitle() const override;
80+
gfx::ImageSkia GetWindowAppIcon() override;
81+
gfx::ImageSkia GetWindowIcon() override;
82+
bool ShouldShowWindowIcon() const override;
83+
views::Widget* GetWidget() override;
84+
const views::Widget* GetWidget() const override;
85+
views::View* GetContentsView() override;
86+
views::View* GetInitiallyFocusedView() override;
87+
ui::ModalType GetModalType() const override;
88+
views::NonClientFrameView* CreateNonClientFrameView(
89+
views::Widget* widget) override;
90+
views::ClientView* CreateClientView(views::Widget* widget) override;
8791

8892
// Overridden from views::View:
89-
virtual gfx::Size GetPreferredSize() const OVERRIDE;
90-
virtual void Layout() OVERRIDE;
91-
virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
93+
gfx::Size GetPreferredSize() const override;
94+
void Layout() override;
95+
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
9296

9397
// Overridden from views::ButtonListener:
94-
virtual void ButtonPressed(views::Button* sender,
95-
const ui::Event& event) OVERRIDE;
98+
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
99+
100+
gfx::ImageSkia icon_;
96101

97102
bool delete_on_close_;
98103
int result_;
@@ -118,7 +123,7 @@ class MessageDialogClientView : public views::ClientView {
118123
}
119124

120125
// views::ClientView:
121-
virtual bool CanClose() OVERRIDE {
126+
bool CanClose() override {
122127
dialog_->Close();
123128
return false;
124129
}
@@ -137,8 +142,10 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
137142
const std::vector<std::string>& buttons,
138143
const std::string& title,
139144
const std::string& message,
140-
const std::string& detail)
141-
: delete_on_close_(false),
145+
const std::string& detail,
146+
const gfx::ImageSkia& icon)
147+
: icon_(icon),
148+
delete_on_close_(false),
142149
result_(-1),
143150
title_(base::UTF8ToUTF16(title)),
144151
parent_(parent_window),
@@ -174,6 +181,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
174181

175182
views::Widget::InitParams params;
176183
params.delegate = this;
184+
params.type = views::Widget::InitParams::TYPE_WINDOW;
177185
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
178186
if (parent_) {
179187
params.parent = parent_->GetNativeWindow();
@@ -184,6 +192,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
184192

185193
widget_.reset(new views::Widget);
186194
widget_->Init(params);
195+
widget_->UpdateWindowIcon();
187196

188197
// Bind to ESC.
189198
AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
@@ -230,6 +239,18 @@ base::string16 MessageDialog::GetWindowTitle() const {
230239
return title_;
231240
}
232241

242+
gfx::ImageSkia MessageDialog::GetWindowAppIcon() {
243+
return icon_;
244+
}
245+
246+
gfx::ImageSkia MessageDialog::GetWindowIcon() {
247+
return icon_;
248+
}
249+
250+
bool MessageDialog::ShouldShowWindowIcon() const {
251+
return true;
252+
}
253+
233254
views::Widget* MessageDialog::GetWidget() {
234255
return widget_.get();
235256
}
@@ -338,8 +359,10 @@ int ShowMessageBox(NativeWindow* parent_window,
338359
const std::vector<std::string>& buttons,
339360
const std::string& title,
340361
const std::string& message,
341-
const std::string& detail) {
342-
MessageDialog dialog(parent_window, type, buttons, title, message, detail);
362+
const std::string& detail,
363+
const gfx::ImageSkia& icon) {
364+
MessageDialog dialog(
365+
parent_window, type, buttons, title, message, detail, icon);
343366
{
344367
base::MessageLoop::ScopedNestableTaskAllower allow(
345368
base::MessageLoopForUI::current());
@@ -357,10 +380,11 @@ void ShowMessageBox(NativeWindow* parent_window,
357380
const std::string& title,
358381
const std::string& message,
359382
const std::string& detail,
383+
const gfx::ImageSkia& icon,
360384
const MessageBoxCallback& callback) {
361385
// The dialog would be deleted when the dialog is closed.
362386
MessageDialog* dialog = new MessageDialog(
363-
parent_window, type, buttons, title, message, detail);
387+
parent_window, type, buttons, title, message, detail, icon);
364388
dialog->set_callback(callback);
365389
dialog->Show();
366390
}

atom/common/native_mate_converters/image_converter.cc

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
101101
bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
102102
v8::Handle<v8::Value> val,
103103
gfx::ImageSkia* out) {
104+
if (val->IsNull())
105+
return true;
106+
104107
base::FilePath path;
105108
if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
106109
return false;

atom/common/native_mate_converters/image_converter_mac.mm

+5-6
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ bool IsTemplateImage(const std::string& path) {
2828
v8::Handle<v8::Value> val,
2929
gfx::ImageSkia* out) {
3030
gfx::Image image;
31-
if (!ConvertFromV8(isolate, val, &image) || image.IsEmpty())
31+
if (!ConvertFromV8(isolate, val, &image))
3232
return false;
3333

34-
gfx::ImageSkia image_skia = image.AsImageSkia();
35-
if (image_skia.isNull())
36-
return false;
37-
38-
*out = image_skia;
34+
*out = image.AsImageSkia();
3935
return true;
4036
}
4137

4238
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
4339
v8::Handle<v8::Value> val,
4440
gfx::Image* out) {
41+
if (val->IsNull())
42+
return true;
43+
4544
std::string path;
4645
if (!ConvertFromV8(isolate, val, &path))
4746
return false;

docs/api/dialog.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ would be passed via `callback(filename)`
7676
* `title` String - Title of the message box, some platforms will not show it
7777
* `message` String - Content of the message box
7878
* `detail` String - Extra information of the message
79+
* `icon` [Image](image.md)
7980
* `callback` Function
8081

8182
Shows a message box, it will block until the message box is closed. It returns

0 commit comments

Comments
 (0)