electron_menu_model.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2015 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 ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
  5. #define ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/files/file_path.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/observer_list_types.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "ui/base/models/simple_menu_model.h"
  16. #include "url/gurl.h"
  17. namespace electron {
  18. class ElectronMenuModel : public ui::SimpleMenuModel {
  19. public:
  20. #if BUILDFLAG(IS_MAC)
  21. struct SharingItem {
  22. SharingItem();
  23. SharingItem(SharingItem&&);
  24. SharingItem(const SharingItem&) = delete;
  25. ~SharingItem();
  26. absl::optional<std::vector<std::string>> texts;
  27. absl::optional<std::vector<GURL>> urls;
  28. absl::optional<std::vector<base::FilePath>> file_paths;
  29. };
  30. #endif
  31. class Delegate : public ui::SimpleMenuModel::Delegate {
  32. public:
  33. ~Delegate() override {}
  34. virtual bool GetAcceleratorForCommandIdWithParams(
  35. int command_id,
  36. bool use_default_accelerator,
  37. ui::Accelerator* accelerator) const = 0;
  38. virtual bool ShouldRegisterAcceleratorForCommandId(
  39. int command_id) const = 0;
  40. virtual bool ShouldCommandIdWorkWhenHidden(int command_id) const = 0;
  41. #if BUILDFLAG(IS_MAC)
  42. virtual bool GetSharingItemForCommandId(int command_id,
  43. SharingItem* item) const = 0;
  44. #endif
  45. private:
  46. // ui::SimpleMenuModel::Delegate:
  47. bool GetAcceleratorForCommandId(
  48. int command_id,
  49. ui::Accelerator* accelerator) const override;
  50. };
  51. class Observer : public base::CheckedObserver {
  52. public:
  53. ~Observer() override {}
  54. // Notifies the menu will open.
  55. virtual void OnMenuWillShow() {}
  56. // Notifies the menu has been closed.
  57. virtual void OnMenuWillClose() {}
  58. };
  59. explicit ElectronMenuModel(Delegate* delegate);
  60. ~ElectronMenuModel() override;
  61. // disable copy
  62. ElectronMenuModel(const ElectronMenuModel&) = delete;
  63. ElectronMenuModel& operator=(const ElectronMenuModel&) = delete;
  64. void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
  65. void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
  66. void SetToolTip(size_t index, const std::u16string& toolTip);
  67. std::u16string GetToolTipAt(size_t index);
  68. void SetRole(size_t index, const std::u16string& role);
  69. std::u16string GetRoleAt(size_t index);
  70. void SetSecondaryLabel(size_t index, const std::u16string& sublabel);
  71. std::u16string GetSecondaryLabelAt(size_t index) const override;
  72. bool GetAcceleratorAtWithParams(size_t index,
  73. bool use_default_accelerator,
  74. ui::Accelerator* accelerator) const;
  75. bool ShouldRegisterAcceleratorAt(size_t index) const;
  76. bool WorksWhenHiddenAt(size_t index) const;
  77. #if BUILDFLAG(IS_MAC)
  78. // Return the SharingItem of menu item.
  79. bool GetSharingItemAt(size_t index, SharingItem* item) const;
  80. // Set/Get the SharingItem of this menu.
  81. void SetSharingItem(SharingItem item);
  82. const absl::optional<SharingItem>& GetSharingItem() const;
  83. #endif
  84. // ui::SimpleMenuModel:
  85. void MenuWillClose() override;
  86. void MenuWillShow() override;
  87. base::WeakPtr<ElectronMenuModel> GetWeakPtr() {
  88. return weak_factory_.GetWeakPtr();
  89. }
  90. using SimpleMenuModel::GetSubmenuModelAt;
  91. ElectronMenuModel* GetSubmenuModelAt(size_t index);
  92. private:
  93. raw_ptr<Delegate> delegate_; // weak ref.
  94. #if BUILDFLAG(IS_MAC)
  95. absl::optional<SharingItem> sharing_item_;
  96. #endif
  97. std::map<int, std::u16string> toolTips_; // command id -> tooltip
  98. std::map<int, std::u16string> roles_; // command id -> role
  99. std::map<int, std::u16string> sublabels_; // command id -> sublabel
  100. base::ObserverList<Observer> observers_;
  101. base::WeakPtrFactory<ElectronMenuModel> weak_factory_{this};
  102. };
  103. } // namespace electron
  104. #endif // ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_