atom_menu_model.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_
  5. #define ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_
  6. #include <map>
  7. #include "base/observer_list.h"
  8. #include "base/observer_list_types.h"
  9. #include "ui/base/models/simple_menu_model.h"
  10. namespace atom {
  11. class AtomMenuModel : public ui::SimpleMenuModel {
  12. public:
  13. class Delegate : public ui::SimpleMenuModel::Delegate {
  14. public:
  15. ~Delegate() override {}
  16. virtual bool GetAcceleratorForCommandIdWithParams(
  17. int command_id,
  18. bool use_default_accelerator,
  19. ui::Accelerator* accelerator) const = 0;
  20. virtual bool ShouldRegisterAcceleratorForCommandId(
  21. int command_id) const = 0;
  22. private:
  23. // ui::SimpleMenuModel::Delegate:
  24. bool GetAcceleratorForCommandId(
  25. int command_id,
  26. ui::Accelerator* accelerator) const override;
  27. };
  28. class Observer : public base::CheckedObserver {
  29. public:
  30. ~Observer() override {}
  31. // Notifies the menu will open.
  32. virtual void OnMenuWillShow() {}
  33. // Notifies the menu has been closed.
  34. virtual void OnMenuWillClose() {}
  35. };
  36. explicit AtomMenuModel(Delegate* delegate);
  37. ~AtomMenuModel() override;
  38. void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
  39. void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
  40. void SetRole(int index, const base::string16& role);
  41. base::string16 GetRoleAt(int index);
  42. bool GetAcceleratorAtWithParams(int index,
  43. bool use_default_accelerator,
  44. ui::Accelerator* accelerator) const;
  45. bool ShouldRegisterAcceleratorAt(int index) const;
  46. // ui::SimpleMenuModel:
  47. void MenuWillClose() override;
  48. void MenuWillShow() override;
  49. using SimpleMenuModel::GetSubmenuModelAt;
  50. AtomMenuModel* GetSubmenuModelAt(int index);
  51. private:
  52. Delegate* delegate_; // weak ref.
  53. std::map<int, base::string16> roles_; // command id -> role
  54. base::ObserverList<Observer> observers_;
  55. DISALLOW_COPY_AND_ASSIGN(AtomMenuModel);
  56. };
  57. } // namespace atom
  58. #endif // ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_