Simplify the code to call delegate method
This commit is contained in:
parent
afd4052bde
commit
b28a241dbf
3 changed files with 35 additions and 111 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/common/native_mate_converters/accelerator_converter.h"
|
#include "atom/common/native_mate_converters/accelerator_converter.h"
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
|
#include "native_mate/callback.h"
|
||||||
#include "native_mate/constructor.h"
|
#include "native_mate/constructor.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
|
@ -17,30 +18,6 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
// Call method of delegate object.
|
|
||||||
v8::Handle<v8::Value> CallDelegate(v8::Isolate* isolate,
|
|
||||||
v8::Handle<v8::Value> default_value,
|
|
||||||
v8::Handle<v8::Object> menu,
|
|
||||||
const char* method,
|
|
||||||
int command_id) {
|
|
||||||
v8::Handle<v8::Value> delegate = menu->Get(
|
|
||||||
MATE_STRING_NEW(isolate, "delegate"));
|
|
||||||
if (!delegate->IsObject())
|
|
||||||
return default_value;
|
|
||||||
|
|
||||||
v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(
|
|
||||||
delegate->ToObject()->Get(MATE_STRING_NEW(isolate, method)));
|
|
||||||
if (!function->IsFunction())
|
|
||||||
return default_value;
|
|
||||||
|
|
||||||
v8::Handle<v8::Value> argv = MATE_INTEGER_NEW(isolate, command_id);
|
|
||||||
return function->Call(isolate->GetCurrentContext()->Global(), 1, &argv);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
Menu::Menu()
|
Menu::Menu()
|
||||||
: model_(new ui::SimpleMenuModel(this)),
|
: model_(new ui::SimpleMenuModel(this)),
|
||||||
parent_(NULL) {
|
parent_(NULL) {
|
||||||
|
@ -49,37 +26,30 @@ Menu::Menu()
|
||||||
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 {
|
bool Menu::IsCommandIdChecked(int command_id) const {
|
||||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
return is_checked_.Run(command_id);
|
||||||
v8::Locker locker(isolate);
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
|
||||||
return CallDelegate(isolate,
|
|
||||||
MATE_FALSE(isolate),
|
|
||||||
const_cast<Menu*>(this)->GetWrapper(isolate),
|
|
||||||
"isCommandIdChecked",
|
|
||||||
command_id)->BooleanValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Menu::IsCommandIdEnabled(int command_id) const {
|
bool Menu::IsCommandIdEnabled(int command_id) const {
|
||||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
return is_enabled_.Run(command_id);
|
||||||
v8::Locker locker(isolate);
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
|
||||||
return CallDelegate(isolate,
|
|
||||||
MATE_TRUE(isolate),
|
|
||||||
const_cast<Menu*>(this)->GetWrapper(isolate),
|
|
||||||
"isCommandIdEnabled",
|
|
||||||
command_id)->BooleanValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Menu::IsCommandIdVisible(int command_id) const {
|
bool Menu::IsCommandIdVisible(int command_id) const {
|
||||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
return is_visible_.Run(command_id);
|
||||||
v8::Locker locker(isolate);
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
|
||||||
return CallDelegate(isolate,
|
|
||||||
MATE_TRUE(isolate),
|
|
||||||
const_cast<Menu*>(this)->GetWrapper(isolate),
|
|
||||||
"isCommandIdVisible",
|
|
||||||
command_id)->BooleanValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Menu::GetAcceleratorForCommandId(int 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::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
v8::Locker locker(isolate);
|
v8::Locker locker(isolate);
|
||||||
v8::HandleScope handle_scope(isolate);
|
v8::HandleScope handle_scope(isolate);
|
||||||
v8::Handle<v8::Value> shortcut = CallDelegate(isolate,
|
v8::Handle<v8::Value> val = get_accelerator_.Run(command_id);
|
||||||
MATE_UNDEFINED(isolate),
|
return mate::ConvertFromV8(isolate, val, accelerator);
|
||||||
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<Menu*>(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<v8::Value> result = CallDelegate(
|
|
||||||
isolate,
|
|
||||||
MATE_FALSE(isolate),
|
|
||||||
const_cast<Menu*>(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<v8::Value> result = CallDelegate(
|
|
||||||
isolate,
|
|
||||||
MATE_FALSE(isolate),
|
|
||||||
const_cast<Menu*>(this)->GetWrapper(isolate),
|
|
||||||
"getSubLabelForCommandId",
|
|
||||||
command_id);
|
|
||||||
base::string16 label;
|
|
||||||
mate::ConvertFromV8(isolate, result, &label);
|
|
||||||
return label;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::ExecuteCommand(int command_id, int event_flags) {
|
void Menu::ExecuteCommand(int command_id, int event_flags) {
|
||||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
execute_command_.Run(command_id);
|
||||||
v8::Locker locker(isolate);
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
|
||||||
CallDelegate(isolate, MATE_FALSE(isolate), GetWrapper(isolate),
|
|
||||||
"executeCommand", command_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
|
void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
|
||||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
menu_will_show_.Run();
|
||||||
v8::Locker locker(isolate);
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
|
||||||
CallDelegate(isolate, MATE_FALSE(isolate), GetWrapper(isolate),
|
|
||||||
"menuWillShow", -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::AttachToWindow(Window* window) {
|
void Menu::AttachToWindow(Window* window) {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_window.h"
|
#include "atom/browser/api/atom_api_window.h"
|
||||||
|
#include "base/callback.h"
|
||||||
#include "base/memory/scoped_ptr.h"
|
#include "base/memory/scoped_ptr.h"
|
||||||
#include "ui/base/models/simple_menu_model.h"
|
#include "ui/base/models/simple_menu_model.h"
|
||||||
#include "native_mate/wrappable.h"
|
#include "native_mate/wrappable.h"
|
||||||
|
@ -16,8 +17,6 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class MenuMac;
|
|
||||||
|
|
||||||
class Menu : public mate::Wrappable,
|
class Menu : public mate::Wrappable,
|
||||||
public ui::SimpleMenuModel::Delegate {
|
public ui::SimpleMenuModel::Delegate {
|
||||||
public:
|
public:
|
||||||
|
@ -40,6 +39,9 @@ class Menu : public mate::Wrappable,
|
||||||
Menu();
|
Menu();
|
||||||
virtual ~Menu();
|
virtual ~Menu();
|
||||||
|
|
||||||
|
// mate::Wrappable:
|
||||||
|
void AfterInit(v8::Isolate* isolate) override;
|
||||||
|
|
||||||
// ui::SimpleMenuModel::Delegate implementations:
|
// ui::SimpleMenuModel::Delegate implementations:
|
||||||
bool IsCommandIdChecked(int command_id) const override;
|
bool IsCommandIdChecked(int command_id) const override;
|
||||||
bool IsCommandIdEnabled(int command_id) const override;
|
bool IsCommandIdEnabled(int command_id) const override;
|
||||||
|
@ -47,9 +49,6 @@ class Menu : public mate::Wrappable,
|
||||||
bool GetAcceleratorForCommandId(
|
bool GetAcceleratorForCommandId(
|
||||||
int command_id,
|
int command_id,
|
||||||
ui::Accelerator* accelerator) override;
|
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 ExecuteCommand(int command_id, int event_flags) override;
|
||||||
void MenuWillShow(ui::SimpleMenuModel* source) override;
|
void MenuWillShow(ui::SimpleMenuModel* source) override;
|
||||||
|
|
||||||
|
@ -85,6 +84,14 @@ class Menu : public mate::Wrappable,
|
||||||
bool IsEnabledAt(int index) const;
|
bool IsEnabledAt(int index) const;
|
||||||
bool IsVisibleAt(int index) const;
|
bool IsVisibleAt(int index) const;
|
||||||
|
|
||||||
|
// Stored delegate methods.
|
||||||
|
base::Callback<bool(int)> is_checked_;
|
||||||
|
base::Callback<bool(int)> is_enabled_;
|
||||||
|
base::Callback<bool(int)> is_visible_;
|
||||||
|
base::Callback<v8::Handle<v8::Value>(int)> get_accelerator_;
|
||||||
|
base::Callback<void(int)> execute_command_;
|
||||||
|
base::Callback<void()> menu_will_show_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Menu);
|
DISALLOW_COPY_AND_ASSIGN(Menu);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
2
vendor/native_mate
vendored
2
vendor/native_mate
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit 8d537ee2b6da29c1aa38928590d4c56700e1c69b
|
Subproject commit d0db7bfb586afe9f491bd4cb368353d2660ecfe1
|
Loading…
Add table
Reference in a new issue