atom_menu_model.h 2.3 KB

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