atom_api_menu.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_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:
  17. static mate::WrappableBase* New(mate::Arguments* args);
  18. static void BuildPrototype(v8::Isolate* isolate,
  19. v8::Local<v8::FunctionTemplate> prototype);
  20. #if defined(OS_MACOSX)
  21. // Set the global menubar.
  22. static void SetApplicationMenu(Menu* menu);
  23. // Fake sending an action from the application menu.
  24. static void SendActionToFirstResponder(const std::string& action);
  25. #endif
  26. AtomMenuModel* model() const { return model_.get(); }
  27. protected:
  28. Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
  29. ~Menu() override;
  30. // mate::Wrappable:
  31. void AfterInit(v8::Isolate* isolate) override;
  32. // ui::SimpleMenuModel::Delegate:
  33. bool IsCommandIdChecked(int command_id) const override;
  34. bool IsCommandIdEnabled(int command_id) const override;
  35. bool IsCommandIdVisible(int command_id) const override;
  36. bool GetAcceleratorForCommandIdWithParams(
  37. int command_id,
  38. bool use_default_accelerator,
  39. ui::Accelerator* accelerator) const override;
  40. void ExecuteCommand(int command_id, int event_flags) override;
  41. void MenuWillShow(ui::SimpleMenuModel* source) override;
  42. virtual void PopupAt(Window* window,
  43. int x = -1, int y = -1,
  44. int positioning_item = 0) = 0;
  45. std::unique_ptr<AtomMenuModel> model_;
  46. Menu* parent_;
  47. private:
  48. void InsertItemAt(int index, int command_id, const base::string16& label);
  49. void InsertSeparatorAt(int index);
  50. void InsertCheckItemAt(int index,
  51. int command_id,
  52. const base::string16& label);
  53. void InsertRadioItemAt(int index,
  54. int command_id,
  55. const base::string16& label,
  56. int group_id);
  57. void InsertSubMenuAt(int index,
  58. int command_id,
  59. const base::string16& label,
  60. Menu* menu);
  61. void SetIcon(int index, const gfx::Image& image);
  62. void SetSublabel(int index, const base::string16& sublabel);
  63. void SetRole(int index, const base::string16& role);
  64. void Clear();
  65. int GetIndexOfCommandId(int command_id);
  66. int GetItemCount() const;
  67. int GetCommandIdAt(int index) const;
  68. base::string16 GetLabelAt(int index) const;
  69. base::string16 GetSublabelAt(int index) const;
  70. bool IsItemCheckedAt(int index) const;
  71. bool IsEnabledAt(int index) const;
  72. bool IsVisibleAt(int index) const;
  73. // Stored delegate methods.
  74. base::Callback<bool(int)> is_checked_;
  75. base::Callback<bool(int)> is_enabled_;
  76. base::Callback<bool(int)> is_visible_;
  77. base::Callback<v8::Local<v8::Value>(int, bool)> get_accelerator_;
  78. base::Callback<void(v8::Local<v8::Value>, int)> execute_command_;
  79. base::Callback<void()> menu_will_show_;
  80. DISALLOW_COPY_AND_ASSIGN(Menu);
  81. };
  82. } // namespace api
  83. } // namespace atom
  84. namespace mate {
  85. template<>
  86. struct Converter<atom::AtomMenuModel*> {
  87. static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
  88. atom::AtomMenuModel** out) {
  89. // null would be tranfered to NULL.
  90. if (val->IsNull()) {
  91. *out = nullptr;
  92. return true;
  93. }
  94. atom::api::Menu* menu;
  95. if (!Converter<atom::api::Menu*>::FromV8(isolate, val, &menu))
  96. return false;
  97. *out = menu->model();
  98. return true;
  99. }
  100. };
  101. } // namespace mate
  102. #endif // ATOM_BROWSER_API_ATOM_API_MENU_H_