From b1584272715e2dc3a771579f415626855c1d9dd5 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jul 2015 18:26:50 +0800 Subject: [PATCH 1/4] Add cancelId option for showMessageBox --- atom/browser/api/atom_api_dialog.cc | 8 +++++--- atom/browser/api/lib/dialog.coffee | 2 ++ atom/browser/ui/message_box.h | 2 ++ atom/browser/ui/message_box_gtk.cc | 11 +++++++---- atom/browser/ui/message_box_mac.mm | 2 ++ atom/browser/ui/message_box_win.cc | 12 +++++++++--- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/atom/browser/api/atom_api_dialog.cc b/atom/browser/api/atom_api_dialog.cc index 02d820bdeb5..b5fd655d6f2 100644 --- a/atom/browser/api/atom_api_dialog.cc +++ b/atom/browser/api/atom_api_dialog.cc @@ -41,6 +41,7 @@ namespace { void ShowMessageBox(int type, const std::vector& buttons, + int cancel_id, const std::vector& texts, const gfx::ImageSkia& icon, atom::NativeWindow* window, @@ -57,11 +58,12 @@ void ShowMessageBox(int type, if (mate::Converter::FromV8(args->isolate(), peek, &callback)) { - atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title, - message, detail, icon, callback); + atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, cancel_id, + title, message, detail, icon, callback); } else { int chosen = atom::ShowMessageBox(window, (atom::MessageBoxType)type, - buttons, title, message, detail, icon); + buttons, cancel_id, title, message, + detail, icon); args->Return(chosen); } } diff --git a/atom/browser/api/lib/dialog.coffee b/atom/browser/api/lib/dialog.coffee index d95fa7426ac..9c1c1934759 100644 --- a/atom/browser/api/lib/dialog.coffee +++ b/atom/browser/api/lib/dialog.coffee @@ -92,9 +92,11 @@ module.exports = options.message ?= '' options.detail ?= '' options.icon ?= null + options.cancelId ?= 0 binding.showMessageBox messageBoxType, options.buttons, + options.cancelId, [options.title, options.message, options.detail], options.icon, window, diff --git a/atom/browser/ui/message_box.h b/atom/browser/ui/message_box.h index acd1e3926b3..817da2c5699 100644 --- a/atom/browser/ui/message_box.h +++ b/atom/browser/ui/message_box.h @@ -32,6 +32,7 @@ typedef base::Callback MessageBoxCallback; int ShowMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, @@ -40,6 +41,7 @@ int ShowMessageBox(NativeWindow* parent_window, void ShowMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, diff --git a/atom/browser/ui/message_box_gtk.cc b/atom/browser/ui/message_box_gtk.cc index 626f16704dd..f5a29f42c07 100644 --- a/atom/browser/ui/message_box_gtk.cc +++ b/atom/browser/ui/message_box_gtk.cc @@ -29,12 +29,13 @@ class GtkMessageBox { GtkMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, const gfx::ImageSkia& icon) : dialog_scope_(parent_window), - cancel_id_(0) { + cancel_id_(cancel_id) { // Create dialog. dialog_ = gtk_message_dialog_new( nullptr, // parent @@ -163,29 +164,31 @@ void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) { int ShowMessageBox(NativeWindow* parent, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, const gfx::ImageSkia& icon) { - return GtkMessageBox(parent, type, buttons, title, message, detail, + return GtkMessageBox(parent, type, buttons, cancel_id, title, message, detail, icon).RunSynchronous(); } void ShowMessageBox(NativeWindow* parent, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, const gfx::ImageSkia& icon, const MessageBoxCallback& callback) { - (new GtkMessageBox(parent, type, buttons, title, message, detail, + (new GtkMessageBox(parent, type, buttons, cancel_id, title, message, detail, icon))->RunAsynchronous(callback); } void ShowErrorBox(const base::string16& title, const base::string16& content) { if (Browser::Get()->is_ready()) { - GtkMessageBox(nullptr, MESSAGE_BOX_TYPE_ERROR, { "OK" }, "Error", + GtkMessageBox(nullptr, MESSAGE_BOX_TYPE_ERROR, { "OK" }, 0, "Error", base::UTF16ToUTF8(title).c_str(), base::UTF16ToUTF8(content).c_str(), gfx::ImageSkia()).RunSynchronous(); diff --git a/atom/browser/ui/message_box_mac.mm b/atom/browser/ui/message_box_mac.mm index b3af25311ea..8fe3b7d060c 100644 --- a/atom/browser/ui/message_box_mac.mm +++ b/atom/browser/ui/message_box_mac.mm @@ -94,6 +94,7 @@ void SetReturnCode(int* ret_code, int result) { int ShowMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, @@ -125,6 +126,7 @@ int ShowMessageBox(NativeWindow* parent_window, void ShowMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, diff --git a/atom/browser/ui/message_box_win.cc b/atom/browser/ui/message_box_win.cc index 508d6eb233d..cd2c1104821 100644 --- a/atom/browser/ui/message_box_win.cc +++ b/atom/browser/ui/message_box_win.cc @@ -43,6 +43,7 @@ class MessageDialog : public views::WidgetDelegate, MessageDialog(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, @@ -85,6 +86,7 @@ class MessageDialog : public views::WidgetDelegate, gfx::ImageSkia icon_; bool delete_on_close_; + int cancel_id_; int result_; base::string16 title_; @@ -125,12 +127,14 @@ class MessageDialogClientView : public views::ClientView { MessageDialog::MessageDialog(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, const gfx::ImageSkia& icon) : icon_(icon), delete_on_close_(false), + cancel_id_(cancel_id), result_(-1), title_(base::UTF8ToUTF16(title)), parent_(parent_window), @@ -211,7 +215,7 @@ int MessageDialog::GetResult() const { return i; } - return 0; + return cancel_id_; } else { return result_; } @@ -337,12 +341,13 @@ void MessageDialog::ButtonPressed(views::Button* sender, int ShowMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, const gfx::ImageSkia& icon) { MessageDialog dialog( - parent_window, type, buttons, title, message, detail, icon); + parent_window, type, buttons, cancel_id, title, message, detail, icon); { base::MessageLoop::ScopedNestableTaskAllower allow( base::MessageLoopForUI::current()); @@ -357,6 +362,7 @@ int ShowMessageBox(NativeWindow* parent_window, void ShowMessageBox(NativeWindow* parent_window, MessageBoxType type, const std::vector& buttons, + int cancel_id, const std::string& title, const std::string& message, const std::string& detail, @@ -364,7 +370,7 @@ void ShowMessageBox(NativeWindow* parent_window, const MessageBoxCallback& callback) { // The dialog would be deleted when the dialog is closed. MessageDialog* dialog = new MessageDialog( - parent_window, type, buttons, title, message, detail, icon); + parent_window, type, buttons, cancel_id, title, message, detail, icon); dialog->set_callback(callback); dialog->Show(); } From fb537d91fce3f97ac3b902c36fcc4626eef0c959 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jul 2015 18:33:11 +0800 Subject: [PATCH 2/4] Set default cancelId in JavaScript --- atom/browser/api/lib/dialog.coffee | 8 +++++++- atom/browser/ui/message_box_gtk.cc | 13 +++++-------- atom/browser/ui/message_box_win.cc | 12 ++---------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/atom/browser/api/lib/dialog.coffee b/atom/browser/api/lib/dialog.coffee index 9c1c1934759..3dbd80a25ef 100644 --- a/atom/browser/api/lib/dialog.coffee +++ b/atom/browser/api/lib/dialog.coffee @@ -92,7 +92,13 @@ module.exports = options.message ?= '' options.detail ?= '' options.icon ?= null - options.cancelId ?= 0 + + unless options.cancelId? + options.cancelId = 0 + for text, i in options.buttons + if text.toLowerCase() in ['cancel', 'no'] + options.cancelId = i + break binding.showMessageBox messageBoxType, options.buttons, diff --git a/atom/browser/ui/message_box_gtk.cc b/atom/browser/ui/message_box_gtk.cc index f5a29f42c07..07695d49845 100644 --- a/atom/browser/ui/message_box_gtk.cc +++ b/atom/browser/ui/message_box_gtk.cc @@ -93,19 +93,16 @@ class GtkMessageBox { const char* TranslateToStock(int id, const std::string& text) { std::string lower = base::StringToLowerASCII(text); - if (lower == "cancel") { - cancel_id_ = id; + if (lower == "cancel") return GTK_STOCK_CANCEL; - } else if (lower == "no") { - cancel_id_ = id; + else if (lower == "no") return GTK_STOCK_NO; - } else if (lower == "ok") { + else if (lower == "ok") return GTK_STOCK_OK; - } else if (lower == "yes") { + else if (lower == "yes") return GTK_STOCK_YES; - } else { + else return text.c_str(); - } } void Show() { diff --git a/atom/browser/ui/message_box_win.cc b/atom/browser/ui/message_box_win.cc index cd2c1104821..e613b9aeb00 100644 --- a/atom/browser/ui/message_box_win.cc +++ b/atom/browser/ui/message_box_win.cc @@ -207,18 +207,10 @@ void MessageDialog::Close() { } int MessageDialog::GetResult() const { - // When the dialog is closed without choosing anything, we think the user - // chose 'Cancel', otherwise we think the default behavior is chosen. - if (result_ == -1) { - for (size_t i = 0; i < buttons_.size(); ++i) - if (LowerCaseEqualsASCII(buttons_[i]->GetText(), "cancel")) { - return i; - } - + if (result_ == -1) return cancel_id_; - } else { + else return result_; - } } //////////////////////////////////////////////////////////////////////////////// From df4b5f4ede3b6aa1d092bfe21216bb3309a5d513 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jul 2015 18:45:43 +0800 Subject: [PATCH 3/4] On OS X the "Cancel" is always get selected when dialog is cancelled --- atom/browser/api/lib/dialog.coffee | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/atom/browser/api/lib/dialog.coffee b/atom/browser/api/lib/dialog.coffee index 3dbd80a25ef..6ee334e29a5 100644 --- a/atom/browser/api/lib/dialog.coffee +++ b/atom/browser/api/lib/dialog.coffee @@ -93,6 +93,7 @@ module.exports = options.detail ?= '' options.icon ?= null + # Choose a default button to get selected when dialog is cancelled. unless options.cancelId? options.cancelId = 0 for text, i in options.buttons @@ -100,6 +101,12 @@ module.exports = options.cancelId = i break + # On OS X the "Cancel" is always get selected when dialog is cancelled. + if process.platform is 'darwin' + for text, i in options.buttons when text is 'Cancel' + options.cancelId = i + break + binding.showMessageBox messageBoxType, options.buttons, options.cancelId, From 7d456d35564d8fd9c7b518dc853806353d10af80 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jul 2015 18:51:49 +0800 Subject: [PATCH 4/4] docs: cancelId --- docs/api/dialog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/api/dialog.md b/docs/api/dialog.md index 30689d95863..2023809b54b 100644 --- a/docs/api/dialog.md +++ b/docs/api/dialog.md @@ -76,6 +76,11 @@ will be passed via `callback(filename)` * `message` String - Content of the message box * `detail` String - Extra information of the message * `icon` [NativeImage](native-image.md) + * `cancelId` Integer - The value will be returned when user cancels the dialog + instead of clicking the buttons of the dialog. By default it is the index + of the buttons that have "cancel" or "no" as label, or 0 if there is no such + buttons. On OS X the index of "Cancel" button will always be used as + `cancelId`, not matter whether it is already specified. * `callback` Function Shows a message box, it will block until the message box is closed. It returns