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"
|
|
|
|
|
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 {
|
|
|
|
|
2013-05-06 16:03:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-05-14 11:26:16 +00:00
|
|
|
// Call method of delegate object.
|
|
|
|
v8::Handle<v8::Value> CallDelegate(v8::Handle<v8::Value> default_value,
|
|
|
|
v8::Handle<v8::Object> menu,
|
|
|
|
const char* method,
|
|
|
|
int command_id) {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-05-14 11:26:16 +00:00
|
|
|
|
|
|
|
v8::Handle<v8::Value> delegate = menu->Get(v8::String::New("delegate"));
|
|
|
|
if (!delegate->IsObject())
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(
|
|
|
|
delegate->ToObject()->Get(v8::String::New(method)));
|
|
|
|
if (!function->IsFunction())
|
|
|
|
return default_value;
|
|
|
|
|
2013-05-16 07:32:37 +00:00
|
|
|
v8::Handle<v8::Value> argv = v8::Integer::New(command_id);
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
return handle_scope.Close(
|
2013-05-16 07:32:37 +00:00
|
|
|
function->Call(v8::Context::GetCurrent()->Global(), 1, &argv));
|
2013-05-14 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 16:03:34 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
Menu::Menu() : model_(new ui::SimpleMenuModel(this)) {
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Menu::~Menu() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Menu::IsCommandIdChecked(int command_id) const {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-05-14 11:26:16 +00:00
|
|
|
return CallDelegate(v8::False(),
|
2014-04-21 15:40:10 +00:00
|
|
|
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
2013-05-14 11:26:16 +00:00
|
|
|
"isCommandIdChecked",
|
|
|
|
command_id)->BooleanValue();
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Menu::IsCommandIdEnabled(int command_id) const {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-05-14 11:26:16 +00:00
|
|
|
return CallDelegate(v8::True(),
|
2014-04-21 15:40:10 +00:00
|
|
|
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
2013-05-14 11:26:16 +00:00
|
|
|
"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-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-05-14 11:26:16 +00:00
|
|
|
return CallDelegate(v8::True(),
|
2014-04-21 15:40:10 +00:00
|
|
|
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
2013-05-14 11:26:16 +00:00
|
|
|
"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,
|
2013-05-14 11:26:16 +00:00
|
|
|
ui::Accelerator* accelerator) {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-05-14 13:37:59 +00:00
|
|
|
v8::Handle<v8::Value> shortcut = CallDelegate(v8::Undefined(),
|
2014-04-21 15:40:10 +00:00
|
|
|
GetWrapper(node_isolate),
|
2013-05-14 13:37:59 +00:00
|
|
|
"getAcceleratorForCommandId",
|
|
|
|
command_id);
|
|
|
|
if (shortcut->IsString()) {
|
2014-04-21 15:40:10 +00:00
|
|
|
std::string shortcut_str = mate::V8ToString(shortcut);
|
2013-05-14 13:37:59 +00:00
|
|
|
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-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-05-14 11:26:16 +00:00
|
|
|
return CallDelegate(v8::False(),
|
2014-04-21 15:40:10 +00:00
|
|
|
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
2013-05-14 11:26:16 +00:00
|
|
|
"isItemForCommandIdDynamic",
|
|
|
|
command_id)->BooleanValue();
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string16 Menu::GetLabelForCommandId(int command_id) const {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2014-04-21 15:40:10 +00:00
|
|
|
v8::Handle<v8::Value> result = CallDelegate(
|
|
|
|
v8::False(),
|
|
|
|
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
|
|
|
"getLabelForCommandId",
|
|
|
|
command_id);
|
|
|
|
string16 label;
|
|
|
|
mate::ConvertFromV8(node_isolate, result, &label);
|
|
|
|
return label;
|
2013-05-14 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string16 Menu::GetSublabelForCommandId(int command_id) const {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2014-04-21 15:40:10 +00:00
|
|
|
v8::Handle<v8::Value> result = CallDelegate(
|
|
|
|
v8::False(),
|
|
|
|
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
|
|
|
"getSubLabelForCommandId",
|
|
|
|
command_id);
|
|
|
|
string16 label;
|
|
|
|
mate::ConvertFromV8(node_isolate, result, &label);
|
|
|
|
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-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2014-04-21 15:40:10 +00:00
|
|
|
CallDelegate(v8::False(), GetWrapper(node_isolate), "executeCommand",
|
|
|
|
command_id);
|
2013-05-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 07:25:36 +00:00
|
|
|
void Menu::MenuWillShow(ui::SimpleMenuModel* source) {
|
|
|
|
v8::Locker locker(node_isolate);
|
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
|
|
|
CallDelegate(v8::False(), GetWrapper(node_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) {
|
|
|
|
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
|
2014-05-25 07:25:36 +00:00
|
|
|
.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 {
|
2013-05-16 02:54:37 +00:00
|
|
|
|
2014-04-21 15:40:10 +00:00
|
|
|
void Initialize(v8::Handle<v8::Object> exports) {
|
|
|
|
using atom::api::Menu;
|
|
|
|
v8::Local<v8::Function> constructor = mate::CreateConstructor<Menu>(
|
|
|
|
node_isolate, "Menu", base::Bind(&Menu::Create));
|
|
|
|
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
|
|
|
dict.Set("Menu", static_cast<v8::Handle<v8::Value>>(constructor));
|
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-04-21 15:40:10 +00:00
|
|
|
NODE_MODULE(atom_browser_menu, Initialize)
|