atom_api_menu.cc 8.7 KB

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