atom_api_menu.h 4.9 KB

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