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"
8 #include "base/message_loop/message_loop.h"
9 #include "ui/base/accelerators/accelerator.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/gfx/image/image.h"
13 struct StatusIconMenuModel::ItemState
{
23 ui::Accelerator accelerator
;
25 base::string16 sublabel
;
29 ////////////////////////////////////////////////////////////////////////////////
30 // StatusIconMenuModel::Delegate, public:
32 void StatusIconMenuModel::Delegate::CommandIdHighlighted(int command_id
) {
35 ////////////////////////////////////////////////////////////////////////////////
36 // StatusIconMenuModel, public:
38 StatusIconMenuModel::StatusIconMenuModel(Delegate
* delegate
)
39 : ui::SimpleMenuModel(this), delegate_(delegate
) {
42 StatusIconMenuModel::~StatusIconMenuModel() {
45 void StatusIconMenuModel::SetCommandIdChecked(int command_id
, bool checked
) {
46 item_states_
[command_id
].checked
= checked
;
47 NotifyMenuStateChanged();
50 void StatusIconMenuModel::SetCommandIdEnabled(int command_id
, bool enabled
) {
51 item_states_
[command_id
].enabled
= enabled
;
52 NotifyMenuStateChanged();
55 void StatusIconMenuModel::SetCommandIdVisible(int command_id
, bool visible
) {
56 item_states_
[command_id
].visible
= visible
;
57 NotifyMenuStateChanged();
60 void StatusIconMenuModel::SetAcceleratorForCommandId(
61 int command_id
, const ui::Accelerator
* accelerator
) {
62 item_states_
[command_id
].accelerator
= *accelerator
;
63 NotifyMenuStateChanged();
66 void StatusIconMenuModel::ChangeLabelForCommandId(int command_id
,
67 const base::string16
& label
) {
68 item_states_
[command_id
].is_dynamic
= true;
69 item_states_
[command_id
].label
= label
;
70 NotifyMenuStateChanged();
73 void StatusIconMenuModel::ChangeSublabelForCommandId(
74 int command_id
, const base::string16
& sublabel
) {
75 item_states_
[command_id
].is_dynamic
= true;
76 item_states_
[command_id
].sublabel
= sublabel
;
77 NotifyMenuStateChanged();
80 void StatusIconMenuModel::ChangeIconForCommandId(
81 int command_id
, const gfx::Image
& icon
) {
82 item_states_
[command_id
].is_dynamic
= true;
83 item_states_
[command_id
].icon
= icon
;
84 NotifyMenuStateChanged();
87 void StatusIconMenuModel::AddObserver(Observer
* observer
) {
88 observer_list_
.AddObserver(observer
);
91 void StatusIconMenuModel::RemoveObserver(Observer
* observer
) {
92 observer_list_
.RemoveObserver(observer
);
95 bool StatusIconMenuModel::IsCommandIdChecked(int command_id
) const {
96 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
97 if (iter
!= item_states_
.end())
98 return iter
->second
.checked
;
102 bool StatusIconMenuModel::IsCommandIdEnabled(int command_id
) const {
103 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
104 if (iter
!= item_states_
.end())
105 return iter
->second
.enabled
;
109 bool StatusIconMenuModel::IsCommandIdVisible(int command_id
) const {
110 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
111 if (iter
!= item_states_
.end())
112 return iter
->second
.visible
;
116 bool StatusIconMenuModel::GetAcceleratorForCommandId(
117 int command_id
, ui::Accelerator
* accelerator
) {
118 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
119 if (iter
!= item_states_
.end() &&
120 iter
->second
.accelerator
.key_code() != ui::VKEY_UNKNOWN
) {
121 *accelerator
= iter
->second
.accelerator
;
127 bool StatusIconMenuModel::IsItemForCommandIdDynamic(int command_id
) const {
128 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
129 if (iter
!= item_states_
.end())
130 return iter
->second
.is_dynamic
;
134 base::string16
StatusIconMenuModel::GetLabelForCommandId(int command_id
) const {
135 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
136 if (iter
!= item_states_
.end())
137 return iter
->second
.label
;
138 return base::string16();
141 base::string16
StatusIconMenuModel::GetSublabelForCommandId(
142 int command_id
) const {
143 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
144 if (iter
!= item_states_
.end())
145 return iter
->second
.sublabel
;
146 return base::string16();
149 bool StatusIconMenuModel::GetIconForCommandId(int command_id
,
150 gfx::Image
* image_skia
) const {
151 ItemStateMap::const_iterator iter
= item_states_
.find(command_id
);
152 if (iter
!= item_states_
.end() && !iter
->second
.icon
.IsEmpty()) {
153 *image_skia
= iter
->second
.icon
;
159 ////////////////////////////////////////////////////////////////////////////////
160 // StatusIconMenuModel, protected:
162 void StatusIconMenuModel::MenuItemsChanged() {
163 NotifyMenuStateChanged();
166 void StatusIconMenuModel::NotifyMenuStateChanged() {
167 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnMenuStateChanged());
170 ////////////////////////////////////////////////////////////////////////////////
171 // StatusIconMenuModel, private:
173 void StatusIconMenuModel::CommandIdHighlighted(int command_id
) {
175 delegate_
->CommandIdHighlighted(command_id
);
178 void StatusIconMenuModel::ExecuteCommand(int command_id
, int event_flags
) {
180 delegate_
->ExecuteCommand(command_id
, event_flags
);