atom_menu_model.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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) {
  23. return GetAcceleratorForCommandIdWithParams(
  24. command_id, false, accelerator);
  25. }
  26. };
  27. class Observer {
  28. public:
  29. virtual ~Observer() {}
  30. // Notifies the menu has been closed.
  31. virtual void MenuClosed() {}
  32. };
  33. explicit AtomMenuModel(Delegate* delegate);
  34. virtual ~AtomMenuModel();
  35. void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
  36. void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
  37. void SetRole(int index, const base::string16& role);
  38. base::string16 GetRoleAt(int index);
  39. bool GetAcceleratorAtWithParams(int index,
  40. bool use_default_accelerator,
  41. ui::Accelerator* accelerator) const;
  42. // ui::SimpleMenuModel:
  43. void MenuClosed() override;
  44. using SimpleMenuModel::GetSubmenuModelAt;
  45. AtomMenuModel* GetSubmenuModelAt(int index);
  46. private:
  47. Delegate* delegate_; // weak ref.
  48. std::map<int, base::string16> roles_; // command id -> role
  49. base::ObserverList<Observer> observers_;
  50. DISALLOW_COPY_AND_ASSIGN(AtomMenuModel);
  51. };
  52. } // namespace atom
  53. #endif // ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_