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.
4
5 #include "chrome/browser/status_icons/status_icon_menu_model.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "ui/base/accelerators/accelerator.h"
10 #include "ui/gfx/image/image.h"
11
12 struct StatusIconMenuModel::ItemState {
ItemStateStatusIconMenuModel::ItemState13 ItemState()
14 : checked(false),
15 enabled(true),
16 visible(true),
17 is_dynamic(false) {}
18 bool checked;
19 bool enabled;
20 bool visible;
21 bool is_dynamic;
22 ui::Accelerator accelerator;
23 base::string16 label;
24 base::string16 sublabel;
25 gfx::Image icon;
26 };
27
28 ////////////////////////////////////////////////////////////////////////////////
29 // StatusIconMenuModel::Delegate, public:
30
CommandIdHighlighted(int command_id)31 void StatusIconMenuModel::Delegate::CommandIdHighlighted(int command_id) {
32 }
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // StatusIconMenuModel, public:
36
StatusIconMenuModel(Delegate * delegate)37 StatusIconMenuModel::StatusIconMenuModel(Delegate* delegate)
38 : ui::SimpleMenuModel(this), delegate_(delegate) {
39 }
40
~StatusIconMenuModel()41 StatusIconMenuModel::~StatusIconMenuModel() {
42 }
43
SetCommandIdChecked(int command_id,bool checked)44 void StatusIconMenuModel::SetCommandIdChecked(int command_id, bool checked) {
45 item_states_[command_id].checked = checked;
46 NotifyMenuStateChanged();
47 }
48
SetCommandIdEnabled(int command_id,bool enabled)49 void StatusIconMenuModel::SetCommandIdEnabled(int command_id, bool enabled) {
50 item_states_[command_id].enabled = enabled;
51 NotifyMenuStateChanged();
52 }
53
SetCommandIdVisible(int command_id,bool visible)54 void StatusIconMenuModel::SetCommandIdVisible(int command_id, bool visible) {
55 item_states_[command_id].visible = visible;
56 NotifyMenuStateChanged();
57 }
58
SetAcceleratorForCommandId(int command_id,const ui::Accelerator * accelerator)59 void StatusIconMenuModel::SetAcceleratorForCommandId(
60 int command_id, const ui::Accelerator* accelerator) {
61 item_states_[command_id].accelerator = *accelerator;
62 NotifyMenuStateChanged();
63 }
64
ChangeLabelForCommandId(int command_id,const base::string16 & label)65 void StatusIconMenuModel::ChangeLabelForCommandId(int command_id,
66 const base::string16& label) {
67 item_states_[command_id].is_dynamic = true;
68 item_states_[command_id].label = label;
69 NotifyMenuStateChanged();
70 }
71
ChangeSublabelForCommandId(int command_id,const base::string16 & sublabel)72 void StatusIconMenuModel::ChangeSublabelForCommandId(
73 int command_id, const base::string16& sublabel) {
74 item_states_[command_id].is_dynamic = true;
75 item_states_[command_id].sublabel = sublabel;
76 NotifyMenuStateChanged();
77 }
78
ChangeIconForCommandId(int command_id,const gfx::Image & icon)79 void StatusIconMenuModel::ChangeIconForCommandId(
80 int command_id, const gfx::Image& icon) {
81 item_states_[command_id].is_dynamic = true;
82 item_states_[command_id].icon = icon;
83 NotifyMenuStateChanged();
84 }
85
AddObserver(Observer * observer)86 void StatusIconMenuModel::AddObserver(Observer* observer) {
87 observer_list_.AddObserver(observer);
88 }
89
RemoveObserver(Observer * observer)90 void StatusIconMenuModel::RemoveObserver(Observer* observer) {
91 observer_list_.RemoveObserver(observer);
92 }
93
IsCommandIdChecked(int command_id) const94 bool StatusIconMenuModel::IsCommandIdChecked(int command_id) const {
95 ItemStateMap::const_iterator iter = item_states_.find(command_id);
96 if (iter != item_states_.end())
97 return iter->second.checked;
98 return false;
99 }
100
IsCommandIdEnabled(int command_id) const101 bool StatusIconMenuModel::IsCommandIdEnabled(int command_id) const {
102 ItemStateMap::const_iterator iter = item_states_.find(command_id);
103 if (iter != item_states_.end())
104 return iter->second.enabled;
105 return true;
106 }
107
IsCommandIdVisible(int command_id) const108 bool StatusIconMenuModel::IsCommandIdVisible(int command_id) const {
109 ItemStateMap::const_iterator iter = item_states_.find(command_id);
110 if (iter != item_states_.end())
111 return iter->second.visible;
112 return true;
113 }
114
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator)115 bool StatusIconMenuModel::GetAcceleratorForCommandId(
116 int command_id, ui::Accelerator* accelerator) {
117 ItemStateMap::const_iterator iter = item_states_.find(command_id);
118 if (iter != item_states_.end() &&
119 iter->second.accelerator.key_code() != ui::VKEY_UNKNOWN) {
120 *accelerator = iter->second.accelerator;
121 return true;
122 }
123 return false;
124 }
125
IsItemForCommandIdDynamic(int command_id) const126 bool StatusIconMenuModel::IsItemForCommandIdDynamic(int command_id) const {
127 ItemStateMap::const_iterator iter = item_states_.find(command_id);
128 if (iter != item_states_.end())
129 return iter->second.is_dynamic;
130 return false;
131 }
132
GetLabelForCommandId(int command_id) const133 base::string16 StatusIconMenuModel::GetLabelForCommandId(int command_id) const {
134 ItemStateMap::const_iterator iter = item_states_.find(command_id);
135 if (iter != item_states_.end())
136 return iter->second.label;
137 return base::string16();
138 }
139
GetSublabelForCommandId(int command_id) const140 base::string16 StatusIconMenuModel::GetSublabelForCommandId(
141 int command_id) const {
142 ItemStateMap::const_iterator iter = item_states_.find(command_id);
143 if (iter != item_states_.end())
144 return iter->second.sublabel;
145 return base::string16();
146 }
147
GetIconForCommandId(int command_id,gfx::Image * image_skia) const148 bool StatusIconMenuModel::GetIconForCommandId(int command_id,
149 gfx::Image* image_skia) const {
150 ItemStateMap::const_iterator iter = item_states_.find(command_id);
151 if (iter != item_states_.end() && !iter->second.icon.IsEmpty()) {
152 *image_skia = iter->second.icon;
153 return true;
154 }
155 return false;
156 }
157
158 ////////////////////////////////////////////////////////////////////////////////
159 // StatusIconMenuModel, protected:
160
MenuItemsChanged()161 void StatusIconMenuModel::MenuItemsChanged() {
162 NotifyMenuStateChanged();
163 }
164
NotifyMenuStateChanged()165 void StatusIconMenuModel::NotifyMenuStateChanged() {
166 FOR_EACH_OBSERVER(Observer, observer_list_, OnMenuStateChanged());
167 }
168
169 ////////////////////////////////////////////////////////////////////////////////
170 // StatusIconMenuModel, private:
171
CommandIdHighlighted(int command_id)172 void StatusIconMenuModel::CommandIdHighlighted(int command_id) {
173 if (delegate_)
174 delegate_->CommandIdHighlighted(command_id);
175 }
176
ExecuteCommand(int command_id,int event_flags)177 void StatusIconMenuModel::ExecuteCommand(int command_id, int event_flags) {
178 if (delegate_)
179 delegate_->ExecuteCommand(command_id, event_flags);
180 }
181