electron/atom/browser/api/atom_api_menu.cc

282 lines
9 KiB
C++
Raw Normal View History

2013-05-06 12:27:09 +00:00
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
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-04-21 15:40:10 +00:00
#include "atom/browser/api/atom_api_window.h"
2014-03-16 00:30:26 +00:00
#include "atom/browser/ui/accelerator_util.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"
#include "atom/common/node_includes.h"
2013-05-06 12:27:09 +00:00
namespace atom {
namespace api {
2013-05-06 16:03:34 +00:00
namespace {
// Call method of delegate object.
2014-06-28 11:31:23 +00:00
v8::Handle<v8::Value> CallDelegate(v8::Isolate* isolate,
v8::Handle<v8::Value> default_value,
v8::Handle<v8::Object> menu,
const char* method,
int command_id) {
2014-06-28 11:31:23 +00:00
v8::Handle<v8::Value> delegate = menu->Get(
MATE_STRING_NEW(isolate, "delegate"));
if (!delegate->IsObject())
return default_value;
v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(
2014-06-28 11:31:23 +00:00
delegate->ToObject()->Get(MATE_STRING_NEW(isolate, method)));
if (!function->IsFunction())
return default_value;
2014-06-28 11:31:23 +00:00
v8::Handle<v8::Value> argv = MATE_INTEGER_NEW(isolate, command_id);
return function->Call(isolate->GetCurrentContext()->Global(), 1, &argv);
}
2013-05-06 16:03:34 +00:00
} // namespace
Menu::Menu()
: model_(new ui::SimpleMenuModel(this)),
parent_(NULL) {
2013-05-06 12:27:09 +00:00
}
Menu::~Menu() {
}
bool Menu::IsCommandIdChecked(int command_id) const {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
return CallDelegate(isolate,
MATE_FALSE(isolate),
const_cast<Menu*>(this)->GetWrapper(isolate),
"isCommandIdChecked",
command_id)->BooleanValue();
2013-05-06 12:27:09 +00:00
}
bool Menu::IsCommandIdEnabled(int command_id) const {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
return CallDelegate(isolate,
MATE_TRUE(isolate),
const_cast<Menu*>(this)->GetWrapper(isolate),
"isCommandIdEnabled",
command_id)->BooleanValue();
2013-05-06 12:27:09 +00:00
}
2013-05-14 08:50:56 +00:00
bool Menu::IsCommandIdVisible(int command_id) const {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
return CallDelegate(isolate,
MATE_TRUE(isolate),
const_cast<Menu*>(this)->GetWrapper(isolate),
"isCommandIdVisible",
command_id)->BooleanValue();
2013-05-14 08:50:56 +00:00
}
2013-05-06 12:27:09 +00:00
bool Menu::GetAcceleratorForCommandId(int command_id,
ui::Accelerator* accelerator) {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
v8::Handle<v8::Value> shortcut = CallDelegate(isolate,
MATE_UNDEFINED(isolate),
GetWrapper(isolate),
"getAcceleratorForCommandId",
command_id);
if (shortcut->IsString()) {
2014-04-21 15:40:10 +00:00
std::string shortcut_str = mate::V8ToString(shortcut);
return accelerator_util::StringToAccelerator(shortcut_str, accelerator);
}
2013-05-06 12:27:09 +00:00
return false;
}
2013-05-14 08:50:56 +00:00
bool Menu::IsItemForCommandIdDynamic(int command_id) const {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
return CallDelegate(isolate,
MATE_FALSE(isolate),
const_cast<Menu*>(this)->GetWrapper(isolate),
"isItemForCommandIdDynamic",
command_id)->BooleanValue();
2013-05-14 08:50:56 +00:00
}
2014-06-28 11:36:57 +00:00
base::string16 Menu::GetLabelForCommandId(int command_id) const {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
2014-04-21 15:40:10 +00:00
v8::Handle<v8::Value> result = CallDelegate(
2014-06-28 11:31:23 +00:00
isolate,
MATE_FALSE(isolate),
const_cast<Menu*>(this)->GetWrapper(isolate),
2014-04-21 15:40:10 +00:00
"getLabelForCommandId",
command_id);
2014-06-28 11:36:57 +00:00
base::string16 label;
2014-06-28 11:49:55 +00:00
mate::ConvertFromV8(isolate, result, &label);
2014-04-21 15:40:10 +00:00
return label;
2013-05-14 08:50:56 +00:00
}
2014-06-28 11:36:57 +00:00
base::string16 Menu::GetSublabelForCommandId(int command_id) const {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
2014-04-21 15:40:10 +00:00
v8::Handle<v8::Value> result = CallDelegate(
2014-06-28 11:31:23 +00:00
isolate,
MATE_FALSE(isolate),
const_cast<Menu*>(this)->GetWrapper(isolate),
2014-04-21 15:40:10 +00:00
"getSubLabelForCommandId",
command_id);
2014-06-28 11:36:57 +00:00
base::string16 label;
2014-06-28 11:31:23 +00:00
mate::ConvertFromV8(isolate, result, &label);
2014-04-21 15:40:10 +00:00
return label;
2013-05-14 08:50:56 +00:00
}
2013-05-06 12:27:09 +00:00
void Menu::ExecuteCommand(int command_id, int event_flags) {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
CallDelegate(isolate, MATE_FALSE(isolate), GetWrapper(isolate),
"executeCommand", command_id);
2013-05-06 12:27:09 +00:00
}
void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
2014-06-28 11:31:23 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
CallDelegate(isolate, MATE_FALSE(isolate), GetWrapper(isolate),
"menuWillShow", -1);
}
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) {
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
}
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
}
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,
v8::Handle<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("insertItem", &Menu::InsertItemAt)
.SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
.SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
.SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
.SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
.SetMethod("setSublabel", &Menu::SetSublabel)
.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)
#if defined(OS_WIN) || defined(TOOLKIT_GTK)
.SetMethod("_attachToWindow", &Menu::AttachToWindow)
#endif
#if defined(OS_WIN)
.SetMethod("_updateStates", &Menu::UpdateStates)
2014-04-21 15:40:10 +00:00
#endif
.SetMethod("_popup", &Menu::Popup);
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 {
2014-04-21 15:40:10 +00:00
void Initialize(v8::Handle<v8::Object> exports) {
using atom::api::Menu;
2014-06-28 11:49:55 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
2014-04-21 15:40:10 +00:00
v8::Local<v8::Function> constructor = mate::CreateConstructor<Menu>(
2014-06-28 11:49:55 +00:00
isolate, "Menu", base::Bind(&Menu::Create));
mate::Dictionary dict(isolate, exports);
2014-04-21 15:40:10 +00:00
dict.Set("Menu", static_cast<v8::Handle<v8::Value>>(constructor));
#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
2014-04-21 15:40:10 +00:00
NODE_MODULE(atom_browser_menu, Initialize)