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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/electron_api_menu.h"
|
2013-05-06 12:27:09 +00:00
|
|
|
|
2019-11-20 11:17:39 +00:00
|
|
|
#include <utility>
|
2019-08-06 21:55:00 +00:00
|
|
|
|
2020-03-30 01:32:02 +00:00
|
|
|
#include "shell/browser/api/ui_event.h"
|
2020-06-22 16:35:24 +00:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_converters/accelerator_converter.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
2020-10-20 01:33:06 +00:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/gin_converters/image_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2020-04-13 23:39:26 +00:00
|
|
|
#include "ui/base/models/image_model.h"
|
2014-04-21 15:40:10 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
|
|
|
|
namespace gin {
|
|
|
|
|
|
|
|
using SharingItem = electron::ElectronMenuModel::SharingItem;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<SharingItem> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
SharingItem* out) {
|
|
|
|
gin_helper::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
dict.GetOptional("texts", &(out->texts));
|
|
|
|
dict.GetOptional("filePaths", &(out->file_paths));
|
|
|
|
dict.GetOptional("urls", &(out->urls));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2013-05-06 12:27:09 +00:00
|
|
|
|
2020-04-02 23:07:56 +00:00
|
|
|
gin::WrapperInfo Menu::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2021-06-08 02:00:05 +00:00
|
|
|
Menu::Menu(gin::Arguments* args)
|
|
|
|
: model_(std::make_unique<ElectronMenuModel>(this)) {
|
2018-01-27 15:40:50 +00:00
|
|
|
model_->AddObserver(this);
|
2020-10-20 01:33:06 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
gin_helper::Dictionary options;
|
|
|
|
if (args->GetNext(&options)) {
|
|
|
|
ElectronMenuModel::SharingItem item;
|
|
|
|
if (options.Get("sharingItem", &item))
|
|
|
|
model_->SetSharingItem(std::move(item));
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2020-04-02 23:07:56 +00:00
|
|
|
bool InvokeBoolMethod(const Menu* menu,
|
|
|
|
const char* method,
|
|
|
|
int command_id,
|
|
|
|
bool default_value = false) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
// We need to cast off const here because GetWrapper() is non-const, but
|
|
|
|
// ui::SimpleMenuModel::Delegate's methods are const.
|
|
|
|
v8::Local<v8::Value> val = gin_helper::CallMethod(
|
|
|
|
isolate, const_cast<Menu*>(menu), method, command_id);
|
|
|
|
bool ret = false;
|
|
|
|
return gin::ConvertFromV8(isolate, val, &ret) ? ret : default_value;
|
2015-02-13 03:47:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 12:27:09 +00:00
|
|
|
bool Menu::IsCommandIdChecked(int command_id) const {
|
2020-04-02 23:07:56 +00:00
|
|
|
return InvokeBoolMethod(this, "_isCommandIdChecked", command_id);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Menu::IsCommandIdEnabled(int command_id) const {
|
2020-04-02 23:07:56 +00:00
|
|
|
return InvokeBoolMethod(this, "_isCommandIdEnabled", 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 {
|
2020-04-02 23:07:56 +00:00
|
|
|
return InvokeBoolMethod(this, "_isCommandIdVisible", command_id);
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 17:00:54 +00:00
|
|
|
bool Menu::ShouldCommandIdWorkWhenHidden(int command_id) const {
|
2020-04-02 23:07:56 +00:00
|
|
|
return InvokeBoolMethod(this, "_shouldCommandIdWorkWhenHidden", command_id);
|
2019-02-28 17:00:54 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
bool Menu::GetAcceleratorForCommandIdWithParams(
|
|
|
|
int command_id,
|
|
|
|
bool use_default_accelerator,
|
|
|
|
ui::Accelerator* accelerator) const {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Value> val = gin_helper::CallMethod(
|
|
|
|
isolate, const_cast<Menu*>(this), "_getAcceleratorForCommandId",
|
|
|
|
command_id, use_default_accelerator);
|
|
|
|
return gin::ConvertFromV8(isolate, val, accelerator);
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 18:43:55 +00:00
|
|
|
bool Menu::ShouldRegisterAcceleratorForCommandId(int command_id) const {
|
2020-04-02 23:07:56 +00:00
|
|
|
return InvokeBoolMethod(this, "_shouldRegisterAcceleratorForCommandId",
|
|
|
|
command_id);
|
2018-11-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
bool Menu::GetSharingItemForCommandId(
|
|
|
|
int command_id,
|
|
|
|
ElectronMenuModel::SharingItem* item) const {
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Value> val =
|
|
|
|
gin_helper::CallMethod(isolate, const_cast<Menu*>(this),
|
|
|
|
"_getSharingItemForCommandId", command_id);
|
|
|
|
return gin::ConvertFromV8(isolate, val, item);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-22 02:22:14 +00:00
|
|
|
void Menu::ExecuteCommand(int command_id, int flags) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
gin_helper::CallMethod(isolate, const_cast<Menu*>(this), "_executeCommand",
|
|
|
|
CreateEventFromFlags(flags), command_id);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 14:56:59 +00:00
|
|
|
void Menu::OnMenuWillShow(ui::SimpleMenuModel* source) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
gin_helper::CallMethod(isolate, const_cast<Menu*>(this), "_menuWillShow");
|
|
|
|
}
|
|
|
|
|
|
|
|
base::OnceClosure Menu::BindSelfToClosure(base::OnceClosure callback) {
|
|
|
|
// return ((callback, ref) => { callback() }).bind(null, callback, this)
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> self;
|
|
|
|
if (GetWrapper(isolate).ToLocal(&self)) {
|
|
|
|
v8::Global<v8::Value> ref(isolate, self);
|
|
|
|
return base::BindOnce(
|
|
|
|
[](base::OnceClosure callback, v8::Global<v8::Value> ref) {
|
|
|
|
std::move(callback).Run();
|
|
|
|
},
|
|
|
|
std::move(callback), std::move(ref));
|
|
|
|
} else {
|
|
|
|
return base::DoNothing();
|
|
|
|
}
|
2014-05-25 07:25:36 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Menu::InsertItemAt(int index,
|
|
|
|
int command_id,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& 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,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& label) {
|
2014-04-21 15:40:10 +00:00
|
|
|
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,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& label,
|
2014-04-21 15:40:10 +00:00
|
|
|
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,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& label,
|
2014-04-21 15:40:10 +00:00
|
|
|
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) {
|
2020-04-13 23:39:26 +00:00
|
|
|
model_->SetIcon(index, ui::ImageModel::FromImage(image));
|
2015-02-13 04:11:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void Menu::SetSublabel(int index, const std::u16string& sublabel) {
|
2020-05-26 20:06:26 +00:00
|
|
|
model_->SetSecondaryLabel(index, sublabel);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void Menu::SetToolTip(int index, const std::u16string& toolTip) {
|
2019-07-11 08:56:22 +00:00
|
|
|
model_->SetToolTip(index, toolTip);
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void Menu::SetRole(int index, const std::u16string& role) {
|
2015-09-01 11:48:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-29 23:28:16 +00:00
|
|
|
int Menu::GetIndexOfCommandId(int command_id) const {
|
2022-08-17 18:35:53 +00:00
|
|
|
return model_->GetIndexOfCommandId(command_id).value_or(-1);
|
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
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string Menu::GetLabelAt(int index) const {
|
2014-04-21 15:40:10 +00:00
|
|
|
return model_->GetLabelAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string Menu::GetSublabelAt(int index) const {
|
2020-05-26 20:06:26 +00:00
|
|
|
return model_->GetSecondaryLabelAt(index);
|
2013-05-06 16:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string Menu::GetToolTipAt(int index) const {
|
2019-07-11 08:56:22 +00:00
|
|
|
return model_->GetToolTipAt(index);
|
|
|
|
}
|
|
|
|
|
2021-06-02 07:32:48 +00:00
|
|
|
std::u16string Menu::GetAcceleratorTextAtForTesting(int index) const {
|
2018-10-31 14:13:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-28 17:00:54 +00:00
|
|
|
bool Menu::WorksWhenHiddenAt(int index) const {
|
|
|
|
return model_->WorksWhenHiddenAt(index);
|
|
|
|
}
|
|
|
|
|
2018-01-27 14:35:58 +00:00
|
|
|
void Menu::OnMenuWillClose() {
|
2020-04-02 23:07:56 +00:00
|
|
|
Unpin();
|
2018-01-27 14:35:58 +00:00
|
|
|
Emit("menu-will-close");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Menu::OnMenuWillShow() {
|
2021-03-29 10:45:08 +00:00
|
|
|
Pin(JavascriptEnvironment::GetIsolate());
|
2018-01-27 14:35:58 +00:00
|
|
|
Emit("menu-will-show");
|
|
|
|
}
|
|
|
|
|
2013-05-14 08:50:56 +00:00
|
|
|
// static
|
2023-02-06 20:59:49 +00:00
|
|
|
void Menu::FillObjectTemplate(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> templ) {
|
|
|
|
gin::ObjectTemplateBuilder(isolate, "Menu", templ)
|
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)
|
2019-07-11 08:56:22 +00:00
|
|
|
.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)
|
2019-07-11 08:56:22 +00:00
|
|
|
.SetMethod("getToolTipAt", &Menu::GetToolTipAt)
|
2014-04-21 15:40:10 +00:00
|
|
|
.SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
|
|
|
|
.SetMethod("isEnabledAt", &Menu::IsEnabledAt)
|
2019-02-28 17:00:54 +00:00
|
|
|
.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)
|
2020-04-02 23:07:56 +00:00
|
|
|
.SetMethod("closePopupAt", &Menu::ClosePopupAt)
|
2021-09-27 14:58:03 +00:00
|
|
|
.SetMethod("_getAcceleratorTextAt", &Menu::GetAcceleratorTextAtForTesting)
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2021-06-29 23:28:16 +00:00
|
|
|
.SetMethod("_getUserAcceleratorAt", &Menu::GetUserAcceleratorAt)
|
2021-06-02 07:32:48 +00:00
|
|
|
#endif
|
2020-04-02 23:07:56 +00:00
|
|
|
.Build();
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|
2013-05-14 08:50:56 +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 electron::api::Menu;
|
|
|
|
|
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) {
|
2014-06-29 12:48:44 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2016-08-02 08:02:04 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2020-04-02 23:07:56 +00:00
|
|
|
dict.Set("Menu", Menu::GetConstructor(context));
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
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
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_menu, Initialize)
|