From: Cheng Zhao Date: Fri, 18 Apr 2014 09:19:15 +0000 (+0800) Subject: Use the new function converter in dialog api. X-Git-Tag: v0.12.0~18^2~22 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=1dd57bd3a49a9d69d90d743c31910f4a2d57a882;p=electron.git Use the new function converter in dialog api. --- diff --git a/atom/browser/api/atom_api_dialog.cc b/atom/browser/api/atom_api_dialog.cc index b4d120f0..8fe5a131 100644 --- a/atom/browser/api/atom_api_dialog.cc +++ b/atom/browser/api/atom_api_dialog.cc @@ -11,8 +11,8 @@ #include "atom/browser/ui/file_dialog.h" #include "atom/browser/ui/message_box.h" #include "atom/common/native_mate_converters/file_path_converter.h" +#include "atom/common/native_mate_converters/function_converter.h" #include "native_mate/dictionary.h" -#include "native_mate/scoped_persistent.h" #include "atom/common/node_includes.h" @@ -37,48 +37,11 @@ struct Converter { } }; - -typedef scoped_refptr> RefCountedV8Function; - -template<> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Handle val, - RefCountedV8Function* out) { - if (!val->IsFunction()) - return false; - - v8::Handle function = v8::Handle::Cast(val); - *out = new mate::RefCountedPersistent(function); - return true; - } -}; - } // namespace mate namespace { -template -void CallV8Function(const mate::RefCountedV8Function& callback, T arg) { - v8::Locker locker(node_isolate); - v8::HandleScope handle_scope(node_isolate); - - v8::Handle value = mate::Converter::ToV8(node_isolate, arg); - callback->NewHandle(node_isolate)->Call( - v8::Context::GetCurrent()->Global(), 1, &value); -} - -template -void CallV8Function2( - const mate::RefCountedV8Function& callback, bool result, const T& arg) { - if (result) - return CallV8Function(callback, arg); - else - return CallV8Function(callback, NULL); -} - - void ShowMessageBox(int type, const std::vector& buttons, const std::string& title, @@ -87,18 +50,12 @@ void ShowMessageBox(int type, atom::NativeWindow* window, mate::Arguments* args) { v8::Handle peek = args->PeekNext(); - mate::RefCountedV8Function callback; - if (mate::Converter::FromV8(node_isolate, - peek, - &callback)) { - atom::ShowMessageBox( - window, - (atom::MessageBoxType)type, - buttons, - title, - message, - detail, - base::Bind(&CallV8Function, callback)); + atom::MessageBoxCallback callback; + if (mate::Converter::FromV8(node_isolate, + peek, + &callback)) { + atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title, + message, detail, callback); } else { int chosen = atom::ShowMessageBox( window, @@ -117,17 +74,12 @@ void ShowOpenDialog(const std::string& title, atom::NativeWindow* window, mate::Arguments* args) { v8::Handle peek = args->PeekNext(); - mate::RefCountedV8Function callback; - if (mate::Converter::FromV8(node_isolate, - peek, - &callback)) { - file_dialog::ShowOpenDialog( - window, - title, - default_path, - properties, - base::Bind(&CallV8Function2>, - callback)); + file_dialog::OpenDialogCallback callback; + if (mate::Converter::FromV8(node_isolate, + peek, + &callback)) { + file_dialog::ShowOpenDialog(window, title, default_path, properties, + callback); } else { std::vector paths; if (file_dialog::ShowOpenDialog(window, @@ -144,15 +96,11 @@ void ShowSaveDialog(const std::string& title, atom::NativeWindow* window, mate::Arguments* args) { v8::Handle peek = args->PeekNext(); - mate::RefCountedV8Function callback; - if (mate::Converter::FromV8(node_isolate, - peek, - &callback)) { - file_dialog::ShowSaveDialog( - window, - title, - default_path, - base::Bind(&CallV8Function2, callback)); + file_dialog::SaveDialogCallback callback; + if (mate::Converter::FromV8(node_isolate, + peek, + &callback)) { + file_dialog::ShowSaveDialog(window, title, default_path, callback); } else { base::FilePath path; if (file_dialog::ShowSaveDialog(window, diff --git a/atom/browser/api/lib/dialog.coffee b/atom/browser/api/lib/dialog.coffee index 5b333f4a..369fed89 100644 --- a/atom/browser/api/lib/dialog.coffee +++ b/atom/browser/api/lib/dialog.coffee @@ -30,7 +30,7 @@ module.exports = String(options.defaultPath), properties, window, - callback + (success, result) -> callback if success then result showSaveDialog: (window, options, callback) -> unless window?.constructor is BrowserWindow @@ -46,7 +46,7 @@ module.exports = binding.showSaveDialog String(options.title), String(options.defaultPath), window, - callback + (success, result) -> callback if success then result showMessageBox: (window, options, callback) -> unless window?.constructor is BrowserWindow