#include "atom/browser/api/atom_api_menu.h"
-#include <string>
-
+#include "atom/browser/api/atom_api_window.h"
#include "atom/browser/ui/accelerator_util.h"
+#include "atom/common/native_mate_converters/string16_converter.h"
+#include "native_mate/constructor.h"
+#include "native_mate/dictionary.h"
+#include "native_mate/object_template_builder.h"
+
#include "atom/common/node_includes.h"
-#include "atom/common/v8/native_type_conversions.h"
-#define UNWRAP_MEMNU_AND_CHECK \
- Menu* self = ObjectWrap::Unwrap<Menu>(args.This()); \
- if (self == NULL) \
- return node::ThrowError("Menu is already destroyed")
+namespace mate {
+
+template<>
+struct Converter<atom::NativeWindow*> {
+ static bool FromV8(v8::Isolate* isolate,
+ v8::Handle<v8::Value> val,
+ atom::NativeWindow** out) {
+ using atom::api::Window;
+ if (val->IsNull()) {
+ *out = NULL;
+ return true; // NULL is a valid value for NativeWindow*.
+ } else if (val->IsObject()) {
+ Window* window = Window::Unwrap<Window>(val->ToObject());
+ *out = window->window();
+ return true;
+ } else {
+ return false;
+ }
+ }
+};
+
+} // namespace mate
+
namespace atom {
} // namespace
-Menu::Menu(v8::Handle<v8::Object> wrapper)
- : EventEmitter(wrapper),
- model_(new ui::SimpleMenuModel(this)) {
+Menu::Menu() : model_(new ui::SimpleMenuModel(this)) {
}
Menu::~Menu() {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::False(),
- const_cast<Menu*>(this)->handle(),
+ const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isCommandIdChecked",
command_id)->BooleanValue();
}
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::True(),
- const_cast<Menu*>(this)->handle(),
+ const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isCommandIdEnabled",
command_id)->BooleanValue();
}
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::True(),
- const_cast<Menu*>(this)->handle(),
+ const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isCommandIdVisible",
command_id)->BooleanValue();
}
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Value> shortcut = CallDelegate(v8::Undefined(),
- handle(),
+ GetWrapper(node_isolate),
"getAcceleratorForCommandId",
command_id);
if (shortcut->IsString()) {
- std::string shortcut_str = FromV8Value(shortcut);
+ std::string shortcut_str = mate::V8ToString(shortcut);
return accelerator_util::StringToAccelerator(shortcut_str, accelerator);
}
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::False(),
- const_cast<Menu*>(this)->handle(),
+ const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isItemForCommandIdDynamic",
command_id)->BooleanValue();
}
string16 Menu::GetLabelForCommandId(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
- return FromV8Value(CallDelegate(v8::False(),
- const_cast<Menu*>(this)->handle(),
- "getLabelForCommandId",
- command_id));
+ v8::Handle<v8::Value> result = CallDelegate(
+ v8::False(),
+ const_cast<Menu*>(this)->GetWrapper(node_isolate),
+ "getLabelForCommandId",
+ command_id);
+ string16 label;
+ mate::ConvertFromV8(node_isolate, result, &label);
+ return label;
}
string16 Menu::GetSublabelForCommandId(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
- return FromV8Value(CallDelegate(v8::False(),
- const_cast<Menu*>(this)->handle(),
- "getSubLabelForCommandId",
- command_id));
+ v8::Handle<v8::Value> result = CallDelegate(
+ v8::False(),
+ const_cast<Menu*>(this)->GetWrapper(node_isolate),
+ "getSubLabelForCommandId",
+ command_id);
+ string16 label;
+ mate::ConvertFromV8(node_isolate, result, &label);
+ return label;
}
void Menu::ExecuteCommand(int command_id, int event_flags) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
- CallDelegate(v8::False(), handle(), "executeCommand", command_id);
+ CallDelegate(v8::False(), GetWrapper(node_isolate), "executeCommand",
+ command_id);
}
-// static
-void Menu::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
- if (!args.IsConstructCall())
- return node::ThrowError("Require constructor call");
-
- Menu::Create(args.This());
+void Menu::InsertItemAt(
+ int index, int command_id, const base::string16& label) {
+ model_->InsertItemAt(index, command_id, label);
}
-// static
-void Menu::InsertItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index, command_id;
- string16 label;
- if (!FromV8Arguments(args, &index, &command_id, &label))
- return node::ThrowTypeError("Bad argument");
-
- if (index < 0)
- self->model_->AddItem(command_id, label);
- else
- self->model_->InsertItemAt(index, command_id, label);
+void Menu::InsertSeparatorAt(int index) {
+ model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
}
-// static
-void Menu::InsertCheckItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index, command_id;
- string16 label;
- if (!FromV8Arguments(args, &index, &command_id, &label))
- return node::ThrowTypeError("Bad argument");
-
- if (index < 0)
- self->model_->AddCheckItem(command_id, label);
- else
- self->model_->InsertCheckItemAt(index, command_id, label);
+void Menu::InsertCheckItemAt(int index,
+ int command_id,
+ const base::string16& label) {
+ model_->InsertCheckItemAt(index, command_id, label);
}
-// static
-void Menu::InsertRadioItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index, command_id, group_id;
- string16 label;
- if (!FromV8Arguments(args, &index, &command_id, &label, &group_id))
- return node::ThrowTypeError("Bad argument");
-
- if (index < 0)
- self->model_->AddRadioItem(command_id, label, group_id);
- else
- self->model_->InsertRadioItemAt(index, command_id, label, group_id);
+void Menu::InsertRadioItemAt(int index,
+ int command_id,
+ const base::string16& label,
+ int group_id) {
+ model_->InsertRadioItemAt(index, command_id, label, group_id);
}
-// static
-void Menu::InsertSeparator(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index;
- if (!FromV8Arguments(args, &index))
- return node::ThrowTypeError("Bad argument");
-
- if (index < 0)
- self->model_->AddSeparator(ui::NORMAL_SEPARATOR);
- else
- self->model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
+void Menu::InsertSubMenuAt(int index,
+ int command_id,
+ const base::string16& label,
+ Menu* menu) {
+ model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
}
-// static
-void Menu::InsertSubMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index, command_id;
- string16 label;
- if (!FromV8Arguments(args, &index, &command_id, &label))
- return node::ThrowTypeError("Bad argument");
-
- Menu* submenu = ObjectWrap::Unwrap<Menu>(args[3]->ToObject());
- if (!submenu)
- return node::ThrowTypeError("The submenu is already destroyed");
-
- if (index < 0)
- self->model_->AddSubMenu(command_id, label, submenu->model_.get());
- else
- self->model_->InsertSubMenuAt(
- index, command_id, label, submenu->model_.get());
+void Menu::SetSublabel(int index, const base::string16& sublabel) {
+ model_->SetSublabel(index, sublabel);
}
-// static
-void Menu::SetIcon(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index;
- base::FilePath path;
- if (!FromV8Arguments(args, &index, &path))
- return node::ThrowTypeError("Bad argument");
-
- // FIXME use webkit_glue's image decoder here.
+void Menu::Clear() {
+ model_->Clear();
}
-// static
-void Menu::SetSublabel(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- int index;
- string16 label;
- if (!FromV8Arguments(args, &index, &label))
- return node::ThrowTypeError("Bad argument");
-
- self->model_->SetSublabel(index, label);
-}
-
-// static
-void Menu::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- self->model_->Clear();
+int Menu::GetIndexOfCommandId(int command_id) {
+ return model_->GetIndexOfCommandId(command_id);
}
-// static
-void Menu::GetIndexOfCommandId(
- const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(self->model_->GetIndexOfCommandId(index));
-}
-
-// static
-void Menu::GetItemCount(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- args.GetReturnValue().Set(self->model_->GetItemCount());
+int Menu::GetItemCount() const {
+ return model_->GetItemCount();
}
-// static
-void Menu::GetCommandIdAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(self->model_->GetCommandIdAt(index));
+int Menu::GetCommandIdAt(int index) const {
+ return model_->GetCommandIdAt(index);
}
-// static
-void Menu::GetLabelAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(ToV8Value(self->model_->GetLabelAt(index)));
+base::string16 Menu::GetLabelAt(int index) const {
+ return model_->GetLabelAt(index);
}
-// static
-void Menu::GetSublabelAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(ToV8Value(self->model_->GetSublabelAt(index)));
+base::string16 Menu::GetSublabelAt(int index) const {
+ return model_->GetSublabelAt(index);
}
-// static
-void Menu::IsItemCheckedAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(self->model_->IsItemCheckedAt(index));
+bool Menu::IsItemCheckedAt(int index) const {
+ return model_->IsItemCheckedAt(index);
}
-// static
-void Menu::IsEnabledAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(self->model_->IsEnabledAt(index));
+bool Menu::IsEnabledAt(int index) const {
+ return model_->IsEnabledAt(index);
}
-// static
-void Menu::IsVisibleAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
- int index = FromV8Value(args[0]);
- args.GetReturnValue().Set(self->model_->IsVisibleAt(index));
+bool Menu::IsVisibleAt(int index) const {
+ return model_->IsVisibleAt(index);
}
// static
-void Menu::Popup(const v8::FunctionCallbackInfo<v8::Value>& args) {
- UNWRAP_MEMNU_AND_CHECK;
-
- atom::NativeWindow* window;
- if (!FromV8Arguments(args, &window))
- return node::ThrowTypeError("Bad argument");
-
- self->Popup(window);
+void Menu::BuildPrototype(v8::Isolate* isolate,
+ v8::Handle<v8::ObjectTemplate> prototype) {
+ mate::ObjectTemplateBuilder(isolate, prototype)
+ .SetMethod("insertItem", &Menu::InsertItemAt)
+ .SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
+ .SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
+ .SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
+ .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
+ .SetMethod("setSublabel", &Menu::SetSublabel)
+ .SetMethod("clear", &Menu::Clear)
+ .SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
+ .SetMethod("getItemCount", &Menu::GetItemCount)
+ .SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
+ .SetMethod("getLabelAt", &Menu::GetLabelAt)
+ .SetMethod("getSublabelAt", &Menu::GetSublabelAt)
+ .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
+ .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
+ .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
+#if defined(OS_WIN) || defined(TOOLKIT_GTK)
+ .SetMethod("attachToWindow", &Menu::AttachToWindow)
+#endif
+ .SetMethod("popup", &Menu::Popup);
}
-// static
-void Menu::Initialize(v8::Handle<v8::Object> target) {
- v8::Local<v8::FunctionTemplate> t(v8::FunctionTemplate::New(Menu::New));
- 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);
+} // namespace api
- NODE_SET_PROTOTYPE_METHOD(t, "popup", Popup);
+} // namespace atom
-#if defined(OS_WIN) || defined(TOOLKIT_GTK)
- NODE_SET_PROTOTYPE_METHOD(t, "attachToWindow", AttachToWindow);
-#endif
- target->Set(v8::String::NewSymbol("Menu"), t->GetFunction());
+namespace {
+void Initialize(v8::Handle<v8::Object> exports) {
+ using atom::api::Menu;
+ v8::Local<v8::Function> constructor = mate::CreateConstructor<Menu>(
+ node_isolate, "Menu", base::Bind(&Menu::Create));
+ mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
+ dict.Set("Menu", static_cast<v8::Handle<v8::Value>>(constructor));
#if defined(OS_MACOSX)
- NODE_SET_METHOD(target, "setApplicationMenu", SetApplicationMenu);
- NODE_SET_METHOD(
- target, "sendActionToFirstResponder", SendActionToFirstResponder);
+ dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
+ dict.SetMethod("sendActionToFirstResponder",
+ &Menu::SendActionToFirstResponder);
#endif
}
-} // namespace api
-
-} // namespace atom
+} // namespace
-NODE_MODULE(atom_browser_menu, atom::api::Menu::Initialize)
+NODE_MODULE(atom_browser_menu, Initialize)
#ifndef ATOM_BROWSER_API_ATOM_API_MENU_H_
#define ATOM_BROWSER_API_ATOM_API_MENU_H_
+#include <string>
+
#include "base/memory/scoped_ptr.h"
-#include "atom/common/api/atom_api_event_emitter.h"
#include "ui/base/models/simple_menu_model.h"
+#include "native_mate/wrappable.h"
namespace atom {
namespace api {
-class Menu : public EventEmitter,
+class MenuMac;
+
+class Menu : public mate::Wrappable,
public ui::SimpleMenuModel::Delegate {
public:
- virtual ~Menu();
+ static mate::Wrappable* Create();
- static Menu* Create(v8::Handle<v8::Object> wrapper);
+ static void BuildPrototype(v8::Isolate* isolate,
+ v8::Handle<v8::ObjectTemplate> prototype);
- static void Initialize(v8::Handle<v8::Object> target);
+#if defined(OS_MACOSX)
+ // Set the global menubar.
+ static void SetApplicationMenu(Menu* menu);
+
+ // Fake sending an action from the application menu.
+ static void SendActionToFirstResponder(const std::string& action);
+#endif
protected:
- explicit Menu(v8::Handle<v8::Object> wrapper);
+ Menu();
+ virtual ~Menu();
// ui::SimpleMenuModel::Delegate implementations:
virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
scoped_ptr<ui::SimpleMenuModel> model_;
private:
- static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
-
- static void InsertItem(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void InsertCheckItem(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void InsertRadioItem(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void InsertSeparator(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void InsertSubMenu(const v8::FunctionCallbackInfo<v8::Value>& args);
-
- static void SetIcon(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void SetSublabel(const v8::FunctionCallbackInfo<v8::Value>& args);
-
- static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
-
- static void GetIndexOfCommandId(
- const v8::FunctionCallbackInfo<v8::Value>& args);
- static void GetItemCount(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void GetCommandIdAt(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void GetLabelAt(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void GetSublabelAt(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void IsItemCheckedAt(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void IsEnabledAt(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void IsVisibleAt(const v8::FunctionCallbackInfo<v8::Value>& args);
-
- static void Popup(const v8::FunctionCallbackInfo<v8::Value>& args);
+ void InsertItemAt(int index, int command_id, const base::string16& label);
+ void InsertSeparatorAt(int index);
+ void InsertCheckItemAt(int index,
+ int command_id,
+ const base::string16& label);
+ void InsertRadioItemAt(int index,
+ int command_id,
+ const base::string16& label,
+ int group_id);
+ void InsertSubMenuAt(int index,
+ int command_id,
+ const base::string16& label,
+ Menu* menu);
+ void SetSublabel(int index, const base::string16& sublabel);
+ void Clear();
+ int GetIndexOfCommandId(int command_id);
+ int GetItemCount() const;
+ int GetCommandIdAt(int index) const;
+ base::string16 GetLabelAt(int index) const;
+ base::string16 GetSublabelAt(int index) const;
+ bool IsItemCheckedAt(int index) const;
+ bool IsEnabledAt(int index) const;
+ bool IsVisibleAt(int index) const;
#if defined(OS_WIN) || defined(TOOLKIT_GTK)
- static void AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args);
-#elif defined(OS_MACOSX)
- static void SetApplicationMenu(
- const v8::FunctionCallbackInfo<v8::Value>& args);
- static void SendActionToFirstResponder(
- const v8::FunctionCallbackInfo<v8::Value>& args);
+ void AttachToWindow(NativeWindow* window);
#endif
DISALLOW_COPY_AND_ASSIGN(Menu);