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/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<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()
|
||||
: 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<Menu*>(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<Menu*>(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<Menu*>(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<v8::Value> 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<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;
|
||||
v8::Handle<v8::Value> 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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue