Browse Source

Merge pull request #2307 from atom/dialog-options

Add "noLink" option for showMessageBox
Cheng Zhao 9 years ago
parent
commit
de17894fce

+ 7 - 11
atom/browser/api/atom_api_dialog.cc

@@ -42,28 +42,24 @@ namespace {
 void ShowMessageBox(int type,
                     const std::vector<std::string>& buttons,
                     int cancel_id,
-                    const std::vector<std::string>& texts,
+                    int options,
+                    const std::string& title,
+                    const std::string& message,
+                    const std::string& detail,
                     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::Local<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, cancel_id,
-                         title, message, detail, icon, callback);
+                         options, title, message, detail, icon, callback);
   } else {
     int chosen = atom::ShowMessageBox(window, (atom::MessageBoxType)type,
-                                      buttons, cancel_id, title, message,
-                                      detail, icon);
+                                      buttons, cancel_id, options, title,
+                                      message, detail, icon);
     args->Return(chosen);
   }
 }

+ 9 - 1
atom/browser/api/lib/dialog.coffee

@@ -11,6 +11,9 @@ fileDialogProperties =
 
 messageBoxTypes = ['none', 'info', 'warning', 'error', 'question']
 
+messageBoxOptions =
+  noLink: 1 << 0
+
 parseArgs = (window, options, callback) ->
   unless window is null or window?.constructor is BrowserWindow
     # Shift.
@@ -101,10 +104,15 @@ module.exports =
           options.cancelId = i
           break
 
+    flags = if options.noLink then messageBoxOptions.noLink else 0
+
     binding.showMessageBox messageBoxType,
                            options.buttons,
                            options.cancelId,
-                           [options.title, options.message, options.detail],
+                           flags,
+                           options.title,
+                           options.message,
+                           options.detail,
                            options.icon,
                            window,
                            callback

+ 7 - 0
atom/browser/ui/message_box.h

@@ -27,12 +27,18 @@ enum MessageBoxType {
   MESSAGE_BOX_TYPE_QUESTION,
 };
 
+enum MessageBoxOptions {
+  MESSAGE_BOX_NONE    = 0,
+  MESSAGE_BOX_NO_LINK = 1 << 0,
+};
+
 typedef base::Callback<void(int code)> MessageBoxCallback;
 
 int ShowMessageBox(NativeWindow* parent_window,
                    MessageBoxType type,
                    const std::vector<std::string>& buttons,
                    int cancel_id,
+                   int options,
                    const std::string& title,
                    const std::string& message,
                    const std::string& detail,
@@ -42,6 +48,7 @@ void ShowMessageBox(NativeWindow* parent_window,
                     MessageBoxType type,
                     const std::vector<std::string>& buttons,
                     int cancel_id,
+                    int options,
                     const std::string& title,
                     const std::string& message,
                     const std::string& detail,

+ 2 - 0
atom/browser/ui/message_box_gtk.cc

@@ -162,6 +162,7 @@ int ShowMessageBox(NativeWindow* parent,
                    MessageBoxType type,
                    const std::vector<std::string>& buttons,
                    int cancel_id,
+                   int options,
                    const std::string& title,
                    const std::string& message,
                    const std::string& detail,
@@ -174,6 +175,7 @@ void ShowMessageBox(NativeWindow* parent,
                     MessageBoxType type,
                     const std::vector<std::string>& buttons,
                     int cancel_id,
+                    int options,
                     const std::string& title,
                     const std::string& message,
                     const std::string& detail,

+ 2 - 0
atom/browser/ui/message_box_mac.mm

@@ -95,6 +95,7 @@ int ShowMessageBox(NativeWindow* parent_window,
                    MessageBoxType type,
                    const std::vector<std::string>& buttons,
                    int cancel_id,
+                   int options,
                    const std::string& title,
                    const std::string& message,
                    const std::string& detail,
@@ -127,6 +128,7 @@ void ShowMessageBox(NativeWindow* parent_window,
                     MessageBoxType type,
                     const std::vector<std::string>& buttons,
                     int cancel_id,
+                    int options,
                     const std::string& title,
                     const std::string& message,
                     const std::string& detail,

+ 18 - 7
atom/browser/ui/message_box_win.cc

@@ -72,6 +72,7 @@ int ShowMessageBoxUTF16(HWND parent,
                         MessageBoxType type,
                         const std::vector<base::string16>& buttons,
                         int cancel_id,
+                        int options,
                         const base::string16& title,
                         const base::string16& message,
                         const base::string16& detail,
@@ -122,11 +123,17 @@ int ShowMessageBoxUTF16(HWND parent,
   // and custom buttons in pButtons.
   std::map<int, int> id_map;
   std::vector<TASKDIALOG_BUTTON> dialog_buttons;
-  MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
+  if (options & MESSAGE_BOX_NO_LINK) {
+    for (size_t i = 0; i < buttons.size(); ++i)
+      dialog_buttons.push_back({i + kIDStart, buttons[i].c_str()});
+  } else {
+    MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
+  }
   if (dialog_buttons.size() > 0) {
     config.pButtons = &dialog_buttons.front();
     config.cButtons = dialog_buttons.size();
-    config.dwFlags |= TDF_USE_COMMAND_LINKS;  // custom buttons as links.
+    if (!(options & MESSAGE_BOX_NO_LINK))
+      config.dwFlags |= TDF_USE_COMMAND_LINKS;  // custom buttons as links.
   }
 
   int id = 0;
@@ -144,13 +151,14 @@ void RunMessageBoxInNewThread(base::Thread* thread,
                               MessageBoxType type,
                               const std::vector<std::string>& buttons,
                               int cancel_id,
+                              int options,
                               const std::string& title,
                               const std::string& message,
                               const std::string& detail,
                               const gfx::ImageSkia& icon,
                               const MessageBoxCallback& callback) {
-  int result = ShowMessageBox(parent, type, buttons, cancel_id, title, message,
-                              detail, icon);
+  int result = ShowMessageBox(parent, type, buttons, cancel_id, options, title,
+                              message, detail, icon);
   content::BrowserThread::PostTask(
       content::BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
   content::BrowserThread::DeleteSoon(
@@ -163,6 +171,7 @@ int ShowMessageBox(NativeWindow* parent,
                    MessageBoxType type,
                    const std::vector<std::string>& buttons,
                    int cancel_id,
+                   int options,
                    const std::string& title,
                    const std::string& message,
                    const std::string& detail,
@@ -180,6 +189,7 @@ int ShowMessageBox(NativeWindow* parent,
                              type,
                              utf16_buttons,
                              cancel_id,
+                             options,
                              base::UTF8ToUTF16(title),
                              base::UTF8ToUTF16(message),
                              base::UTF8ToUTF16(detail),
@@ -190,6 +200,7 @@ void ShowMessageBox(NativeWindow* parent,
                     MessageBoxType type,
                     const std::vector<std::string>& buttons,
                     int cancel_id,
+                    int options,
                     const std::string& title,
                     const std::string& message,
                     const std::string& detail,
@@ -207,12 +218,12 @@ void ShowMessageBox(NativeWindow* parent,
   unretained->message_loop()->PostTask(
       FROM_HERE,
       base::Bind(&RunMessageBoxInNewThread, base::Unretained(unretained),
-                 parent, type, buttons, cancel_id, title, message, detail, icon,
-                 callback));
+                 parent, type, buttons, cancel_id, options, title, message,
+                 detail, icon, callback));
 }
 
 void ShowErrorBox(const base::string16& title, const base::string16& content) {
-  ShowMessageBoxUTF16(NULL, MESSAGE_BOX_TYPE_ERROR, {}, 0, L"Error", title,
+  ShowMessageBoxUTF16(NULL, MESSAGE_BOX_TYPE_ERROR, {}, 0, 0, L"Error", title,
                       content, gfx::ImageSkia());
 }
 

+ 6 - 1
docs/api/dialog.md

@@ -80,7 +80,12 @@ will be passed via `callback(filename)`
     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 and Windows the index of "Cancel" button will always be
-    used as `cancelId`, not matter whether it is already specified.
+    used as `cancelId`, not matter whether it is already specified
+  * `noLink` Boolean - On Windows Electron would try to figure out which ones of
+    the `buttons` are common buttons (like "Cancel" or "Yes"), and show the
+    others as command links in the dialog, this can make the dialog appear in
+    the style of modern Windows apps. If you don't like this behavior, you can
+    specify `noLink` to `true`
 * `callback` Function
 
 Shows a message box, it will block until the message box is closed. It returns

+ 1 - 1
vendor/native_mate

@@ -1 +1 @@
-Subproject commit 41cd6d13c9c9be164f427864277f3cc36b69eb39
+Subproject commit 656e403f0102c59428261c1eaad22912d2bbd3c5