atom_api_menu.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 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. const base::Closure& 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. private:
  57. void InsertItemAt(int index, int command_id, const base::string16& label);
  58. void InsertSeparatorAt(int index);
  59. void InsertCheckItemAt(int index,
  60. int command_id,
  61. const base::string16& label);
  62. void InsertRadioItemAt(int index,
  63. int command_id,
  64. const base::string16& label,
  65. int group_id);
  66. void InsertSubMenuAt(int index,
  67. int command_id,
  68. const base::string16& label,
  69. Menu* menu);
  70. void SetIcon(int index, const gfx::Image& image);
  71. void SetSublabel(int index, const base::string16& sublabel);
  72. void SetRole(int index, const base::string16& role);
  73. void Clear();
  74. int GetIndexOfCommandId(int command_id);
  75. int GetItemCount() const;
  76. int GetCommandIdAt(int index) const;
  77. base::string16 GetLabelAt(int index) const;
  78. base::string16 GetSublabelAt(int index) const;
  79. base::string16 GetAcceleratorTextAt(int index) const;
  80. bool IsItemCheckedAt(int index) const;
  81. bool IsEnabledAt(int index) const;
  82. bool IsVisibleAt(int index) const;
  83. bool WorksWhenHiddenAt(int index) const;
  84. // Stored delegate methods.
  85. base::Callback<bool(v8::Local<v8::Value>, int)> is_checked_;
  86. base::Callback<bool(v8::Local<v8::Value>, int)> is_enabled_;
  87. base::Callback<bool(v8::Local<v8::Value>, int)> is_visible_;
  88. base::Callback<bool(v8::Local<v8::Value>, int)> works_when_hidden_;
  89. base::Callback<v8::Local<v8::Value>(v8::Local<v8::Value>, int, bool)>
  90. get_accelerator_;
  91. base::Callback<bool(v8::Local<v8::Value>, int)> should_register_accelerator_;
  92. base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>, int)>
  93. execute_command_;
  94. base::Callback<void(v8::Local<v8::Value>)> menu_will_show_;
  95. DISALLOW_COPY_AND_ASSIGN(Menu);
  96. };
  97. } // namespace api
  98. } // namespace atom
  99. namespace mate {
  100. template <>
  101. struct Converter<atom::AtomMenuModel*> {
  102. static bool FromV8(v8::Isolate* isolate,
  103. v8::Local<v8::Value> val,
  104. atom::AtomMenuModel** out) {
  105. // null would be tranfered to NULL.
  106. if (val->IsNull()) {
  107. *out = nullptr;
  108. return true;
  109. }
  110. atom::api::Menu* menu;
  111. if (!Converter<atom::api::Menu*>::FromV8(isolate, val, &menu))
  112. return false;
  113. *out = menu->model();
  114. return true;
  115. }
  116. };
  117. } // namespace mate
  118. #endif // ATOM_BROWSER_API_ATOM_API_MENU_H_