Simplify menu api.

This commit is contained in:
Cheng Zhao 2014-04-21 23:40:10 +08:00
parent a13c29f0e6
commit 2efed6f910
8 changed files with 233 additions and 362 deletions

View file

@ -4,16 +4,38 @@
#include "atom/browser/api/atom_api_menu.h" #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/browser/ui/accelerator_util.h"
#include "atom/common/node_includes.h" #include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/v8/native_type_conversions.h" #include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "atom/common/node_includes.h"
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
#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 atom {
@ -46,9 +68,7 @@ v8::Handle<v8::Value> CallDelegate(v8::Handle<v8::Value> default_value,
} // namespace } // namespace
Menu::Menu(v8::Handle<v8::Object> wrapper) Menu::Menu() : model_(new ui::SimpleMenuModel(this)) {
: EventEmitter(wrapper),
model_(new ui::SimpleMenuModel(this)) {
} }
Menu::~Menu() { Menu::~Menu() {
@ -58,7 +78,7 @@ bool Menu::IsCommandIdChecked(int command_id) const {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::False(), return CallDelegate(v8::False(),
const_cast<Menu*>(this)->handle(), const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isCommandIdChecked", "isCommandIdChecked",
command_id)->BooleanValue(); command_id)->BooleanValue();
} }
@ -67,7 +87,7 @@ bool Menu::IsCommandIdEnabled(int command_id) const {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::True(), return CallDelegate(v8::True(),
const_cast<Menu*>(this)->handle(), const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isCommandIdEnabled", "isCommandIdEnabled",
command_id)->BooleanValue(); command_id)->BooleanValue();
} }
@ -76,7 +96,7 @@ bool Menu::IsCommandIdVisible(int command_id) const {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::True(), return CallDelegate(v8::True(),
const_cast<Menu*>(this)->handle(), const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isCommandIdVisible", "isCommandIdVisible",
command_id)->BooleanValue(); command_id)->BooleanValue();
} }
@ -86,11 +106,11 @@ bool Menu::GetAcceleratorForCommandId(int command_id,
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Value> shortcut = CallDelegate(v8::Undefined(), v8::Handle<v8::Value> shortcut = CallDelegate(v8::Undefined(),
handle(), GetWrapper(node_isolate),
"getAcceleratorForCommandId", "getAcceleratorForCommandId",
command_id); command_id);
if (shortcut->IsString()) { if (shortcut->IsString()) {
std::string shortcut_str = FromV8Value(shortcut); std::string shortcut_str = mate::V8ToString(shortcut);
return accelerator_util::StringToAccelerator(shortcut_str, accelerator); return accelerator_util::StringToAccelerator(shortcut_str, accelerator);
} }
@ -101,7 +121,7 @@ bool Menu::IsItemForCommandIdDynamic(int command_id) const {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::False(), return CallDelegate(v8::False(),
const_cast<Menu*>(this)->handle(), const_cast<Menu*>(this)->GetWrapper(node_isolate),
"isItemForCommandIdDynamic", "isItemForCommandIdDynamic",
command_id)->BooleanValue(); command_id)->BooleanValue();
} }
@ -109,254 +129,150 @@ bool Menu::IsItemForCommandIdDynamic(int command_id) const {
string16 Menu::GetLabelForCommandId(int command_id) const { string16 Menu::GetLabelForCommandId(int command_id) const {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
return FromV8Value(CallDelegate(v8::False(), v8::Handle<v8::Value> result = CallDelegate(
const_cast<Menu*>(this)->handle(), v8::False(),
"getLabelForCommandId", const_cast<Menu*>(this)->GetWrapper(node_isolate),
command_id)); "getLabelForCommandId",
command_id);
string16 label;
mate::ConvertFromV8(node_isolate, result, &label);
return label;
} }
string16 Menu::GetSublabelForCommandId(int command_id) const { string16 Menu::GetSublabelForCommandId(int command_id) const {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
return FromV8Value(CallDelegate(v8::False(), v8::Handle<v8::Value> result = CallDelegate(
const_cast<Menu*>(this)->handle(), v8::False(),
"getSubLabelForCommandId", const_cast<Menu*>(this)->GetWrapper(node_isolate),
command_id)); "getSubLabelForCommandId",
command_id);
string16 label;
mate::ConvertFromV8(node_isolate, result, &label);
return label;
} }
void Menu::ExecuteCommand(int command_id, int event_flags) { void Menu::ExecuteCommand(int command_id, int event_flags) {
v8::Locker locker(node_isolate); v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate); v8::HandleScope handle_scope(node_isolate);
CallDelegate(v8::False(), handle(), "executeCommand", command_id); CallDelegate(v8::False(), GetWrapper(node_isolate), "executeCommand",
command_id);
}
void Menu::InsertItemAt(
int index, int command_id, const base::string16& label) {
model_->InsertItemAt(index, command_id, label);
}
void Menu::InsertSeparatorAt(int index) {
model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
}
void Menu::InsertCheckItemAt(int index,
int command_id,
const base::string16& label) {
model_->InsertCheckItemAt(index, command_id, label);
}
void Menu::InsertRadioItemAt(int index,
int command_id,
const base::string16& label,
int group_id) {
model_->InsertRadioItemAt(index, command_id, label, group_id);
}
void Menu::InsertSubMenuAt(int index,
int command_id,
const base::string16& label,
Menu* menu) {
model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
}
void Menu::SetSublabel(int index, const base::string16& sublabel) {
model_->SetSublabel(index, sublabel);
}
void Menu::Clear() {
model_->Clear();
}
int Menu::GetIndexOfCommandId(int command_id) {
return model_->GetIndexOfCommandId(command_id);
}
int Menu::GetItemCount() const {
return model_->GetItemCount();
}
int Menu::GetCommandIdAt(int index) const {
return model_->GetCommandIdAt(index);
}
base::string16 Menu::GetLabelAt(int index) const {
return model_->GetLabelAt(index);
}
base::string16 Menu::GetSublabelAt(int index) const {
return model_->GetSublabelAt(index);
}
bool Menu::IsItemCheckedAt(int index) const {
return model_->IsItemCheckedAt(index);
}
bool Menu::IsEnabledAt(int index) const {
return model_->IsEnabledAt(index);
}
bool Menu::IsVisibleAt(int index) const {
return model_->IsVisibleAt(index);
} }
// static // static
void Menu::New(const v8::FunctionCallbackInfo<v8::Value>& args) { void Menu::BuildPrototype(v8::Isolate* isolate,
if (!args.IsConstructCall()) v8::Handle<v8::ObjectTemplate> prototype) {
return node::ThrowError("Require constructor call"); mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("insertItem", &Menu::InsertItemAt)
Menu::Create(args.This()); .SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
} .SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
.SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
// static .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
void Menu::InsertItem(const v8::FunctionCallbackInfo<v8::Value>& args) { .SetMethod("setSublabel", &Menu::SetSublabel)
UNWRAP_MEMNU_AND_CHECK; .SetMethod("clear", &Menu::Clear)
.SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
int index, command_id; .SetMethod("getItemCount", &Menu::GetItemCount)
string16 label; .SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
if (!FromV8Arguments(args, &index, &command_id, &label)) .SetMethod("getLabelAt", &Menu::GetLabelAt)
return node::ThrowTypeError("Bad argument"); .SetMethod("getSublabelAt", &Menu::GetSublabelAt)
.SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
if (index < 0) .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
self->model_->AddItem(command_id, label); .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
else
self->model_->InsertItemAt(index, command_id, label);
}
// 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);
}
// 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);
}
// 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);
}
// 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());
}
// 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.
}
// 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();
}
// 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());
}
// 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));
}
// 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)));
}
// 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)));
}
// 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));
}
// 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));
}
// 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));
}
// 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);
}
// 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);
NODE_SET_PROTOTYPE_METHOD(t, "popup", Popup);
#if defined(OS_WIN) || defined(TOOLKIT_GTK) #if defined(OS_WIN) || defined(TOOLKIT_GTK)
NODE_SET_PROTOTYPE_METHOD(t, "attachToWindow", AttachToWindow); .SetMethod("attachToWindow", &Menu::AttachToWindow)
#endif
target->Set(v8::String::NewSymbol("Menu"), t->GetFunction());
#if defined(OS_MACOSX)
NODE_SET_METHOD(target, "setApplicationMenu", SetApplicationMenu);
NODE_SET_METHOD(
target, "sendActionToFirstResponder", SendActionToFirstResponder);
#endif #endif
.SetMethod("popup", &Menu::Popup);
} }
} // namespace api } // namespace api
} // namespace atom } // namespace atom
NODE_MODULE(atom_browser_menu, atom::api::Menu::Initialize)
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)
dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
dict.SetMethod("sendActionToFirstResponder",
&Menu::SendActionToFirstResponder);
#endif
}
} // namespace
NODE_MODULE(atom_browser_menu, Initialize)

View file

@ -5,9 +5,11 @@
#ifndef ATOM_BROWSER_API_ATOM_API_MENU_H_ #ifndef ATOM_BROWSER_API_ATOM_API_MENU_H_
#define ATOM_BROWSER_API_ATOM_API_MENU_H_ #define ATOM_BROWSER_API_ATOM_API_MENU_H_
#include <string>
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "atom/common/api/atom_api_event_emitter.h"
#include "ui/base/models/simple_menu_model.h" #include "ui/base/models/simple_menu_model.h"
#include "native_mate/wrappable.h"
namespace atom { namespace atom {
@ -15,17 +17,27 @@ class NativeWindow;
namespace api { namespace api {
class Menu : public EventEmitter, class MenuMac;
class Menu : public mate::Wrappable,
public ui::SimpleMenuModel::Delegate { public ui::SimpleMenuModel::Delegate {
public: 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: protected:
explicit Menu(v8::Handle<v8::Object> wrapper); Menu();
virtual ~Menu();
// ui::SimpleMenuModel::Delegate implementations: // ui::SimpleMenuModel::Delegate implementations:
virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
@ -44,38 +56,32 @@ class Menu : public EventEmitter,
scoped_ptr<ui::SimpleMenuModel> model_; scoped_ptr<ui::SimpleMenuModel> model_;
private: private:
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); void InsertItemAt(int index, int command_id, const base::string16& label);
void InsertSeparatorAt(int index);
static void InsertItem(const v8::FunctionCallbackInfo<v8::Value>& args); void InsertCheckItemAt(int index,
static void InsertCheckItem(const v8::FunctionCallbackInfo<v8::Value>& args); int command_id,
static void InsertRadioItem(const v8::FunctionCallbackInfo<v8::Value>& args); const base::string16& label);
static void InsertSeparator(const v8::FunctionCallbackInfo<v8::Value>& args); void InsertRadioItemAt(int index,
static void InsertSubMenu(const v8::FunctionCallbackInfo<v8::Value>& args); int command_id,
const base::string16& label,
static void SetIcon(const v8::FunctionCallbackInfo<v8::Value>& args); int group_id);
static void SetSublabel(const v8::FunctionCallbackInfo<v8::Value>& args); void InsertSubMenuAt(int index,
int command_id,
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args); const base::string16& label,
Menu* menu);
static void GetIndexOfCommandId( void SetSublabel(int index, const base::string16& sublabel);
const v8::FunctionCallbackInfo<v8::Value>& args); void Clear();
static void GetItemCount(const v8::FunctionCallbackInfo<v8::Value>& args); int GetIndexOfCommandId(int command_id);
static void GetCommandIdAt(const v8::FunctionCallbackInfo<v8::Value>& args); int GetItemCount() const;
static void GetLabelAt(const v8::FunctionCallbackInfo<v8::Value>& args); int GetCommandIdAt(int index) const;
static void GetSublabelAt(const v8::FunctionCallbackInfo<v8::Value>& args); base::string16 GetLabelAt(int index) const;
static void IsItemCheckedAt(const v8::FunctionCallbackInfo<v8::Value>& args); base::string16 GetSublabelAt(int index) const;
static void IsEnabledAt(const v8::FunctionCallbackInfo<v8::Value>& args); bool IsItemCheckedAt(int index) const;
static void IsVisibleAt(const v8::FunctionCallbackInfo<v8::Value>& args); bool IsEnabledAt(int index) const;
bool IsVisibleAt(int index) const;
static void Popup(const v8::FunctionCallbackInfo<v8::Value>& args);
#if defined(OS_WIN) || defined(TOOLKIT_GTK) #if defined(OS_WIN) || defined(TOOLKIT_GTK)
static void AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args); void AttachToWindow(NativeWindow* window);
#elif defined(OS_MACOSX)
static void SetApplicationMenu(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SendActionToFirstResponder(
const v8::FunctionCallbackInfo<v8::Value>& args);
#endif #endif
DISALLOW_COPY_AND_ASSIGN(Menu); DISALLOW_COPY_AND_ASSIGN(Menu);

View file

@ -5,7 +5,6 @@
#include "atom/browser/api/atom_api_menu_gtk.h" #include "atom/browser/api/atom_api_menu_gtk.h"
#include "atom/browser/native_window_gtk.h" #include "atom/browser/native_window_gtk.h"
#include "atom/common/v8/native_type_conversions.h"
#include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/render_widget_host_view.h"
#include "ui/gfx/point.h" #include "ui/gfx/point.h"
#include "ui/gfx/screen.h" #include "ui/gfx/screen.h"
@ -16,11 +15,7 @@ namespace atom {
namespace api { namespace api {
MenuGtk::MenuGtk(v8::Handle<v8::Object> wrapper) MenuGtk::MenuGtk() {
: Menu(wrapper) {
}
MenuGtk::~MenuGtk() {
} }
void MenuGtk::Popup(NativeWindow* native_window) { void MenuGtk::Popup(NativeWindow* native_window) {
@ -41,22 +36,16 @@ void MenuGtk::Popup(NativeWindow* native_window) {
menu_gtk_->PopupAsContext(point, triggering_event_time); menu_gtk_->PopupAsContext(point, triggering_event_time);
} }
// static void Menu::AttachToWindow(NativeWindow* window) {
void Menu::AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args) { if (window == NULL)
Menu* self = ObjectWrap::Unwrap<Menu>(args.This()); return node::ThrowTypeError("Window is dead");
if (self == NULL)
return node::ThrowError("Menu is already destroyed");
NativeWindow* native_window; static_cast<NativeWindowGtk*>(native_window)->SetMenu(model_.get());
if (!FromV8Arguments(args, &native_window))
return node::ThrowTypeError("Bad argument");
static_cast<NativeWindowGtk*>(native_window)->SetMenu(self->model_.get());
} }
// static // static
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) { mate::Wrappable* Menu::Create() {
return new MenuGtk(wrapper); return new MenuGtk();
} }
} // namespace api } // namespace api

View file

@ -14,14 +14,12 @@ namespace api {
class MenuGtk : public Menu, class MenuGtk : public Menu,
public ::MenuGtk::Delegate { public ::MenuGtk::Delegate {
public:
explicit MenuGtk(v8::Handle<v8::Object> wrapper);
virtual ~MenuGtk();
protected: protected:
virtual void Popup(NativeWindow* window) OVERRIDE; virtual void Popup(NativeWindow* window) OVERRIDE;
private: private:
MenuGtk();
scoped_ptr<::MenuGtk> menu_gtk_; scoped_ptr<::MenuGtk> menu_gtk_;
DISALLOW_COPY_AND_ASSIGN(MenuGtk); DISALLOW_COPY_AND_ASSIGN(MenuGtk);

View file

@ -16,11 +16,9 @@ namespace atom {
namespace api { namespace api {
class MenuMac : public Menu { class MenuMac : public Menu {
public:
explicit MenuMac(v8::Handle<v8::Object> wrapper);
virtual ~MenuMac();
protected: protected:
MenuMac();
virtual void Popup(NativeWindow* window) OVERRIDE; virtual void Popup(NativeWindow* window) OVERRIDE;
base::scoped_nsobject<AtomMenuController> menu_controller_; base::scoped_nsobject<AtomMenuController> menu_controller_;
@ -28,7 +26,6 @@ class MenuMac : public Menu {
private: private:
friend class Menu; friend class Menu;
// Fake sending an action from the application menu.
static void SendActionToFirstResponder(const std::string& action); static void SendActionToFirstResponder(const std::string& action);
DISALLOW_COPY_AND_ASSIGN(MenuMac); DISALLOW_COPY_AND_ASSIGN(MenuMac);

View file

@ -16,11 +16,7 @@ namespace atom {
namespace api { namespace api {
MenuMac::MenuMac(v8::Handle<v8::Object> wrapper) MenuMac::MenuMac() {
: Menu(wrapper) {
}
MenuMac::~MenuMac() {
} }
void MenuMac::Popup(NativeWindow* native_window) { void MenuMac::Popup(NativeWindow* native_window) {
@ -51,22 +47,8 @@ void MenuMac::Popup(NativeWindow* native_window) {
} }
// static // static
void MenuMac::SendActionToFirstResponder(const std::string& action) { void Menu::SetApplicationMenu(Menu* base_menu) {
SEL selector = NSSelectorFromString(base::SysUTF8ToNSString(action)); MenuMac* menu = static_cast<MenuMac*>(base_menu);
[[NSApplication sharedApplication] sendAction:selector
to:nil
from:[NSApp mainMenu]];
}
// static
void Menu::SetApplicationMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (!args[0]->IsObject())
return node::ThrowTypeError("Bad argument");
MenuMac* menu = ObjectWrap::Unwrap<MenuMac>(args[0]->ToObject());
if (!menu)
return node::ThrowError("Menu is destroyed");
base::scoped_nsobject<AtomMenuController> menu_controller( base::scoped_nsobject<AtomMenuController> menu_controller(
[[AtomMenuController alloc] initWithModel:menu->model_.get()]); [[AtomMenuController alloc] initWithModel:menu->model_.get()]);
[NSApp setMainMenu:[menu_controller menu]]; [NSApp setMainMenu:[menu_controller menu]];
@ -76,18 +58,14 @@ void Menu::SetApplicationMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
} }
// static // static
void Menu::SendActionToFirstResponder( void Menu::SendActionToFirstResponder(const std::string& action) {
const v8::FunctionCallbackInfo<v8::Value>& args) { SEL selector = NSSelectorFromString(base::SysUTF8ToNSString(action));
std::string action; [NSApp sendAction:selector to:nil from:[NSApp mainMenu]];
if (!FromV8Arguments(args, &action))
return node::ThrowTypeError("Bad argument");
MenuMac::SendActionToFirstResponder(action);
} }
// static // static
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) { mate::Wrappable* Menu::Create() {
return new MenuMac(wrapper); return new MenuMac();
} }
} // namespace api } // namespace api

View file

@ -6,7 +6,6 @@
#include "atom/browser/native_window_win.h" #include "atom/browser/native_window_win.h"
#include "atom/browser/ui/win/menu_2.h" #include "atom/browser/ui/win/menu_2.h"
#include "atom/common/v8/native_type_conversions.h"
#include "ui/gfx/point.h" #include "ui/gfx/point.h"
#include "ui/gfx/screen.h" #include "ui/gfx/screen.h"
@ -16,11 +15,7 @@ namespace atom {
namespace api { namespace api {
MenuWin::MenuWin(v8::Handle<v8::Object> wrapper) MenuWin::MenuWin() {
: Menu(wrapper) {
}
MenuWin::~MenuWin() {
} }
void MenuWin::Popup(NativeWindow* native_window) { void MenuWin::Popup(NativeWindow* native_window) {
@ -29,22 +24,16 @@ void MenuWin::Popup(NativeWindow* native_window) {
menu_->RunContextMenuAt(cursor); menu_->RunContextMenuAt(cursor);
} }
// static void Menu::AttachToWindow(NativeWindow* window) {
void Menu::AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args) { if (window == NULL)
Menu* self = ObjectWrap::Unwrap<Menu>(args.This()); return node::ThrowTypeError("Window is dead");
if (self == NULL)
return node::ThrowError("Menu is already destroyed");
NativeWindow* native_window; static_cast<NativeWindowWin*>(native_window)->SetMenu(model_.get());
if (!FromV8Arguments(args, &native_window))
return node::ThrowTypeError("Bad argument");
static_cast<NativeWindowWin*>(native_window)->SetMenu(self->model_.get());
} }
// static // static
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) { mate::Wrappable* Menu::Create() {
return new MenuWin(wrapper); return new MenuWin();
} }
} // namespace api } // namespace api

View file

@ -14,14 +14,12 @@ class Menu2;
namespace api { namespace api {
class MenuWin : public Menu { class MenuWin : public Menu {
public:
explicit MenuWin(v8::Handle<v8::Object> wrapper);
virtual ~MenuWin();
protected: protected:
virtual void Popup(NativeWindow* window) OVERRIDE; virtual void Popup(NativeWindow* window) OVERRIDE;
private: private:
MenuWin();
scoped_ptr<atom::Menu2> menu_; scoped_ptr<atom::Menu2> menu_;
DISALLOW_COPY_AND_ASSIGN(MenuWin); DISALLOW_COPY_AND_ASSIGN(MenuWin);