atom_menu_model.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. ~Delegate() override {}
  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(
  22. int command_id,
  23. ui::Accelerator* accelerator) const override;
  24. };
  25. class Observer {
  26. public:
  27. virtual ~Observer() {}
  28. // Notifies the menu will open.
  29. virtual void OnMenuWillShow() {}
  30. // Notifies the menu has been closed.
  31. virtual void OnMenuWillClose() {}
  32. };
  33. explicit AtomMenuModel(Delegate* delegate);
  34. ~AtomMenuModel() override;
  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 MenuWillClose() override;
  44. void MenuWillShow() override;
  45. using SimpleMenuModel::GetSubmenuModelAt;
  46. AtomMenuModel* GetSubmenuModelAt(int index);
  47. private:
  48. Delegate* delegate_; // weak ref.
  49. std::map<int, base::string16> roles_; // command id -> role
  50. base::ObserverList<Observer> observers_;
  51. DISALLOW_COPY_AND_ASSIGN(AtomMenuModel);
  52. };
  53. } // namespace atom
  54. #endif // ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_