atom_api_menu.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_menu.h"
  5. #include <utility>
  6. #include "atom/browser/native_window.h"
  7. #include "atom/common/native_mate_converters/accelerator_converter.h"
  8. #include "atom/common/native_mate_converters/image_converter.h"
  9. #include "atom/common/native_mate_converters/once_callback.h"
  10. #include "atom/common/native_mate_converters/string16_converter.h"
  11. #include "native_mate/constructor.h"
  12. #include "native_mate/dictionary.h"
  13. #include "native_mate/object_template_builder.h"
  14. #include "atom/common/node_includes.h"
  15. namespace atom {
  16. namespace api {
  17. Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
  18. : model_(new AtomMenuModel(this)) {
  19. InitWith(isolate, wrapper);
  20. model_->AddObserver(this);
  21. }
  22. Menu::~Menu() {
  23. if (model_) {
  24. model_->RemoveObserver(this);
  25. }
  26. }
  27. void Menu::AfterInit(v8::Isolate* isolate) {
  28. mate::Dictionary wrappable(isolate, GetWrapper());
  29. mate::Dictionary delegate;
  30. if (!wrappable.Get("delegate", &delegate))
  31. return;
  32. delegate.Get("isCommandIdChecked", &is_checked_);
  33. delegate.Get("isCommandIdEnabled", &is_enabled_);
  34. delegate.Get("isCommandIdVisible", &is_visible_);
  35. delegate.Get("getAcceleratorForCommandId", &get_accelerator_);
  36. delegate.Get("shouldRegisterAcceleratorForCommandId",
  37. &should_register_accelerator_);
  38. delegate.Get("executeCommand", &execute_command_);
  39. delegate.Get("menuWillShow", &menu_will_show_);
  40. }
  41. bool Menu::IsCommandIdChecked(int command_id) const {
  42. v8::Locker locker(isolate());
  43. v8::HandleScope handle_scope(isolate());
  44. return is_checked_.Run(GetWrapper(), command_id);
  45. }
  46. bool Menu::IsCommandIdEnabled(int command_id) const {
  47. v8::Locker locker(isolate());
  48. v8::HandleScope handle_scope(isolate());
  49. return is_enabled_.Run(GetWrapper(), command_id);
  50. }
  51. bool Menu::IsCommandIdVisible(int command_id) const {
  52. v8::Locker locker(isolate());
  53. v8::HandleScope handle_scope(isolate());
  54. return is_visible_.Run(GetWrapper(), command_id);
  55. }
  56. bool Menu::GetAcceleratorForCommandIdWithParams(
  57. int command_id,
  58. bool use_default_accelerator,
  59. ui::Accelerator* accelerator) const {
  60. v8::Locker locker(isolate());
  61. v8::HandleScope handle_scope(isolate());
  62. v8::Local<v8::Value> val =
  63. get_accelerator_.Run(GetWrapper(), command_id, use_default_accelerator);
  64. return mate::ConvertFromV8(isolate(), val, accelerator);
  65. }
  66. bool Menu::ShouldRegisterAcceleratorForCommandId(int command_id) const {
  67. v8::Locker locker(isolate());
  68. v8::HandleScope handle_scope(isolate());
  69. return should_register_accelerator_.Run(GetWrapper(), command_id);
  70. }
  71. void Menu::ExecuteCommand(int command_id, int flags) {
  72. v8::Locker locker(isolate());
  73. v8::HandleScope handle_scope(isolate());
  74. execute_command_.Run(GetWrapper(),
  75. mate::internal::CreateEventFromFlags(isolate(), flags),
  76. command_id);
  77. }
  78. void Menu::OnMenuWillShow(ui::SimpleMenuModel* source) {
  79. v8::Locker locker(isolate());
  80. v8::HandleScope handle_scope(isolate());
  81. menu_will_show_.Run(GetWrapper());
  82. }
  83. void Menu::InsertItemAt(int index,
  84. int command_id,
  85. const base::string16& label) {
  86. model_->InsertItemAt(index, command_id, label);
  87. }
  88. void Menu::InsertSeparatorAt(int index) {
  89. model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
  90. }
  91. void Menu::InsertCheckItemAt(int index,
  92. int command_id,
  93. const base::string16& label) {
  94. model_->InsertCheckItemAt(index, command_id, label);
  95. }
  96. void Menu::InsertRadioItemAt(int index,
  97. int command_id,
  98. const base::string16& label,
  99. int group_id) {
  100. model_->InsertRadioItemAt(index, command_id, label, group_id);
  101. }
  102. void Menu::InsertSubMenuAt(int index,
  103. int command_id,
  104. const base::string16& label,
  105. Menu* menu) {
  106. menu->parent_ = this;
  107. model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
  108. }
  109. void Menu::SetIcon(int index, const gfx::Image& image) {
  110. model_->SetIcon(index, image);
  111. }
  112. void Menu::SetSublabel(int index, const base::string16& sublabel) {
  113. model_->SetSublabel(index, sublabel);
  114. }
  115. void Menu::SetRole(int index, const base::string16& role) {
  116. model_->SetRole(index, role);
  117. }
  118. void Menu::Clear() {
  119. model_->Clear();
  120. }
  121. int Menu::GetIndexOfCommandId(int command_id) {
  122. return model_->GetIndexOfCommandId(command_id);
  123. }
  124. int Menu::GetItemCount() const {
  125. return model_->GetItemCount();
  126. }
  127. int Menu::GetCommandIdAt(int index) const {
  128. return model_->GetCommandIdAt(index);
  129. }
  130. base::string16 Menu::GetLabelAt(int index) const {
  131. return model_->GetLabelAt(index);
  132. }
  133. base::string16 Menu::GetSublabelAt(int index) const {
  134. return model_->GetSublabelAt(index);
  135. }
  136. base::string16 Menu::GetAcceleratorTextAt(int index) const {
  137. ui::Accelerator accelerator;
  138. model_->GetAcceleratorAtWithParams(index, true, &accelerator);
  139. return accelerator.GetShortcutText();
  140. }
  141. bool Menu::IsItemCheckedAt(int index) const {
  142. return model_->IsItemCheckedAt(index);
  143. }
  144. bool Menu::IsEnabledAt(int index) const {
  145. return model_->IsEnabledAt(index);
  146. }
  147. bool Menu::IsVisibleAt(int index) const {
  148. return model_->IsVisibleAt(index);
  149. }
  150. void Menu::OnMenuWillClose() {
  151. Emit("menu-will-close");
  152. }
  153. void Menu::OnMenuWillShow() {
  154. Emit("menu-will-show");
  155. }
  156. base::OnceClosure Menu::BindSelfToClosure(base::OnceClosure callback) {
  157. // return ((callback, ref) => { callback() }).bind(null, callback, this)
  158. v8::Global<v8::Value> ref(isolate(), GetWrapper());
  159. return base::BindOnce(
  160. [](base::OnceClosure callback, v8::Global<v8::Value> ref) {
  161. std::move(callback).Run();
  162. },
  163. std::move(callback), std::move(ref));
  164. }
  165. // static
  166. void Menu::BuildPrototype(v8::Isolate* isolate,
  167. v8::Local<v8::FunctionTemplate> prototype) {
  168. prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
  169. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  170. .MakeDestroyable()
  171. .SetMethod("insertItem", &Menu::InsertItemAt)
  172. .SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
  173. .SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
  174. .SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
  175. .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
  176. .SetMethod("setIcon", &Menu::SetIcon)
  177. .SetMethod("setSublabel", &Menu::SetSublabel)
  178. .SetMethod("setRole", &Menu::SetRole)
  179. .SetMethod("clear", &Menu::Clear)
  180. .SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
  181. .SetMethod("getItemCount", &Menu::GetItemCount)
  182. .SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
  183. .SetMethod("getLabelAt", &Menu::GetLabelAt)
  184. .SetMethod("getSublabelAt", &Menu::GetSublabelAt)
  185. .SetMethod("getAcceleratorTextAt", &Menu::GetAcceleratorTextAt)
  186. .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
  187. .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
  188. .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
  189. .SetMethod("popupAt", &Menu::PopupAt)
  190. .SetMethod("closePopupAt", &Menu::ClosePopupAt);
  191. }
  192. } // namespace api
  193. } // namespace atom
  194. namespace {
  195. using atom::api::Menu;
  196. void Initialize(v8::Local<v8::Object> exports,
  197. v8::Local<v8::Value> unused,
  198. v8::Local<v8::Context> context,
  199. void* priv) {
  200. v8::Isolate* isolate = context->GetIsolate();
  201. Menu::SetConstructor(isolate, base::Bind(&Menu::New));
  202. mate::Dictionary dict(isolate, exports);
  203. dict.Set(
  204. "Menu",
  205. Menu::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  206. #if defined(OS_MACOSX)
  207. dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
  208. dict.SetMethod("sendActionToFirstResponder",
  209. &Menu::SendActionToFirstResponder);
  210. #endif
  211. }
  212. } // namespace
  213. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_menu, Initialize)