atom_api_menu.h 4.3 KB

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