electron_api_menu.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_ELECTRON_API_MENU_H_
  5. #define SHELL_BROWSER_API_ELECTRON_API_MENU_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/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 {
  16. namespace api {
  17. class Menu : public gin::Wrappable<Menu>,
  18. public gin_helper::EventEmitterMixin<Menu>,
  19. public gin_helper::Constructible<Menu>,
  20. public gin_helper::Pinnable<Menu>,
  21. public ElectronMenuModel::Delegate,
  22. public ElectronMenuModel::Observer {
  23. public:
  24. // gin_helper::Constructible
  25. static gin::Handle<Menu> New(gin::Arguments* args);
  26. static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
  27. v8::Isolate*,
  28. v8::Local<v8::ObjectTemplate>);
  29. // gin::Wrappable
  30. static gin::WrapperInfo kWrapperInfo;
  31. #if defined(OS_MAC)
  32. // Set the global menubar.
  33. static void SetApplicationMenu(Menu* menu);
  34. // Fake sending an action from the application menu.
  35. static void SendActionToFirstResponder(const std::string& action);
  36. #endif
  37. ElectronMenuModel* model() const { return model_.get(); }
  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 defined(OS_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. DISALLOW_COPY_AND_ASSIGN(Menu);
  104. };
  105. } // namespace api
  106. } // namespace electron
  107. namespace gin {
  108. template <>
  109. struct Converter<electron::ElectronMenuModel*> {
  110. static bool FromV8(v8::Isolate* isolate,
  111. v8::Local<v8::Value> val,
  112. electron::ElectronMenuModel** out) {
  113. // null would be transferred to NULL.
  114. if (val->IsNull()) {
  115. *out = nullptr;
  116. return true;
  117. }
  118. electron::api::Menu* menu;
  119. if (!Converter<electron::api::Menu*>::FromV8(isolate, val, &menu))
  120. return false;
  121. *out = menu->model();
  122. return true;
  123. }
  124. };
  125. } // namespace gin
  126. #endif // SHELL_BROWSER_API_ELECTRON_API_MENU_H_