1 // Copyright (c) 2013 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
5 #include "atom/browser/api/atom_api_menu.h"
7 #include "atom/browser/native_window.h"
8 #include "atom/common/native_mate_converters/accelerator_converter.h"
9 #include "atom/common/native_mate_converters/callback.h"
10 #include "atom/common/native_mate_converters/image_converter.h"
11 #include "atom/common/native_mate_converters/string16_converter.h"
12 #include "native_mate/constructor.h"
13 #include "native_mate/dictionary.h"
14 #include "native_mate/object_template_builder.h"
15 #include "tizen/common/env_variables.h"
17 #include "atom/common/node_includes.h"
23 Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
24 : model_(new AtomMenuModel(this)),
26 InitWith(isolate, wrapper);
32 void Menu::AfterInit(v8::Isolate* isolate) {
33 mate::Dictionary wrappable(isolate, GetWrapper());
34 mate::Dictionary delegate;
35 if (!wrappable.Get("delegate", &delegate))
38 delegate.Get("isCommandIdChecked", &is_checked_);
39 delegate.Get("isCommandIdEnabled", &is_enabled_);
40 delegate.Get("isCommandIdVisible", &is_visible_);
41 delegate.Get("getAcceleratorForCommandId", &get_accelerator_);
42 delegate.Get("executeCommand", &execute_command_);
43 delegate.Get("menuWillShow", &menu_will_show_);
46 bool Menu::IsCommandIdChecked(int command_id) const {
47 return is_checked_.Run(command_id);
50 bool Menu::IsCommandIdEnabled(int command_id) const {
51 return is_enabled_.Run(command_id);
54 bool Menu::IsCommandIdVisible(int command_id) const {
55 return is_visible_.Run(command_id);
58 bool Menu::GetAcceleratorForCommandIdWithParams(
60 bool use_default_accelerator,
61 ui::Accelerator* accelerator) const {
62 if (!::tizen::is_single_process)
63 v8::Locker locker(isolate());
64 v8::HandleScope handle_scope(isolate());
65 v8::Local<v8::Value> val = get_accelerator_.Run(
66 command_id, use_default_accelerator);
67 return mate::ConvertFromV8(isolate(), val, accelerator);
70 void Menu::ExecuteCommand(int command_id, int flags) {
72 mate::internal::CreateEventFromFlags(isolate(), flags),
76 void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
77 menu_will_show_.Run();
80 void Menu::InsertItemAt(
81 int index, int command_id, const base::string16& label) {
82 model_->InsertItemAt(index, command_id, label);
85 void Menu::InsertSeparatorAt(int index) {
86 model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
89 void Menu::InsertCheckItemAt(int index,
91 const base::string16& label) {
92 model_->InsertCheckItemAt(index, command_id, label);
95 void Menu::InsertRadioItemAt(int index,
97 const base::string16& label,
99 model_->InsertRadioItemAt(index, command_id, label, group_id);
102 void Menu::InsertSubMenuAt(int index,
104 const base::string16& label,
106 menu->parent_ = this;
107 model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
110 void Menu::SetIcon(int index, const gfx::Image& image) {
111 model_->SetIcon(index, image);
114 void Menu::SetSublabel(int index, const base::string16& sublabel) {
115 model_->SetSublabel(index, sublabel);
118 void Menu::SetRole(int index, const base::string16& role) {
119 model_->SetRole(index, role);
126 int Menu::GetIndexOfCommandId(int command_id) {
127 return model_->GetIndexOfCommandId(command_id);
130 int Menu::GetItemCount() const {
131 return model_->GetItemCount();
134 int Menu::GetCommandIdAt(int index) const {
135 return model_->GetCommandIdAt(index);
138 base::string16 Menu::GetLabelAt(int index) const {
139 return model_->GetLabelAt(index);
142 base::string16 Menu::GetSublabelAt(int index) const {
143 return model_->GetSublabelAt(index);
146 bool Menu::IsItemCheckedAt(int index) const {
147 return model_->IsItemCheckedAt(index);
150 bool Menu::IsEnabledAt(int index) const {
151 return model_->IsEnabledAt(index);
154 bool Menu::IsVisibleAt(int index) const {
155 return model_->IsVisibleAt(index);
159 void Menu::BuildPrototype(v8::Isolate* isolate,
160 v8::Local<v8::FunctionTemplate> prototype) {
161 prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
162 mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
164 .SetMethod("insertItem", &Menu::InsertItemAt)
165 .SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
166 .SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
167 .SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
168 .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
169 .SetMethod("setIcon", &Menu::SetIcon)
170 .SetMethod("setSublabel", &Menu::SetSublabel)
171 .SetMethod("setRole", &Menu::SetRole)
172 .SetMethod("clear", &Menu::Clear)
173 .SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
174 .SetMethod("getItemCount", &Menu::GetItemCount)
175 .SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
176 .SetMethod("getLabelAt", &Menu::GetLabelAt)
177 .SetMethod("getSublabelAt", &Menu::GetSublabelAt)
178 .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
179 .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
180 .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
181 .SetMethod("popupAt", &Menu::PopupAt)
182 .SetMethod("closePopupAt", &Menu::ClosePopupAt);
192 using atom::api::Menu;
194 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
195 v8::Local<v8::Context> context, void* priv) {
196 v8::Isolate* isolate = context->GetIsolate();
197 Menu::SetConstructor(isolate, base::Bind(&Menu::New));
199 mate::Dictionary dict(isolate, exports);
200 dict.Set("Menu", Menu::GetConstructor(isolate)->GetFunction());
201 #if defined(OS_MACOSX)
202 dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
203 dict.SetMethod("sendActionToFirstResponder",
204 &Menu::SendActionToFirstResponder);
210 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_menu, Initialize)