atom_menu_model.h 2.1 KB

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