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/gfx/image/image.h"
     11 
     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;
     26 };
     27 
     28 ////////////////////////////////////////////////////////////////////////////////
     29 // StatusIconMenuModel::Delegate, public:
     30 
     31 void StatusIconMenuModel::Delegate::CommandIdHighlighted(int command_id) {
     32 }
     33 
     34 ////////////////////////////////////////////////////////////////////////////////
     35 // StatusIconMenuModel, public:
     36 
     37 StatusIconMenuModel::StatusIconMenuModel(Delegate* delegate)
     38     : ui::SimpleMenuModel(this), delegate_(delegate) {
     39 }
     40 
     41 StatusIconMenuModel::~StatusIconMenuModel() {
     42 }
     43 
     44 void StatusIconMenuModel::SetCommandIdChecked(int command_id, bool checked) {
     45   item_states_[command_id].checked = checked;
     46   NotifyMenuStateChanged();
     47 }
     48 
     49 void StatusIconMenuModel::SetCommandIdEnabled(int command_id, bool enabled) {
     50   item_states_[command_id].enabled = enabled;
     51   NotifyMenuStateChanged();
     52 }
     53 
     54 void StatusIconMenuModel::SetCommandIdVisible(int command_id, bool visible) {
     55   item_states_[command_id].visible = visible;
     56   NotifyMenuStateChanged();
     57 }
     58 
     59 void StatusIconMenuModel::SetAcceleratorForCommandId(
     60     int command_id, const ui::Accelerator* accelerator) {
     61   item_states_[command_id].accelerator = *accelerator;
     62   NotifyMenuStateChanged();
     63 }
     64 
     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 
     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 
     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 
     86 void StatusIconMenuModel::AddObserver(Observer* observer) {
     87   observer_list_.AddObserver(observer);
     88 }
     89 
     90 void StatusIconMenuModel::RemoveObserver(Observer* observer) {
     91   observer_list_.RemoveObserver(observer);
     92 }
     93 
     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;
     99 }
    100 
    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;
    106 }
    107 
    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;
    113 }
    114 
    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 
    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;
    131 }
    132 
    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();
    138 }
    139 
    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();
    146 }
    147 
    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;
    154   }
    155   return false;
    156 }
    157 
    158 ////////////////////////////////////////////////////////////////////////////////
    159 // StatusIconMenuModel, protected:
    160 
    161 void StatusIconMenuModel::MenuItemsChanged() {
    162   NotifyMenuStateChanged();
    163 }
    164 
    165 void StatusIconMenuModel::NotifyMenuStateChanged() {
    166   FOR_EACH_OBSERVER(Observer, observer_list_, OnMenuStateChanged());
    167 }
    168 
    169 ////////////////////////////////////////////////////////////////////////////////
    170 // StatusIconMenuModel, private:
    171 
    172 void StatusIconMenuModel::CommandIdHighlighted(int command_id) {
    173   if (delegate_)
    174     delegate_->CommandIdHighlighted(command_id);
    175 }
    176 
    177 void StatusIconMenuModel::ExecuteCommand(int command_id, int event_flags) {
    178   if (delegate_)
    179     delegate_->ExecuteCommand(command_id, event_flags);
    180 }
    181