Expose MenuModel's methods in js.

This commit is contained in:
Cheng Zhao 2013-05-07 00:03:34 +08:00
parent 7cc04035c4
commit dae98fa43f
2 changed files with 245 additions and 0 deletions

View file

@ -4,10 +4,30 @@
#include "browser/api/atom_api_menu.h"
#define UNWRAP_MEMNU_AND_CHECK \
Menu* self = ObjectWrap::Unwrap<Menu>(args.This()); \
if (self == NULL) \
return node::ThrowError("Menu is already destroyed")
namespace atom {
namespace api {
namespace {
// Converts a V8 value to a string16.
string16 V8ValueToUTF16(v8::Handle<v8::Value> value) {
v8::String::Value s(value);
return string16(reinterpret_cast<const char16*>(*s), s.length());
}
// Converts string16 to V8 String.
v8::Handle<v8::Value> UTF16ToV8Value(const string16& s) {
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
}
} // namespace
Menu::Menu(v8::Handle<v8::Object> wrapper)
: EventEmitter(wrapper),
model_(new ui::SimpleMenuModel(this)) {
@ -44,6 +64,194 @@ v8::Handle<v8::Value> Menu::New(const v8::Arguments &args) {
return args.This();
}
// static
v8::Handle<v8::Value> Menu::InsertItem(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString())
return node::ThrowTypeError("Bad argument");
int index = args[0]->IntegerValue();
if (index < 0)
self->model_->AddItem(args[1]->IntegerValue(), V8ValueToUTF16(args[2]));
else
self->model_->InsertItemAt(
index, args[1]->IntegerValue(), V8ValueToUTF16(args[2]));
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::InsertCheckItem(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString())
return node::ThrowTypeError("Bad argument");
int index = args[0]->IntegerValue();
if (index < 0)
self->model_->AddCheckItem(args[1]->IntegerValue(), V8ValueToUTF16(args[2]));
else
self->model_->InsertCheckItemAt(
index, args[1]->IntegerValue(), V8ValueToUTF16(args[2]));
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::InsertRadioItem(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() ||
!args[1]->IsNumber() ||
!args[2]->IsString() ||
!args[3]->IsNumber())
return node::ThrowTypeError("Bad argument");
int index = args[0]->IntegerValue();
int command_id = args[1]->IntegerValue();
int group_id = args[3]->IntegerValue();
if (index < 0)
self->model_->AddRadioItem(command_id, V8ValueToUTF16(args[2]), group_id);
else
self->model_->InsertRadioItemAt(
index, command_id, V8ValueToUTF16(args[2]), group_id);
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::InsertSeparator(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString())
return node::ThrowTypeError("Bad argument");
int index = args[0]->IntegerValue();
if (index < 0)
self->model_->AddSeparator(ui::NORMAL_SEPARATOR);
else
self->model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::InsertSubMenu(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
// FIXME should rely on js code to keep a reference of submenu and check
// the constructor type of menu object.
if (!args[0]->IsNumber() ||
!args[1]->IsNumber() ||
!args[2]->IsString() ||
!args[3]->IsObject())
return node::ThrowTypeError("Bad argument");
Menu* submenu = ObjectWrap::Unwrap<Menu>(args[3]->ToObject());
if (!submenu)
return node::ThrowTypeError("The submenu is already destroyed");
int index = args[0]->IntegerValue();
int command_id = args[1]->IntegerValue();
if (index < 0)
self->model_->AddSubMenu(
command_id, V8ValueToUTF16(args[2]), submenu->model_.get());
else
self->model_->InsertSubMenuAt(
index, command_id, V8ValueToUTF16(args[2]), submenu->model_.get());
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::SetIcon(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() || !args[1]->IsString())
return node::ThrowTypeError("Bad argument");
// FIXME use webkit_glue's image decoder here.
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::SetSublabel(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() || !args[1]->IsString())
return node::ThrowTypeError("Bad argument");
self->model_->SetSublabel(args[0]->IntegerValue(), V8ValueToUTF16(args[1]));
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::Clear(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
self->model_->Clear();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::GetIndexOfCommandId(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return v8::Integer::New(self->model_->GetIndexOfCommandId(args[0]->IntegerValue()));
}
// static
v8::Handle<v8::Value> Menu::GetItemCount(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return v8::Integer::New(self->model_->GetItemCount());
}
// static
v8::Handle<v8::Value> Menu::GetCommandIdAt(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return v8::Integer::New(self->model_->GetCommandIdAt(args[0]->IntegerValue()));
}
// static
v8::Handle<v8::Value> Menu::GetLabelAt(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return UTF16ToV8Value(self->model_->GetLabelAt(args[0]->IntegerValue()));
}
// static
v8::Handle<v8::Value> Menu::GetSublabelAt(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return UTF16ToV8Value(self->model_->GetSublabelAt(args[0]->IntegerValue()));
}
// static
v8::Handle<v8::Value> Menu::IsItemCheckedAt(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return v8::Boolean::New(
self->model_->IsItemCheckedAt(args[0]->IntegerValue()));
}
// static
v8::Handle<v8::Value> Menu::IsEnabledAt(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return v8::Boolean::New(self->model_->IsEnabledAt(args[0]->IntegerValue()));
}
// static
v8::Handle<v8::Value> Menu::IsVisibleAt(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK;
return v8::Boolean::New(self->model_->IsVisibleAt(args[0]->IntegerValue()));
}
// static
void Menu::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope scope;
@ -52,6 +260,23 @@ void Menu::Initialize(v8::Handle<v8::Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Menu"));
NODE_SET_PROTOTYPE_METHOD(t, "insertItem", InsertItem);
NODE_SET_PROTOTYPE_METHOD(t, "insertCheckItem", InsertCheckItem);
NODE_SET_PROTOTYPE_METHOD(t, "insertRadioItem", InsertRadioItem);
NODE_SET_PROTOTYPE_METHOD(t, "insertSeparator", InsertSeparator);
NODE_SET_PROTOTYPE_METHOD(t, "insertSubMenu", InsertSubMenu);
NODE_SET_PROTOTYPE_METHOD(t, "setIcon", SetIcon);
NODE_SET_PROTOTYPE_METHOD(t, "setSublabel", SetSublabel);
NODE_SET_PROTOTYPE_METHOD(t, "clear", Clear);
NODE_SET_PROTOTYPE_METHOD(t, "getIndexOfCommandId", GetIndexOfCommandId);
NODE_SET_PROTOTYPE_METHOD(t, "getItemCount", GetItemCount);
NODE_SET_PROTOTYPE_METHOD(t, "getCommandIdAt", GetCommandIdAt);
NODE_SET_PROTOTYPE_METHOD(t, "getLabelAt", GetLabelAt);
NODE_SET_PROTOTYPE_METHOD(t, "getSublabelAt", GetSublabelAt);
NODE_SET_PROTOTYPE_METHOD(t, "isItemCheckedAt", IsItemCheckedAt);
NODE_SET_PROTOTYPE_METHOD(t, "isEnabledAt", IsEnabledAt);
NODE_SET_PROTOTYPE_METHOD(t, "isVisibleAt", IsVisibleAt);
target->Set(v8::String::NewSymbol("Menu"), t->GetFunction());
}

View file

@ -38,6 +38,26 @@ class Menu : public EventEmitter,
private:
static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> InsertItem(const v8::Arguments &args);
static v8::Handle<v8::Value> InsertCheckItem(const v8::Arguments &args);
static v8::Handle<v8::Value> InsertRadioItem(const v8::Arguments &args);
static v8::Handle<v8::Value> InsertSeparator(const v8::Arguments &args);
static v8::Handle<v8::Value> InsertSubMenu(const v8::Arguments &args);
static v8::Handle<v8::Value> SetIcon(const v8::Arguments &args);
static v8::Handle<v8::Value> SetSublabel(const v8::Arguments &args);
static v8::Handle<v8::Value> Clear(const v8::Arguments &args);
static v8::Handle<v8::Value> GetIndexOfCommandId(const v8::Arguments &args);
static v8::Handle<v8::Value> GetItemCount(const v8::Arguments &args);
static v8::Handle<v8::Value> GetCommandIdAt(const v8::Arguments &args);
static v8::Handle<v8::Value> GetLabelAt(const v8::Arguments &args);
static v8::Handle<v8::Value> GetSublabelAt(const v8::Arguments &args);
static v8::Handle<v8::Value> IsItemCheckedAt(const v8::Arguments &args);
static v8::Handle<v8::Value> IsEnabledAt(const v8::Arguments &args);
static v8::Handle<v8::Value> IsVisibleAt(const v8::Arguments &args);
DISALLOW_COPY_AND_ASSIGN(Menu);
};