Home | History | Annotate | Download | only in status_icons
      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/base/l10n/l10n_util.h"
     11 #include "ui/gfx/image/image.h"
     12 
     13 struct StatusIconMenuModel::ItemState {
     14   ItemState()
     15       : checked(false),
     16         enabled(true),
     17         visible(true),
     18         is_dynamic(false) {}
     19   bool checked;
     20   bool enabled;
     21   bool visible;
     22   bool is_dynamic;
     23   ui::Accelerator accelerator;
     24   base::string16 label;
     25   base::string16 sublabel;
     26   gfx::Image icon;
     27 };
     28 
     29 ////////////////////////////////////////////////////////////////////////////////
     30 // StatusIconMenuModel::Delegate, public:
     31 
     32 void StatusIconMenuModel::Delegate::CommandIdHighlighted(int command_id) {
     33 }
     34 
     35 ////////////////////////////////////////////////////////////////////////////////
     36 // StatusIconMenuModel, public:
     37 
     38 StatusIconMenuModel::StatusIconMenuModel(Delegate* delegate)
     39     : ui::SimpleMenuModel(this), delegate_(delegate) {
     40 }
     41 
     42 StatusIconMenuModel::~StatusIconMenuModel() {
     43 }
     44 
     45 void StatusIconMenuModel::SetCommandIdChecked(int command_id, bool checked) {
     46   item_states_[command_id].checked = checked;
     47   NotifyMenuStateChanged();
     48 }
     49 
     50 void StatusIconMenuModel::SetCommandIdEnabled(int command_id, bool enabled) {
     51   item_states_[command_id].enabled = enabled;
     52   NotifyMenuStateChanged();
     53 }
     54 
     55 void StatusIconMenuModel::SetCommandIdVisible(int command_id, bool visible) {
     56   item_states_[command_id].visible = visible;
     57   NotifyMenuStateChanged();
     58 }
     59 
     60 void StatusIconMenuModel::SetAcceleratorForCommandId(
     61     int command_id, const ui::Accelerator* accelerator) {
     62   item_states_[command_id].accelerator = *accelerator;
     63   NotifyMenuStateChanged();
     64 }
     65 
     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();
     71 }
     72 
     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();
     78 }
     79 
     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();
     85 }
     86 
     87 void StatusIconMenuModel::AddObserver(Observer* observer) {
     88   observer_list_.AddObserver(observer);
     89 }
     90 
     91 void StatusIconMenuModel::RemoveObserver(Observer* observer) {
     92   observer_list_.RemoveObserver(observer);
     93 }
     94 
     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;
     99   return false;
    100 }
    101 
    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;
    106   return true;
    107 }
    108 
    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;
    113   return true;
    114 }
    115 
    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;
    122     return true;
    123   }
    124   return false;
    125 }
    126 
    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;
    131   return false;
    132 }
    133 
    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();
    139 }
    140 
    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();
    147 }
    148 
    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;
    154     return true;
    155   }
    156   return false;
    157 }
    158 
    159 ////////////////////////////////////////////////////////////////////////////////
    160 // StatusIconMenuModel, protected:
    161 
    162 void StatusIconMenuModel::MenuItemsChanged() {
    163   NotifyMenuStateChanged();
    164 }
    165 
    166 void StatusIconMenuModel::NotifyMenuStateChanged() {
    167   FOR_EACH_OBSERVER(Observer, observer_list_, OnMenuStateChanged());
    168 }
    169 
    170 ////////////////////////////////////////////////////////////////////////////////
    171 // StatusIconMenuModel, private:
    172 
    173 void StatusIconMenuModel::CommandIdHighlighted(int command_id) {
    174   if (delegate_)
    175     delegate_->CommandIdHighlighted(command_id);
    176 }
    177 
    178 void StatusIconMenuModel::ExecuteCommand(int command_id, int event_flags) {
    179   if (delegate_)
    180     delegate_->ExecuteCommand(command_id, event_flags);
    181 }
    182