electron_api_menu.h 4.9 KB

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