Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / status_icons / status_icon_menu_model.cc
blobeee644ecedc7960ffb009b151f92d1efbe085794
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 #include "chrome/browser/status_icons/status_icon_menu_model.h"
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"
12 struct StatusIconMenuModel::ItemState {
13 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;
28 ////////////////////////////////////////////////////////////////////////////////
29 // StatusIconMenuModel::Delegate, public:
31 void StatusIconMenuModel::Delegate::CommandIdHighlighted(int command_id) {
34 ////////////////////////////////////////////////////////////////////////////////
35 // StatusIconMenuModel, public:
37 StatusIconMenuModel::StatusIconMenuModel(Delegate* delegate)
38 : ui::SimpleMenuModel(this), delegate_(delegate) {
41 StatusIconMenuModel::~StatusIconMenuModel() {
44 void StatusIconMenuModel::SetCommandIdChecked(int command_id, bool checked) {
45 item_states_[command_id].checked = checked;
46 NotifyMenuStateChanged();
49 void StatusIconMenuModel::SetCommandIdEnabled(int command_id, bool enabled) {
50 item_states_[command_id].enabled = enabled;
51 NotifyMenuStateChanged();
54 void StatusIconMenuModel::SetCommandIdVisible(int command_id, bool visible) {
55 item_states_[command_id].visible = visible;
56 NotifyMenuStateChanged();
59 void StatusIconMenuModel::SetAcceleratorForCommandId(
60 int command_id, const ui::Accelerator* accelerator) {
61 item_states_[command_id].accelerator = *accelerator;
62 NotifyMenuStateChanged();
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();
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();
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();
86 void StatusIconMenuModel::AddObserver(Observer* observer) {
87 observer_list_.AddObserver(observer);
90 void StatusIconMenuModel::RemoveObserver(Observer* observer) {
91 observer_list_.RemoveObserver(observer);
94 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;
101 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;
108 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;
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;
123 return false;
126 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;
133 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();
140 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();
148 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;
155 return false;
158 ////////////////////////////////////////////////////////////////////////////////
159 // StatusIconMenuModel, protected:
161 void StatusIconMenuModel::MenuItemsChanged() {
162 NotifyMenuStateChanged();
165 void StatusIconMenuModel::NotifyMenuStateChanged() {
166 FOR_EACH_OBSERVER(Observer, observer_list_, OnMenuStateChanged());
169 ////////////////////////////////////////////////////////////////////////////////
170 // StatusIconMenuModel, private:
172 void StatusIconMenuModel::CommandIdHighlighted(int command_id) {
173 if (delegate_)
174 delegate_->CommandIdHighlighted(command_id);
177 void StatusIconMenuModel::ExecuteCommand(int command_id, int event_flags) {
178 if (delegate_)
179 delegate_->ExecuteCommand(command_id, event_flags);