Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / status_icons / status_icon_menu_model.h
blob748142062b46b3da58007e39ec09013f41394000
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_MENU_MODEL_H_
6 #define CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_MENU_MODEL_H_
8 #include <map>
10 #include "base/memory/weak_ptr.h"
11 #include "base/observer_list.h"
12 #include "ui/base/models/simple_menu_model.h"
14 namespace gfx {
15 class Image;
18 class StatusIconMenuModelTest;
20 // StatusIconMenuModel contains the state of the SimpleMenuModel as well as that
21 // of its delegate. This is done so that we can easily identify when the menu
22 // model state has changed and can tell the status icon to update the menu. This
23 // is necessary some platforms which do not notify us before showing the menu
24 // (like Ubuntu Unity).
25 class StatusIconMenuModel
26 : public ui::SimpleMenuModel,
27 public ui::SimpleMenuModel::Delegate,
28 public base::SupportsWeakPtr<StatusIconMenuModel> {
29 public:
30 class Delegate {
31 public:
32 // Notifies the delegate that the item with the specified command id was
33 // visually highlighted within the menu.
34 virtual void CommandIdHighlighted(int command_id);
36 // Performs the action associates with the specified command id.
37 // The passed |event_flags| are the flags from the event which issued this
38 // command and they can be examined to find modifier keys.
39 virtual void ExecuteCommand(int command_id, int event_flags) = 0;
41 protected:
42 virtual ~Delegate() {}
45 class Observer {
46 public:
47 // Invoked when the menu model has changed.
48 virtual void OnMenuStateChanged() {}
50 protected:
51 virtual ~Observer() {}
54 // The Delegate can be NULL.
55 explicit StatusIconMenuModel(Delegate* delegate);
56 ~StatusIconMenuModel() override;
58 // Methods for seting the state of specific command ids.
59 void SetCommandIdChecked(int command_id, bool checked);
60 void SetCommandIdEnabled(int command_id, bool enabled);
61 void SetCommandIdVisible(int command_id, bool visible);
63 // Sets the accelerator for the specified command id.
64 void SetAcceleratorForCommandId(
65 int command_id, const ui::Accelerator* accelerator);
67 // Calling any of these "change" methods will mark the menu item as "dynamic"
68 // (see menu_model.h:IsItemDynamicAt) which many platforms take as a cue to
69 // refresh the label, sublabel and icon of the menu item each time the menu is
70 // shown.
71 void ChangeLabelForCommandId(int command_id, const base::string16& label);
72 void ChangeSublabelForCommandId(
73 int command_id, const base::string16& sublabel);
74 void ChangeIconForCommandId(int command_id, const gfx::Image& icon);
76 void AddObserver(Observer* observer);
77 void RemoveObserver(Observer* observer);
79 // Overridden from ui::SimpleMenuModel::Delegate:
80 bool IsCommandIdChecked(int command_id) const override;
81 bool IsCommandIdEnabled(int command_id) const override;
82 bool IsCommandIdVisible(int command_id) const override;
83 bool GetAcceleratorForCommandId(int command_id,
84 ui::Accelerator* accelerator) override;
85 bool IsItemForCommandIdDynamic(int command_id) const override;
86 base::string16 GetLabelForCommandId(int command_id) const override;
87 base::string16 GetSublabelForCommandId(int command_id) const override;
88 bool GetIconForCommandId(int command_id, gfx::Image* icon) const override;
90 protected:
91 // Overriden from ui::SimpleMenuModel:
92 void MenuItemsChanged() override;
94 void NotifyMenuStateChanged();
96 void set_delegate(Delegate* delegate) { delegate_ = delegate; }
97 Delegate* delegate() { return delegate_; }
99 private:
100 // Overridden from ui::SimpleMenuModel::Delegate:
101 void CommandIdHighlighted(int command_id) override;
102 void ExecuteCommand(int command_id, int event_flags) override;
104 struct ItemState;
106 // Map the properties to the command id (used as key).
107 typedef std::map<int, ItemState> ItemStateMap;
109 ItemStateMap item_states_;
111 base::ObserverList<Observer> observer_list_;
113 Delegate* delegate_;
115 DISALLOW_COPY_AND_ASSIGN(StatusIconMenuModel);
118 #endif // CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_MENU_MODEL_H_