2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-06 12:27:09 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/api/atom_api_menu.h"
|
2013-05-06 12:27:09 +00:00
|
|
|
|
2014-07-04 08:54:10 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
2014-08-04 16:00:39 +00:00
|
|
|
#include "atom/common/native_mate_converters/accelerator_converter.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2015-02-13 04:11:50 +00:00
|
|
|
#include "atom/common/native_mate_converters/image_converter.h"
|
2014-04-21 15:40:10 +00:00
|
|
|
#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"
|
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-05-14 11:26:16 +00:00
|
|
|
|
2013-05-06 12:27:09 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2016-08-02 06:15:40 +00:00
|
|
|
Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
|
2015-08-10 04:39:05 +00:00
|
|
|
: model_(new AtomMenuModel(this)),
|
2016-07-10 09:52:28 +00:00
|
|
|
parent_(nullptr) {
|
2016-08-02 06:15:40 +00:00
|
|
|
InitWith(isolate, wrapper);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Menu::~Menu() {
|
|
|
|
}
|
|
|
|
|
2015-02-13 03:47:22 +00:00
|
|
|
void Menu::AfterInit(v8::Isolate* isolate) {
|
2016-04-25 01:19:25 +00:00
|
|
|
mate::Dictionary wrappable(isolate, GetWrapper());
|
2015-02-13 03:47:22 +00:00
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
2013-05-06 12:27:09 +00:00
|
|
|
bool Menu::IsCommandIdChecked(int command_id) const {
|
2015-02-13 03:47:22 +00:00
|
|
|
return is_checked_.Run(command_id);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Menu::IsCommandIdEnabled(int command_id) const {
|
2015-02-13 03:47:22 +00:00
|
|
|
return is_enabled_.Run(command_id);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 08:50:56 +00:00
|
|
|
bool Menu::IsCommandIdVisible(int command_id) const {
|
2015-02-13 03:47:22 +00:00
|
|
|
return is_visible_.Run(command_id);
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
bool Menu::GetAcceleratorForCommandIdWithParams(
|
|
|
|
int command_id,
|
|
|
|
bool use_default_accelerator,
|
|
|
|
ui::Accelerator* accelerator) const {
|
2015-06-23 09:22:14 +00:00
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2016-07-02 02:47:40 +00:00
|
|
|
v8::Local<v8::Value> val = get_accelerator_.Run(
|
|
|
|
command_id, use_default_accelerator);
|
2015-06-23 09:22:14 +00:00
|
|
|
return mate::ConvertFromV8(isolate(), val, accelerator);
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 02:22:14 +00:00
|
|
|
void Menu::ExecuteCommand(int command_id, int flags) {
|
|
|
|
execute_command_.Run(
|
|
|
|
mate::internal::CreateEventFromFlags(isolate(), flags),
|
|
|
|
command_id);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 07:25:36 +00:00
|
|
|
void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
|
2015-02-13 03:47:22 +00:00
|
|
|
menu_will_show_.Run();
|
2014-05-25 07:25:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::InsertItemAt(
|
|
|
|
int index, int command_id, const base::string16& label) {
|
|
|
|
model_->InsertItemAt(index, command_id, label);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::InsertSeparatorAt(int index) {
|
|
|
|
model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::InsertCheckItemAt(int index,
|
|
|
|
int command_id,
|
|
|
|
const base::string16& label) {
|
|
|
|
model_->InsertCheckItemAt(index, command_id, label);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::InsertRadioItemAt(int index,
|
|
|
|
int command_id,
|
|
|
|
const base::string16& label,
|
|
|
|
int group_id) {
|
|
|
|
model_->InsertRadioItemAt(index, command_id, label, group_id);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::InsertSubMenuAt(int index,
|
|
|
|
int command_id,
|
|
|
|
const base::string16& label,
|
|
|
|
Menu* menu) {
|
2014-05-26 03:34:36 +00:00
|
|
|
menu->parent_ = this;
|
2014-04-21 15:40:10 +00:00
|
|
|
model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 04:11:50 +00:00
|
|
|
void Menu::SetIcon(int index, const gfx::Image& image) {
|
|
|
|
model_->SetIcon(index, image);
|
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::SetSublabel(int index, const base::string16& sublabel) {
|
|
|
|
model_->SetSublabel(index, sublabel);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
void Menu::SetRole(int index, const base::string16& role) {
|
|
|
|
model_->SetRole(index, role);
|
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::Clear() {
|
|
|
|
model_->Clear();
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
int Menu::GetIndexOfCommandId(int command_id) {
|
|
|
|
return model_->GetIndexOfCommandId(command_id);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
int Menu::GetItemCount() const {
|
|
|
|
return model_->GetItemCount();
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
int Menu::GetCommandIdAt(int index) const {
|
|
|
|
return model_->GetCommandIdAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
base::string16 Menu::GetLabelAt(int index) const {
|
|
|
|
return model_->GetLabelAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
base::string16 Menu::GetSublabelAt(int index) const {
|
|
|
|
return model_->GetSublabelAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
bool Menu::IsItemCheckedAt(int index) const {
|
|
|
|
return model_->IsItemCheckedAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
bool Menu::IsEnabledAt(int index) const {
|
|
|
|
return model_->IsEnabledAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
bool Menu::IsVisibleAt(int index) const {
|
|
|
|
return model_->IsVisibleAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 08:50:56 +00:00
|
|
|
// static
|
2014-04-21 15:40:10 +00:00
|
|
|
void Menu::BuildPrototype(v8::Isolate* isolate,
|
2016-08-02 09:08:12 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2015-12-03 07:38:43 +00:00
|
|
|
.MakeDestroyable()
|
2014-04-21 15:40:10 +00:00
|
|
|
.SetMethod("insertItem", &Menu::InsertItemAt)
|
|
|
|
.SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
|
|
|
|
.SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
|
|
|
|
.SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
|
|
|
|
.SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
|
2015-02-13 04:11:50 +00:00
|
|
|
.SetMethod("setIcon", &Menu::SetIcon)
|
2014-04-21 15:40:10 +00:00
|
|
|
.SetMethod("setSublabel", &Menu::SetSublabel)
|
2015-09-01 11:48:11 +00:00
|
|
|
.SetMethod("setRole", &Menu::SetRole)
|
2014-04-21 15:40:10 +00:00
|
|
|
.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)
|
2017-02-16 18:58:02 +00:00
|
|
|
.SetMethod("popupAt", &Menu::PopupAt)
|
|
|
|
.SetMethod("closePopupAt", &Menu::ClosePopupAt);
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
} // namespace api
|
2013-05-06 16:03:34 +00:00
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
} // namespace atom
|
2013-05-14 08:50:56 +00:00
|
|
|
|
2013-10-02 13:24:21 +00:00
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
namespace {
|
2013-05-16 02:54:37 +00:00
|
|
|
|
2016-08-02 08:02:04 +00:00
|
|
|
using atom::api::Menu;
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2016-08-02 10:28:12 +00:00
|
|
|
Menu::SetConstructor(isolate, base::Bind(&Menu::New));
|
2016-08-02 08:02:04 +00:00
|
|
|
|
2014-06-28 11:49:55 +00:00
|
|
|
mate::Dictionary dict(isolate, exports);
|
2016-08-02 08:20:58 +00:00
|
|
|
dict.Set("Menu", Menu::GetConstructor(isolate)->GetFunction());
|
2013-05-16 12:31:58 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2014-04-21 15:40:10 +00:00
|
|
|
dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
|
|
|
|
dict.SetMethod("sendActionToFirstResponder",
|
|
|
|
&Menu::SendActionToFirstResponder);
|
2013-05-16 12:31:58 +00:00
|
|
|
#endif
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
} // namespace
|
2013-05-06 12:27:09 +00:00
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_menu, Initialize)
|