From: Cheng Zhao Date: Fri, 13 Feb 2015 03:47:22 +0000 (+0800) Subject: Simplify the code to call delegate method X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b28a241dbf90e63fffef0837b7ccb766d53056fb;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git Simplify the code to call delegate method --- diff --git a/atom/browser/api/atom_api_menu.cc b/atom/browser/api/atom_api_menu.cc index 6c3bd89..9704909 100644 --- a/atom/browser/api/atom_api_menu.cc +++ b/atom/browser/api/atom_api_menu.cc @@ -7,6 +7,7 @@ #include "atom/browser/native_window.h" #include "atom/common/native_mate_converters/accelerator_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" +#include "native_mate/callback.h" #include "native_mate/constructor.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" @@ -17,30 +18,6 @@ namespace atom { namespace api { -namespace { - -// Call method of delegate object. -v8::Handle CallDelegate(v8::Isolate* isolate, - v8::Handle default_value, - v8::Handle menu, - const char* method, - int command_id) { - v8::Handle delegate = menu->Get( - MATE_STRING_NEW(isolate, "delegate")); - if (!delegate->IsObject()) - return default_value; - - v8::Handle function = v8::Handle::Cast( - delegate->ToObject()->Get(MATE_STRING_NEW(isolate, method))); - if (!function->IsFunction()) - return default_value; - - v8::Handle argv = MATE_INTEGER_NEW(isolate, command_id); - return function->Call(isolate->GetCurrentContext()->Global(), 1, &argv); -} - -} // namespace - Menu::Menu() : model_(new ui::SimpleMenuModel(this)), parent_(NULL) { @@ -49,37 +26,30 @@ Menu::Menu() Menu::~Menu() { } +void Menu::AfterInit(v8::Isolate* isolate) { + mate::Dictionary wrappable(isolate, GetWrapper(isolate)); + mate::Dictionary delegate; + if (!wrappable.Get("delegate", &delegate)) + return; + + delegate.Get("isCommandIdChecked", &is_checked_); + delegate.Get("isCommandIdEnabled", &is_enabled_); + delegate.Get("isCommandIdVisible", &is_visible_); + delegate.Get("getAcceleratorForCommandId", &get_accelerator_); + delegate.Get("executeCommand", &execute_command_); + delegate.Get("menuWillShow", &menu_will_show_); +} + bool Menu::IsCommandIdChecked(int command_id) const { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - return CallDelegate(isolate, - MATE_FALSE(isolate), - const_cast(this)->GetWrapper(isolate), - "isCommandIdChecked", - command_id)->BooleanValue(); + return is_checked_.Run(command_id); } bool Menu::IsCommandIdEnabled(int command_id) const { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - return CallDelegate(isolate, - MATE_TRUE(isolate), - const_cast(this)->GetWrapper(isolate), - "isCommandIdEnabled", - command_id)->BooleanValue(); + return is_enabled_.Run(command_id); } bool Menu::IsCommandIdVisible(int command_id) const { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - return CallDelegate(isolate, - MATE_TRUE(isolate), - const_cast(this)->GetWrapper(isolate), - "isCommandIdVisible", - command_id)->BooleanValue(); + return is_visible_.Run(command_id); } bool Menu::GetAcceleratorForCommandId(int command_id, @@ -87,69 +57,16 @@ bool Menu::GetAcceleratorForCommandId(int command_id, v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(isolate); v8::HandleScope handle_scope(isolate); - v8::Handle shortcut = CallDelegate(isolate, - MATE_UNDEFINED(isolate), - GetWrapper(isolate), - "getAcceleratorForCommandId", - command_id); - return mate::ConvertFromV8(isolate, shortcut, accelerator); -} - -bool Menu::IsItemForCommandIdDynamic(int command_id) const { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - return CallDelegate(isolate, - MATE_FALSE(isolate), - const_cast(this)->GetWrapper(isolate), - "isItemForCommandIdDynamic", - command_id)->BooleanValue(); -} - -base::string16 Menu::GetLabelForCommandId(int command_id) const { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - v8::Handle result = CallDelegate( - isolate, - MATE_FALSE(isolate), - const_cast(this)->GetWrapper(isolate), - "getLabelForCommandId", - command_id); - base::string16 label; - mate::ConvertFromV8(isolate, result, &label); - return label; -} - -base::string16 Menu::GetSublabelForCommandId(int command_id) const { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - v8::Handle result = CallDelegate( - isolate, - MATE_FALSE(isolate), - const_cast(this)->GetWrapper(isolate), - "getSubLabelForCommandId", - command_id); - base::string16 label; - mate::ConvertFromV8(isolate, result, &label); - return label; + v8::Handle val = get_accelerator_.Run(command_id); + return mate::ConvertFromV8(isolate, val, accelerator); } void Menu::ExecuteCommand(int command_id, int event_flags) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - CallDelegate(isolate, MATE_FALSE(isolate), GetWrapper(isolate), - "executeCommand", command_id); + execute_command_.Run(command_id); } void Menu::MenuWillShow(ui::SimpleMenuModel* source) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - CallDelegate(isolate, MATE_FALSE(isolate), GetWrapper(isolate), - "menuWillShow", -1); + menu_will_show_.Run(); } void Menu::AttachToWindow(Window* window) { diff --git a/atom/browser/api/atom_api_menu.h b/atom/browser/api/atom_api_menu.h index 7b8e68d..0c8f28e 100644 --- a/atom/browser/api/atom_api_menu.h +++ b/atom/browser/api/atom_api_menu.h @@ -8,6 +8,7 @@ #include #include "atom/browser/api/atom_api_window.h" +#include "base/callback.h" #include "base/memory/scoped_ptr.h" #include "ui/base/models/simple_menu_model.h" #include "native_mate/wrappable.h" @@ -16,8 +17,6 @@ namespace atom { namespace api { -class MenuMac; - class Menu : public mate::Wrappable, public ui::SimpleMenuModel::Delegate { public: @@ -40,6 +39,9 @@ class Menu : public mate::Wrappable, Menu(); virtual ~Menu(); + // mate::Wrappable: + void AfterInit(v8::Isolate* isolate) override; + // ui::SimpleMenuModel::Delegate implementations: bool IsCommandIdChecked(int command_id) const override; bool IsCommandIdEnabled(int command_id) const override; @@ -47,9 +49,6 @@ class Menu : public mate::Wrappable, bool GetAcceleratorForCommandId( int command_id, ui::Accelerator* accelerator) override; - bool IsItemForCommandIdDynamic(int command_id) const override; - base::string16 GetLabelForCommandId(int command_id) const override; - base::string16 GetSublabelForCommandId(int command_id) const override; void ExecuteCommand(int command_id, int event_flags) override; void MenuWillShow(ui::SimpleMenuModel* source) override; @@ -85,6 +84,14 @@ class Menu : public mate::Wrappable, bool IsEnabledAt(int index) const; bool IsVisibleAt(int index) const; + // Stored delegate methods. + base::Callback is_checked_; + base::Callback is_enabled_; + base::Callback is_visible_; + base::Callback(int)> get_accelerator_; + base::Callback execute_command_; + base::Callback menu_will_show_; + DISALLOW_COPY_AND_ASSIGN(Menu); }; diff --git a/vendor/native_mate b/vendor/native_mate index 8d537ee..d0db7bf 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit 8d537ee2b6da29c1aa38928590d4c56700e1c69b +Subproject commit d0db7bfb586afe9f491bd4cb368353d2660ecfe1