From fb537d91fce3f97ac3b902c36fcc4626eef0c959 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jul 2015 18:33:11 +0800 Subject: [PATCH] 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 9c1c1934..3dbd80a2 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 f5a29f42..07695d49 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 cd2c1104..e613b9ae 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_; - } } //////////////////////////////////////////////////////////////////////////////// -- 2.43.0