From 85d65886615ae4f5624b62c650a610ab78565125 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 23 Sep 2013 14:29:55 +0800 Subject: [PATCH] Make dialog.showMessageBox asynchronous. --- browser/api/atom_api_dialog.cc | 49 +++++++++++++++++++++++++++++----- browser/api/lib/dialog.coffee | 7 +++-- browser/ui/message_box_mac.mm | 9 ++++++- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/browser/api/atom_api_dialog.cc b/browser/api/atom_api_dialog.cc index 69b0135b..298cba49 100644 --- a/browser/api/atom_api_dialog.cc +++ b/browser/api/atom_api_dialog.cc @@ -6,12 +6,16 @@ #include +#include "base/bind.h" #include "base/utf_string_conversions.h" #include "base/values.h" #include "browser/api/atom_api_window.h" #include "browser/native_window.h" #include "browser/ui/file_dialog.h" #include "browser/ui/message_box.h" +#include "vendor/node/src/node_internals.h" + +using node::node_isolate; namespace atom { @@ -24,11 +28,25 @@ base::FilePath V8ValueToFilePath(v8::Handle path) { return base::FilePath::FromUTF8Unsafe(path_string); } -v8::Handle FilePathToV8Value(const base::FilePath path) { +v8::Handle ToV8Value(const base::FilePath& path) { std::string path_string(path.AsUTF8Unsafe()); return v8::String::New(path_string.data(), path_string.size()); } +v8::Handle ToV8Value(int code) { + return v8::Integer::New(code); +} + +template +void CallV8Function(v8::Persistent callback, T arg) { + DCHECK(!callback.IsEmpty()); + + v8::HandleScope scope; + v8::Handle value = ToV8Value(arg); + callback->Call(callback, 1, &value); + callback.Dispose(node_isolate); +} + void Initialize(v8::Handle target) { v8::HandleScope scope; @@ -58,6 +76,13 @@ v8::Handle ShowMessageBox(const v8::Arguments &args) { native_window = window->window(); } + v8::Persistent callback; + if (args[6]->IsFunction()) { + callback = v8::Persistent::New( + node_isolate, + v8::Local::Cast(args[6])); + } + MessageBoxType type = (MessageBoxType)(args[0]->IntegerValue()); std::vector buttons; @@ -69,9 +94,21 @@ v8::Handle ShowMessageBox(const v8::Arguments &args) { std::string message(*v8::String::Utf8Value(args[3])); std::string detail(*v8::String::Utf8Value(args[4])); - int chosen = atom::ShowMessageBox( - native_window, type, buttons, title, message, detail); - return scope.Close(v8::Integer::New(chosen)); + if (callback.IsEmpty()) { + int chosen = atom::ShowMessageBox( + native_window, type, buttons, title, message, detail); + return scope.Close(v8::Integer::New(chosen)); + } else { + atom::ShowMessageBox( + native_window, + type, + buttons, + title, + message, + detail, + base::Bind(&CallV8Function, callback)); + return v8::Undefined(); + } } v8::Handle ShowOpenDialog(const v8::Arguments &args) { @@ -92,7 +129,7 @@ v8::Handle ShowOpenDialog(const v8::Arguments &args) { v8::Handle result = v8::Array::New(paths.size()); for (size_t i = 0; i < paths.size(); ++i) - result->Set(i, FilePathToV8Value(paths[i])); + result->Set(i, ToV8Value(paths[i])); return scope.Close(result); } @@ -119,7 +156,7 @@ v8::Handle ShowSaveDialog(const v8::Arguments &args) { &path)) return v8::Undefined(); - return scope.Close(FilePathToV8Value(path)); + return scope.Close(ToV8Value(path)); } } // namespace api diff --git a/browser/api/lib/dialog.coffee b/browser/api/lib/dialog.coffee index 59568bcf..7011de20 100644 --- a/browser/api/lib/dialog.coffee +++ b/browser/api/lib/dialog.coffee @@ -30,8 +30,10 @@ module.exports = binding.showSaveDialog window, options.title, options.defaultPath - showMessageBox: (window, options) -> + showMessageBox: (window, options, callback) -> if window? and window.constructor isnt BrowserWindow + # Shift. + callback = options options = window window = null @@ -51,4 +53,5 @@ module.exports = String(options.title), String(options.message), String(options.detail), - window + window, + callback diff --git a/browser/ui/message_box_mac.mm b/browser/ui/message_box_mac.mm index fb5e6cb6..b5d0cc4b 100644 --- a/browser/ui/message_box_mac.mm +++ b/browser/ui/message_box_mac.mm @@ -103,7 +103,14 @@ void ShowMessageBox(NativeWindow* parent_window, const MessageBoxCallback& callback) { NSAlert* alert = CreateNSAlert( parent_window, type, buttons, title, message, detail); - [[ModalDelegate alloc] initWithCallback:callback andAlert:alert]; + ModalDelegate* delegate = [[ModalDelegate alloc] initWithCallback:callback + andAlert:alert]; + + NSWindow* window = parent_window ? parent_window->GetNativeWindow() : nil; + [alert beginSheetModalForWindow:window + modalDelegate:delegate + didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) + contextInfo:nil]; } } // namespace atom -- 2.43.0