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 "ui/message_center/views/message_center_button_bar.h" 6 7 #include "grit/ui_resources.h" 8 #include "grit/ui_strings.h" 9 #include "ui/base/l10n/l10n_util.h" 10 #include "ui/base/models/simple_menu_model.h" 11 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/text_constants.h" 14 #include "ui/message_center/message_center.h" 15 #include "ui/message_center/message_center_style.h" 16 #include "ui/message_center/notifier_settings.h" 17 #include "ui/message_center/views/message_center_view.h" 18 #include "ui/views/controls/button/button.h" 19 #include "ui/views/controls/button/image_button.h" 20 #include "ui/views/controls/button/label_button.h" 21 #include "ui/views/controls/button/menu_button.h" 22 #include "ui/views/controls/button/menu_button_listener.h" 23 #include "ui/views/controls/label.h" 24 #include "ui/views/controls/menu/menu_runner.h" 25 #include "ui/views/layout/box_layout.h" 26 #include "ui/views/layout/grid_layout.h" 27 #include "ui/views/painter.h" 28 29 namespace message_center { 30 31 namespace { 32 const int kButtonSize = 40; 33 const int kChevronWidth = 8; 34 const int kFooterTopMargin = 6; 35 const int kFooterBottomMargin = 3; 36 const int kFooterLeftMargin = 20; 37 const int kFooterRightMargin = 14; 38 } // namespace 39 40 // NotificationCenterButton //////////////////////////////////////////////////// 41 //////////////////////////////////////////////////////////////////////////////// 42 class NotificationCenterButton : public views::ToggleImageButton { 43 public: 44 NotificationCenterButton(views::ButtonListener* listener, 45 int normal_id, 46 int hover_id, 47 int pressed_id, 48 int text_id); 49 void set_size(gfx::Size size) { size_ = size; } 50 51 protected: 52 // Overridden from views::View: 53 virtual gfx::Size GetPreferredSize() OVERRIDE; 54 55 private: 56 gfx::Size size_; 57 58 DISALLOW_COPY_AND_ASSIGN(NotificationCenterButton); 59 }; 60 61 NotificationCenterButton::NotificationCenterButton( 62 views::ButtonListener* listener, 63 int normal_id, 64 int hover_id, 65 int pressed_id, 66 int text_id) 67 : views::ToggleImageButton(listener), size_(kButtonSize, kButtonSize) { 68 ui::ResourceBundle& resource_bundle = ui::ResourceBundle::GetSharedInstance(); 69 SetImage(STATE_NORMAL, resource_bundle.GetImageSkiaNamed(normal_id)); 70 SetImage(STATE_HOVERED, resource_bundle.GetImageSkiaNamed(hover_id)); 71 SetImage(STATE_PRESSED, resource_bundle.GetImageSkiaNamed(pressed_id)); 72 SetImageAlignment(views::ImageButton::ALIGN_CENTER, 73 views::ImageButton::ALIGN_MIDDLE); 74 if (text_id) 75 SetTooltipText(resource_bundle.GetLocalizedString(text_id)); 76 77 SetFocusable(true); 78 set_request_focus_on_press(false); 79 80 SetFocusPainter(views::Painter::CreateSolidFocusPainter( 81 kFocusBorderColor, 82 gfx::Insets(1, 2, 2, 2))); 83 } 84 85 gfx::Size NotificationCenterButton::GetPreferredSize() { return size_; } 86 87 // MessageCenterButtonBar ///////////////////////////////////////////////////// 88 /////////////////////////////////////////////////////////////////////////////// 89 MessageCenterButtonBar::MessageCenterButtonBar( 90 MessageCenterView* message_center_view, 91 MessageCenter* message_center, 92 NotifierSettingsProvider* notifier_settings_provider, 93 bool settings_initially_visible) 94 : message_center_view_(message_center_view), 95 message_center_(message_center), 96 title_arrow_(NULL), 97 notification_label_(NULL), 98 button_container_(NULL), 99 close_all_button_(NULL), 100 settings_button_(NULL), 101 quiet_mode_button_(NULL) { 102 if (get_use_acceleration_when_possible()) 103 SetPaintToLayer(true); 104 set_background( 105 views::Background::CreateSolidBackground(kMessageCenterBackgroundColor)); 106 107 ui::ResourceBundle& resource_bundle = ui::ResourceBundle::GetSharedInstance(); 108 109 title_arrow_ = new NotificationCenterButton(this, 110 IDR_NOTIFICATION_ARROW, 111 IDR_NOTIFICATION_ARROW_HOVER, 112 IDR_NOTIFICATION_ARROW_PRESSED, 113 0); 114 title_arrow_->set_size(gfx::Size(kChevronWidth, kButtonSize)); 115 116 // Keyboardists can use the gear button to switch modes. 117 title_arrow_->SetFocusable(false); 118 AddChildView(title_arrow_); 119 120 gfx::Font notification_label_font = 121 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); 122 notification_label_ = new views::Label( 123 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_FOOTER_TITLE), 124 notification_label_font); 125 notification_label_->SetAutoColorReadabilityEnabled(false); 126 notification_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 127 notification_label_->SetEnabledColor(kRegularTextColor); 128 AddChildView(notification_label_); 129 130 button_container_ = new views::View; 131 button_container_->SetLayoutManager( 132 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); 133 quiet_mode_button_ = new NotificationCenterButton( 134 this, 135 IDR_NOTIFICATION_DO_NOT_DISTURB, 136 IDR_NOTIFICATION_DO_NOT_DISTURB_HOVER, 137 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED, 138 IDS_MESSAGE_CENTER_QUIET_MODE_BUTTON_TOOLTIP); 139 quiet_mode_button_->SetToggledImage( 140 views::Button::STATE_NORMAL, 141 resource_bundle.GetImageSkiaNamed( 142 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED)); 143 quiet_mode_button_->SetToggledImage( 144 views::Button::STATE_HOVERED, 145 resource_bundle.GetImageSkiaNamed( 146 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED)); 147 quiet_mode_button_->SetToggledImage( 148 views::Button::STATE_PRESSED, 149 resource_bundle.GetImageSkiaNamed( 150 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED)); 151 quiet_mode_button_->SetToggled(message_center->IsQuietMode()); 152 button_container_->AddChildView(quiet_mode_button_); 153 154 close_all_button_ = 155 new NotificationCenterButton(this, 156 IDR_NOTIFICATION_CLEAR_ALL, 157 IDR_NOTIFICATION_CLEAR_ALL_HOVER, 158 IDR_NOTIFICATION_CLEAR_ALL_PRESSED, 159 IDS_MESSAGE_CENTER_CLEAR_ALL); 160 close_all_button_->SetImage( 161 views::Button::STATE_DISABLED, 162 resource_bundle.GetImageSkiaNamed(IDR_NOTIFICATION_CLEAR_ALL_DISABLED)); 163 button_container_->AddChildView(close_all_button_); 164 165 settings_button_ = 166 new NotificationCenterButton(this, 167 IDR_NOTIFICATION_SETTINGS, 168 IDR_NOTIFICATION_SETTINGS_HOVER, 169 IDR_NOTIFICATION_SETTINGS_PRESSED, 170 IDS_MESSAGE_CENTER_SETTINGS_BUTTON_LABEL); 171 button_container_->AddChildView(settings_button_); 172 173 SetCloseAllButtonEnabled(!settings_initially_visible); 174 SetBackArrowVisible(settings_initially_visible); 175 ViewVisibilityChanged(); 176 } 177 178 void MessageCenterButtonBar::ViewVisibilityChanged() { 179 gfx::ImageSkia* settings_image = 180 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 181 IDR_NOTIFICATION_SETTINGS); 182 int image_margin = std::max(0, (kButtonSize - settings_image->width()) / 2); 183 views::GridLayout* layout = new views::GridLayout(this); 184 SetLayoutManager(layout); 185 layout->SetInsets(kFooterTopMargin, 186 kFooterLeftMargin, 187 kFooterBottomMargin, 188 std::max(0, kFooterRightMargin - image_margin)); 189 views::ColumnSet* column = layout->AddColumnSet(0); 190 if (title_arrow_->visible()) { 191 // Column for the left-arrow used to back out of settings. 192 column->AddColumn(views::GridLayout::LEADING, 193 views::GridLayout::CENTER, 194 0.0f, 195 views::GridLayout::USE_PREF, 196 0, 197 0); 198 199 column->AddPaddingColumn(0.0f, 10); 200 } 201 202 // Column for the label "Notifications". 203 column->AddColumn(views::GridLayout::LEADING, 204 views::GridLayout::CENTER, 205 0.0f, 206 views::GridLayout::USE_PREF, 207 0, 208 0); 209 210 // Fills in the remaining space between "Notifications" and buttons. 211 column->AddPaddingColumn(1.0f, image_margin); 212 213 // The button area column. 214 column->AddColumn(views::GridLayout::LEADING, 215 views::GridLayout::CENTER, 216 0.0f, 217 views::GridLayout::USE_PREF, 218 0, 219 0); 220 221 layout->StartRow(0, 0); 222 if (title_arrow_->visible()) 223 layout->AddView(title_arrow_); 224 layout->AddView(notification_label_); 225 layout->AddView(button_container_); 226 } 227 228 MessageCenterButtonBar::~MessageCenterButtonBar() {} 229 230 void MessageCenterButtonBar::SetAllButtonsEnabled(bool enabled) { 231 if (close_all_button_) 232 close_all_button_->SetEnabled(enabled); 233 settings_button_->SetEnabled(enabled); 234 quiet_mode_button_->SetEnabled(enabled); 235 } 236 237 void MessageCenterButtonBar::SetCloseAllButtonEnabled(bool enabled) { 238 if (close_all_button_) 239 close_all_button_->SetEnabled(enabled); 240 } 241 242 void MessageCenterButtonBar::SetBackArrowVisible(bool visible) { 243 if (title_arrow_) 244 title_arrow_->SetVisible(visible); 245 ViewVisibilityChanged(); 246 Layout(); 247 } 248 249 void MessageCenterButtonBar::ChildVisibilityChanged(views::View* child) { 250 InvalidateLayout(); 251 } 252 253 void MessageCenterButtonBar::ButtonPressed(views::Button* sender, 254 const ui::Event& event) { 255 if (sender == close_all_button_) { 256 message_center_view()->ClearAllNotifications(); 257 } else if (sender == settings_button_ || sender == title_arrow_) { 258 MessageCenterView* center_view = message_center_view(); 259 center_view->SetSettingsVisible(!center_view->settings_visible()); 260 } else if (sender == quiet_mode_button_) { 261 if (message_center()->IsQuietMode()) 262 message_center()->SetQuietMode(false); 263 else 264 message_center()->EnterQuietModeWithExpire(base::TimeDelta::FromDays(1)); 265 quiet_mode_button_->SetToggled(message_center()->IsQuietMode()); 266 } else { 267 NOTREACHED(); 268 } 269 } 270 271 } // namespace message_center 272