123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- // Copyright (c) 2013 GitHub, Inc.
- // Use of this source code is governed by the MIT license that can be
- // found in the LICENSE file.
- #include "atom/browser/api/atom_api_menu.h"
- #include <utility>
- #include "atom/browser/native_window.h"
- #include "atom/common/native_mate_converters/accelerator_converter.h"
- #include "atom/common/native_mate_converters/image_converter.h"
- #include "atom/common/native_mate_converters/once_callback.h"
- #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"
- namespace atom {
- namespace api {
- Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
- : model_(new AtomMenuModel(this)) {
- InitWith(isolate, wrapper);
- model_->AddObserver(this);
- }
- Menu::~Menu() {
- if (model_) {
- model_->RemoveObserver(this);
- }
- }
- 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("getAcceleratorForCommandId", &get_accelerator_);
- delegate.Get("shouldRegisterAcceleratorForCommandId",
- &should_register_accelerator_);
- delegate.Get("executeCommand", &execute_command_);
- delegate.Get("menuWillShow", &menu_will_show_);
- }
- bool Menu::IsCommandIdChecked(int command_id) const {
- v8::Locker locker(isolate());
- v8::HandleScope handle_scope(isolate());
- return is_checked_.Run(GetWrapper(), command_id);
- }
- bool Menu::IsCommandIdEnabled(int command_id) const {
- v8::Locker locker(isolate());
- v8::HandleScope handle_scope(isolate());
- return is_enabled_.Run(GetWrapper(), command_id);
- }
- bool Menu::IsCommandIdVisible(int command_id) const {
- v8::Locker locker(isolate());
- v8::HandleScope handle_scope(isolate());
- return is_visible_.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());
- v8::Local<v8::Value> val =
- get_accelerator_.Run(GetWrapper(), command_id, use_default_accelerator);
- return mate::ConvertFromV8(isolate(), val, accelerator);
- }
- 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());
- execute_command_.Run(GetWrapper(),
- mate::internal::CreateEventFromFlags(isolate(), flags),
- command_id);
- }
- void Menu::OnMenuWillShow(ui::SimpleMenuModel* source) {
- v8::Locker locker(isolate());
- v8::HandleScope handle_scope(isolate());
- menu_will_show_.Run(GetWrapper());
- }
- void Menu::InsertItemAt(int index,
- int command_id,
- const base::string16& label) {
- model_->InsertItemAt(index, command_id, label);
- }
- void Menu::InsertSeparatorAt(int index) {
- model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
- }
- void Menu::InsertCheckItemAt(int index,
- int command_id,
- const base::string16& label) {
- model_->InsertCheckItemAt(index, command_id, label);
- }
- void Menu::InsertRadioItemAt(int index,
- int command_id,
- const base::string16& label,
- int group_id) {
- model_->InsertRadioItemAt(index, command_id, label, group_id);
- }
- void Menu::InsertSubMenuAt(int index,
- int command_id,
- const base::string16& label,
- Menu* menu) {
- menu->parent_ = this;
- model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
- }
- void Menu::SetIcon(int index, const gfx::Image& image) {
- model_->SetIcon(index, image);
- }
- void Menu::SetSublabel(int index, const base::string16& sublabel) {
- model_->SetSublabel(index, sublabel);
- }
- void Menu::SetRole(int index, const base::string16& role) {
- model_->SetRole(index, role);
- }
- void Menu::Clear() {
- model_->Clear();
- }
- int Menu::GetIndexOfCommandId(int command_id) {
- return model_->GetIndexOfCommandId(command_id);
- }
- int Menu::GetItemCount() const {
- return model_->GetItemCount();
- }
- int Menu::GetCommandIdAt(int index) const {
- return model_->GetCommandIdAt(index);
- }
- base::string16 Menu::GetLabelAt(int index) const {
- return model_->GetLabelAt(index);
- }
- base::string16 Menu::GetSublabelAt(int index) const {
- return model_->GetSublabelAt(index);
- }
- base::string16 Menu::GetAcceleratorTextAt(int index) const {
- ui::Accelerator accelerator;
- model_->GetAcceleratorAtWithParams(index, true, &accelerator);
- return accelerator.GetShortcutText();
- }
- bool Menu::IsItemCheckedAt(int index) const {
- return model_->IsItemCheckedAt(index);
- }
- bool Menu::IsEnabledAt(int index) const {
- return model_->IsEnabledAt(index);
- }
- bool Menu::IsVisibleAt(int index) const {
- return model_->IsVisibleAt(index);
- }
- void Menu::OnMenuWillClose() {
- Emit("menu-will-close");
- }
- void Menu::OnMenuWillShow() {
- Emit("menu-will-show");
- }
- base::OnceClosure Menu::BindSelfToClosure(base::OnceClosure callback) {
- // return ((callback, ref) => { callback() }).bind(null, callback, this)
- v8::Global<v8::Value> ref(isolate(), GetWrapper());
- return base::BindOnce(
- [](base::OnceClosure callback, v8::Global<v8::Value> ref) {
- std::move(callback).Run();
- },
- std::move(callback), std::move(ref));
- }
- // static
- void Menu::BuildPrototype(v8::Isolate* isolate,
- v8::Local<v8::FunctionTemplate> prototype) {
- prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
- mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
- .MakeDestroyable()
- .SetMethod("insertItem", &Menu::InsertItemAt)
- .SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
- .SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
- .SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
- .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
- .SetMethod("setIcon", &Menu::SetIcon)
- .SetMethod("setSublabel", &Menu::SetSublabel)
- .SetMethod("setRole", &Menu::SetRole)
- .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("getAcceleratorTextAt", &Menu::GetAcceleratorTextAt)
- .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
- .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
- .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
- .SetMethod("popupAt", &Menu::PopupAt)
- .SetMethod("closePopupAt", &Menu::ClosePopupAt);
- }
- } // namespace api
- } // namespace atom
- namespace {
- using atom::api::Menu;
- 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::Bind(&Menu::New));
- mate::Dictionary dict(isolate, exports);
- dict.Set(
- "Menu",
- Menu::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
- #if defined(OS_MACOSX)
- dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
- dict.SetMethod("sendActionToFirstResponder",
- &Menu::SendActionToFirstResponder);
- #endif
- }
- } // namespace
- NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_menu, Initialize)
|