atom_api_menu.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef SHELL_BROWSER_API_ATOM_API_MENU_H_
  5. #define SHELL_BROWSER_API_ATOM_API_MENU_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "shell/browser/api/atom_api_top_level_window.h"
  10. #include "shell/browser/api/trackable_object.h"
  11. #include "shell/browser/ui/atom_menu_model.h"
  12. namespace electron {
  13. namespace api {
  14. class Menu : public mate::TrackableObject<Menu>,
  15. public AtomMenuModel::Delegate,
  16. public AtomMenuModel::Observer {
  17. public:
  18. static mate::WrappableBase* New(mate::Arguments* args);
  19. static void BuildPrototype(v8::Isolate* isolate,
  20. v8::Local<v8::FunctionTemplate> prototype);
  21. #if defined(OS_MACOSX)
  22. // Set the global menubar.
  23. static void SetApplicationMenu(Menu* menu);
  24. // Fake sending an action from the application menu.
  25. static void SendActionToFirstResponder(const std::string& action);
  26. #endif
  27. AtomMenuModel* model() const { return model_.get(); }
  28. protected:
  29. Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
  30. ~Menu() override;
  31. // mate::Wrappable:
  32. void AfterInit(v8::Isolate* isolate) override;
  33. // ui::SimpleMenuModel::Delegate:
  34. bool IsCommandIdChecked(int command_id) const override;
  35. bool IsCommandIdEnabled(int command_id) const override;
  36. bool IsCommandIdVisible(int command_id) const override;
  37. bool ShouldCommandIdWorkWhenHidden(int command_id) const override;
  38. bool GetAcceleratorForCommandIdWithParams(
  39. int command_id,
  40. bool use_default_accelerator,
  41. ui::Accelerator* accelerator) const override;
  42. bool ShouldRegisterAcceleratorForCommandId(int command_id) const override;
  43. void ExecuteCommand(int command_id, int event_flags) override;
  44. void OnMenuWillShow(ui::SimpleMenuModel* source) override;
  45. virtual void PopupAt(TopLevelWindow* window,
  46. int x,
  47. int y,
  48. int positioning_item,
  49. base::OnceClosure callback) = 0;
  50. virtual void ClosePopupAt(int32_t window_id) = 0;
  51. std::unique_ptr<AtomMenuModel> model_;
  52. Menu* parent_ = nullptr;
  53. // Observable:
  54. void OnMenuWillClose() override;
  55. void OnMenuWillShow() override;
  56. protected:
  57. // Returns a new callback which keeps references of the JS wrapper until the
  58. // passed |callback| is called.
  59. base::OnceClosure BindSelfToClosure(base::OnceClosure callback);
  60. private:
  61. void InsertItemAt(int index, int command_id, const base::string16& label);
  62. void InsertSeparatorAt(int index);
  63. void InsertCheckItemAt(int index,
  64. int command_id,
  65. const base::string16& label);
  66. void InsertRadioItemAt(int index,
  67. int command_id,
  68. const base::string16& label,
  69. int group_id);
  70. void InsertSubMenuAt(int index,
  71. int command_id,
  72. const base::string16& label,
  73. Menu* menu);
  74. void SetIcon(int index, const gfx::Image& image);
  75. void SetSublabel(int index, const base::string16& sublabel);
  76. void SetToolTip(int index, const base::string16& toolTip);
  77. void SetRole(int index, const base::string16& role);
  78. void Clear();
  79. int GetIndexOfCommandId(int command_id);
  80. int GetItemCount() const;
  81. int GetCommandIdAt(int index) const;
  82. base::string16 GetLabelAt(int index) const;
  83. base::string16 GetSublabelAt(int index) const;
  84. base::string16 GetToolTipAt(int index) const;
  85. base::string16 GetAcceleratorTextAt(int index) const;
  86. bool IsItemCheckedAt(int index) const;
  87. bool IsEnabledAt(int index) const;
  88. bool IsVisibleAt(int index) const;
  89. bool WorksWhenHiddenAt(int index) const;
  90. // Stored delegate methods.
  91. base::RepeatingCallback<bool(v8::Local<v8::Value>, int)> is_checked_;
  92. base::RepeatingCallback<bool(v8::Local<v8::Value>, int)> is_enabled_;
  93. base::RepeatingCallback<bool(v8::Local<v8::Value>, int)> is_visible_;
  94. base::RepeatingCallback<bool(v8::Local<v8::Value>, int)> works_when_hidden_;
  95. base::RepeatingCallback<v8::Local<v8::Value>(v8::Local<v8::Value>, int, bool)>
  96. get_accelerator_;
  97. base::RepeatingCallback<bool(v8::Local<v8::Value>, int)>
  98. should_register_accelerator_;
  99. base::RepeatingCallback<void(v8::Local<v8::Value>, v8::Local<v8::Value>, int)>
  100. execute_command_;
  101. base::RepeatingCallback<void(v8::Local<v8::Value>)> menu_will_show_;
  102. DISALLOW_COPY_AND_ASSIGN(Menu);
  103. };
  104. } // namespace api
  105. } // namespace electron
  106. namespace mate {
  107. template <>
  108. struct Converter<electron::AtomMenuModel*> {
  109. static bool FromV8(v8::Isolate* isolate,
  110. v8::Local<v8::Value> val,
  111. electron::AtomMenuModel** out) {
  112. // null would be tranfered to NULL.
  113. if (val->IsNull()) {
  114. *out = nullptr;
  115. return true;
  116. }
  117. electron::api::Menu* menu;
  118. if (!Converter<electron::api::Menu*>::FromV8(isolate, val, &menu))
  119. return false;
  120. *out = menu->model();
  121. return true;
  122. }
  123. };
  124. } // namespace mate
  125. #endif // SHELL_BROWSER_API_ATOM_API_MENU_H_