electron/shell/browser/api/atom_api_menu.cc

281 lines
8.7 KiB
C++
Raw Normal View History

// 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.
#include "shell/browser/api/atom_api_menu.h"
2013-05-06 12:27:09 +00:00
#include <map>
2014-04-21 15:40:10 +00:00
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "shell/browser/native_window.h"
#include "shell/common/native_mate_converters/accelerator_converter.h"
#include "shell/common/native_mate_converters/callback.h"
#include "shell/common/native_mate_converters/image_converter.h"
#include "shell/common/native_mate_converters/string16_converter.h"
#include "shell/common/node_includes.h"
2014-04-21 15:40:10 +00:00
namespace {
// We need this map to keep references to currently opened menus.
// Without this menus would be destroyed by js garbage collector
// even when they are still displayed.
std::map<uint32_t, v8::Global<v8::Object>> g_menus;
} // unnamed namespace
namespace electron {
2013-05-06 12:27:09 +00:00
namespace api {
Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
: model_(new AtomMenuModel(this)) {
InitWith(isolate, wrapper);
2018-01-27 15:40:50 +00:00
model_->AddObserver(this);
2013-05-06 12:27:09 +00:00
}
Menu::~Menu() {
2018-01-27 15:40:50 +00:00
if (model_) {
model_->RemoveObserver(this);
}
2013-05-06 12:27:09 +00:00
}
void Menu::AfterInit(v8::Isolate* isolate) {
mate::Dictionary wrappable(isolate, GetWrapper());
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("shouldCommandIdWorkWhenHidden", &works_when_hidden_);
delegate.Get("getAcceleratorForCommandId", &get_accelerator_);
delegate.Get("shouldRegisterAcceleratorForCommandId",
&should_register_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 {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
return is_checked_.Run(GetWrapper(), command_id);
2013-05-06 12:27:09 +00:00
}
bool Menu::IsCommandIdEnabled(int command_id) const {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
return is_enabled_.Run(GetWrapper(), 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 {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
return is_visible_.Run(GetWrapper(), command_id);
2013-05-14 08:50:56 +00:00
}
bool Menu::ShouldCommandIdWorkWhenHidden(int command_id) const {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
return works_when_hidden_.Run(GetWrapper(), command_id);
}
bool Menu::GetAcceleratorForCommandIdWithParams(
int command_id,
bool use_default_accelerator,
ui::Accelerator* accelerator) const {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
2018-04-18 01:55:30 +00:00
v8::Local<v8::Value> val =
get_accelerator_.Run(GetWrapper(), command_id, use_default_accelerator);
return mate::ConvertFromV8(isolate(), val, accelerator);
2013-05-14 08:50:56 +00:00
}
bool Menu::ShouldRegisterAcceleratorForCommandId(int command_id) const {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
return should_register_accelerator_.Run(GetWrapper(), command_id);
}
void Menu::ExecuteCommand(int command_id, int flags) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
2018-04-18 01:55:30 +00:00
execute_command_.Run(GetWrapper(),
mate::internal::CreateEventFromFlags(isolate(), flags),
command_id);
2013-05-06 12:27:09 +00:00
}
void Menu::OnMenuWillShow(ui::SimpleMenuModel* source) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
menu_will_show_.Run(GetWrapper());
}
2018-04-18 01:55:30 +00:00
void Menu::InsertItemAt(int index,
int command_id,
const base::string16& label) {
2014-04-21 15:40:10 +00:00
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) {
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
}
void Menu::SetToolTip(int index, const base::string16& toolTip) {
model_->SetToolTip(index, toolTip);
}
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
}
base::string16 Menu::GetToolTipAt(int index) const {
return model_->GetToolTipAt(index);
}
base::string16 Menu::GetAcceleratorTextAt(int index) const {
ui::Accelerator accelerator;
model_->GetAcceleratorAtWithParams(index, true, &accelerator);
return accelerator.GetShortcutText();
}
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
}
bool Menu::WorksWhenHiddenAt(int index) const {
return model_->WorksWhenHiddenAt(index);
}
2018-01-27 14:35:58 +00:00
void Menu::OnMenuWillClose() {
g_menus.erase(weak_map_id());
2018-01-27 14:35:58 +00:00
Emit("menu-will-close");
}
void Menu::OnMenuWillShow() {
g_menus[weak_map_id()] = v8::Global<v8::Object>(isolate(), GetWrapper());
2018-01-27 14:35:58 +00:00
Emit("menu-will-show");
}
2013-05-14 08:50:56 +00:00
// static
2014-04-21 15:40:10 +00:00
void Menu::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
2016-08-02 10:28:12 +00:00
prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
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)
.SetMethod("setToolTip", &Menu::SetToolTip)
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("getToolTipAt", &Menu::GetToolTipAt)
.SetMethod("getAcceleratorTextAt", &Menu::GetAcceleratorTextAt)
2014-04-21 15:40:10 +00:00
.SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
.SetMethod("isEnabledAt", &Menu::IsEnabledAt)
.SetMethod("worksWhenHiddenAt", &Menu::WorksWhenHiddenAt)
2014-04-21 15:40:10 +00:00
.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
} // namespace electron
2013-05-14 08:50:56 +00:00
2014-04-21 15:40:10 +00:00
namespace {
using electron::api::Menu;
2016-08-02 08:02:04 +00:00
2018-04-18 01:55:30 +00:00
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
Menu::SetConstructor(isolate, base::BindRepeating(&Menu::New));
2016-08-02 08:02:04 +00:00
2014-06-28 11:49:55 +00:00
mate::Dictionary dict(isolate, exports);
dict.Set(
"Menu",
Menu::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
#if defined(OS_MACOSX)
2014-04-21 15:40:10 +00:00
dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
dict.SetMethod("sendActionToFirstResponder",
&Menu::SendActionToFirstResponder);
#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
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_menu, Initialize)