atom_api_menu.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "base/callback.h"
  12. namespace atom {
  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 GetAcceleratorForCommandIdWithParams(
  38. int command_id,
  39. bool use_default_accelerator,
  40. ui::Accelerator* accelerator) const override;
  41. bool ShouldRegisterAcceleratorForCommandId(int command_id) const override;
  42. void ExecuteCommand(int command_id, int event_flags) override;
  43. void MenuWillShow(ui::SimpleMenuModel* source) override;
  44. virtual void PopupAt(TopLevelWindow* window,
  45. int x,
  46. int y,
  47. int positioning_item,
  48. const base::Closure& callback) = 0;
  49. virtual void ClosePopupAt(int32_t window_id) = 0;
  50. std::unique_ptr<AtomMenuModel> model_;
  51. Menu* parent_ = nullptr;
  52. // Observable:
  53. void OnMenuWillClose() override;
  54. void OnMenuWillShow() override;
  55. private:
  56. void InsertItemAt(int index, int command_id, const base::string16& label);
  57. void InsertSeparatorAt(int index);
  58. void InsertCheckItemAt(int index,
  59. int command_id,
  60. const base::string16& label);
  61. void InsertRadioItemAt(int index,
  62. int command_id,
  63. const base::string16& label,
  64. int group_id);
  65. void InsertSubMenuAt(int index,
  66. int command_id,
  67. const base::string16& label,
  68. Menu* menu);
  69. void SetIcon(int index, const gfx::Image& image);
  70. void SetSublabel(int index, const base::string16& sublabel);
  71. void SetRole(int index, const base::string16& role);
  72. void Clear();
  73. int GetIndexOfCommandId(int command_id);
  74. int GetItemCount() const;
  75. int GetCommandIdAt(int index) const;
  76. base::string16 GetLabelAt(int index) const;
  77. base::string16 GetSublabelAt(int index) const;
  78. base::string16 GetAcceleratorTextAt(int index) const;
  79. bool IsItemCheckedAt(int index) const;
  80. bool IsEnabledAt(int index) const;
  81. bool IsVisibleAt(int index) const;
  82. // Stored delegate methods.
  83. base::Callback<bool(v8::Local<v8::Value>, int)> is_checked_;
  84. base::Callback<bool(v8::Local<v8::Value>, int)> is_enabled_;
  85. base::Callback<bool(v8::Local<v8::Value>, int)> is_visible_;
  86. base::Callback<v8::Local<v8::Value>(v8::Local<v8::Value>, int, bool)>
  87. get_accelerator_;
  88. base::Callback<bool(v8::Local<v8::Value>, int)> should_register_accelerator_;
  89. base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>, int)>
  90. execute_command_;
  91. base::Callback<void(v8::Local<v8::Value>)> menu_will_show_;
  92. DISALLOW_COPY_AND_ASSIGN(Menu);
  93. };
  94. } // namespace api
  95. } // namespace atom
  96. namespace mate {
  97. template <>
  98. struct Converter<atom::AtomMenuModel*> {
  99. static bool FromV8(v8::Isolate* isolate,
  100. v8::Local<v8::Value> val,
  101. atom::AtomMenuModel** out) {
  102. // null would be tranfered to NULL.
  103. if (val->IsNull()) {
  104. *out = nullptr;
  105. return true;
  106. }
  107. atom::api::Menu* menu;
  108. if (!Converter<atom::api::Menu*>::FromV8(isolate, val, &menu))
  109. return false;
  110. *out = menu->model();
  111. return true;
  112. }
  113. };
  114. } // namespace mate
  115. #endif // ATOM_BROWSER_API_ATOM_API_MENU_H_