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 "ui/message_center/views/message_center_button_bar.h"
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"
28 namespace message_center
{
31 const int kButtonSize
= 40;
32 const int kChevronWidth
= 8;
33 const int kFooterLeftMargin
= 20;
34 const int kFooterRightMargin
= 14;
37 // NotificationCenterButton ////////////////////////////////////////////////////
38 ////////////////////////////////////////////////////////////////////////////////
39 class NotificationCenterButton
: public views::ToggleImageButton
{
41 NotificationCenterButton(views::ButtonListener
* listener
,
46 void set_size(gfx::Size size
) { size_
= size
; }
49 // Overridden from views::View:
50 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
51 virtual void OnPaintFocusBorder(gfx::Canvas
* canvas
) OVERRIDE
;
56 DISALLOW_COPY_AND_ASSIGN(NotificationCenterButton
);
59 NotificationCenterButton::NotificationCenterButton(
60 views::ButtonListener
* listener
,
65 : views::ToggleImageButton(listener
), size_(kButtonSize
, kButtonSize
) {
66 ui::ResourceBundle
& resource_bundle
= ui::ResourceBundle::GetSharedInstance();
67 SetImage(STATE_NORMAL
, resource_bundle
.GetImageSkiaNamed(normal_id
));
68 SetImage(STATE_HOVERED
, resource_bundle
.GetImageSkiaNamed(hover_id
));
69 SetImage(STATE_PRESSED
, resource_bundle
.GetImageSkiaNamed(pressed_id
));
70 SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
71 views::ImageButton::ALIGN_MIDDLE
);
73 SetTooltipText(resource_bundle
.GetLocalizedString(text_id
));
76 set_request_focus_on_press(false);
79 gfx::Size
NotificationCenterButton::GetPreferredSize() { return size_
; }
81 void NotificationCenterButton::OnPaintFocusBorder(gfx::Canvas
* canvas
) {
82 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) {
83 canvas
->DrawRect(gfx::Rect(2, 1, width() - 4, height() - 3),
88 // MessageCenterButtonBar /////////////////////////////////////////////////////
89 ///////////////////////////////////////////////////////////////////////////////
90 MessageCenterButtonBar::MessageCenterButtonBar(
91 MessageCenterView
* message_center_view
,
92 MessageCenter
* message_center
,
93 NotifierSettingsProvider
* notifier_settings_provider
,
94 bool settings_initially_visible
)
95 : message_center_view_(message_center_view
),
96 message_center_(message_center
),
98 notification_label_(NULL
),
99 button_container_(NULL
),
100 close_all_button_(NULL
),
101 settings_button_(NULL
),
102 quiet_mode_button_(NULL
) {
103 if (get_use_acceleration_when_possible())
104 SetPaintToLayer(true);
106 views::Background::CreateSolidBackground(kMessageCenterBackgroundColor
));
108 ui::ResourceBundle
& resource_bundle
= ui::ResourceBundle::GetSharedInstance();
110 title_arrow_
= new NotificationCenterButton(this,
111 IDR_NOTIFICATION_ARROW
,
112 IDR_NOTIFICATION_ARROW_HOVER
,
113 IDR_NOTIFICATION_ARROW_PRESSED
,
115 title_arrow_
->set_size(gfx::Size(kChevronWidth
, kButtonSize
));
117 // Keyboardists can use the gear button to switch modes.
118 title_arrow_
->set_focusable(false);
119 AddChildView(title_arrow_
);
121 gfx::Font notification_label_font
=
122 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
);
123 notification_label_
= new views::Label(
124 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_FOOTER_TITLE
),
125 notification_label_font
);
126 notification_label_
->SetAutoColorReadabilityEnabled(false);
127 notification_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
128 notification_label_
->SetEnabledColor(kRegularTextColor
);
129 AddChildView(notification_label_
);
131 button_container_
= new views::View
;
132 button_container_
->SetLayoutManager(
133 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0, 0));
134 quiet_mode_button_
= new NotificationCenterButton(
136 IDR_NOTIFICATION_DO_NOT_DISTURB
,
137 IDR_NOTIFICATION_DO_NOT_DISTURB_HOVER
,
138 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
,
139 IDS_MESSAGE_CENTER_QUIET_MODE_BUTTON_TOOLTIP
);
140 quiet_mode_button_
->SetToggledImage(
141 views::Button::STATE_NORMAL
,
142 resource_bundle
.GetImageSkiaNamed(
143 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
));
144 quiet_mode_button_
->SetToggledImage(
145 views::Button::STATE_HOVERED
,
146 resource_bundle
.GetImageSkiaNamed(
147 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
));
148 quiet_mode_button_
->SetToggledImage(
149 views::Button::STATE_PRESSED
,
150 resource_bundle
.GetImageSkiaNamed(
151 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
));
152 quiet_mode_button_
->SetToggled(message_center
->IsQuietMode());
153 button_container_
->AddChildView(quiet_mode_button_
);
156 new NotificationCenterButton(this,
157 IDR_NOTIFICATION_CLEAR_ALL
,
158 IDR_NOTIFICATION_CLEAR_ALL_HOVER
,
159 IDR_NOTIFICATION_CLEAR_ALL_PRESSED
,
160 IDS_MESSAGE_CENTER_CLEAR_ALL
);
161 button_container_
->AddChildView(close_all_button_
);
163 new NotificationCenterButton(this,
164 IDR_NOTIFICATION_SETTINGS
,
165 IDR_NOTIFICATION_SETTINGS_HOVER
,
166 IDR_NOTIFICATION_SETTINGS_PRESSED
,
167 IDS_MESSAGE_CENTER_SETTINGS_BUTTON_LABEL
);
168 button_container_
->AddChildView(settings_button_
);
170 SetCloseAllButtonEnabled(!settings_initially_visible
);
171 SetBackArrowVisible(settings_initially_visible
);
172 ViewVisibilityChanged();
175 void MessageCenterButtonBar::ViewVisibilityChanged() {
176 gfx::ImageSkia
* settings_image
=
177 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
178 IDR_NOTIFICATION_SETTINGS
);
179 int image_margin
= std::max(0, (kButtonSize
- settings_image
->width()) / 2);
180 views::GridLayout
* layout
= new views::GridLayout(this);
181 SetLayoutManager(layout
);
183 0, kFooterLeftMargin
, 0, std::max(0, kFooterRightMargin
- image_margin
));
184 views::ColumnSet
* column
= layout
->AddColumnSet(0);
185 if (title_arrow_
->visible()) {
186 // Column for the left-arrow used to back out of settings.
187 column
->AddColumn(views::GridLayout::LEADING
,
188 views::GridLayout::CENTER
,
190 views::GridLayout::USE_PREF
,
194 column
->AddPaddingColumn(0.0f
, 10);
197 // Column for the label "Notifications".
198 column
->AddColumn(views::GridLayout::LEADING
,
199 views::GridLayout::CENTER
,
201 views::GridLayout::USE_PREF
,
205 // Fills in the remaining space between "Notifications" and buttons.
206 column
->AddPaddingColumn(1.0f
, image_margin
);
208 // The button area column.
209 column
->AddColumn(views::GridLayout::LEADING
,
210 views::GridLayout::CENTER
,
212 views::GridLayout::USE_PREF
,
216 layout
->StartRow(0, 0);
217 if (title_arrow_
->visible())
218 layout
->AddView(title_arrow_
);
219 layout
->AddView(notification_label_
);
220 layout
->AddView(button_container_
);
223 MessageCenterButtonBar::~MessageCenterButtonBar() {}
225 void MessageCenterButtonBar::SetAllButtonsEnabled(bool enabled
) {
226 if (close_all_button_
)
227 close_all_button_
->SetEnabled(enabled
);
228 settings_button_
->SetEnabled(enabled
);
229 quiet_mode_button_
->SetEnabled(enabled
);
232 void MessageCenterButtonBar::SetCloseAllButtonEnabled(bool enabled
) {
233 if (close_all_button_
)
234 close_all_button_
->SetEnabled(enabled
);
237 void MessageCenterButtonBar::SetBackArrowVisible(bool visible
) {
239 title_arrow_
->SetVisible(visible
);
240 ViewVisibilityChanged();
244 void MessageCenterButtonBar::ChildVisibilityChanged(views::View
* child
) {
248 void MessageCenterButtonBar::ButtonPressed(views::Button
* sender
,
249 const ui::Event
& event
) {
250 if (sender
== close_all_button_
) {
251 message_center_view()->ClearAllNotifications();
252 } else if (sender
== settings_button_
|| sender
== title_arrow_
) {
253 MessageCenterView
* center_view
= message_center_view();
254 center_view
->SetSettingsVisible(!center_view
->settings_visible());
255 } else if (sender
== quiet_mode_button_
) {
256 if (message_center()->IsQuietMode())
257 message_center()->SetQuietMode(false);
259 message_center()->EnterQuietModeWithExpire(base::TimeDelta::FromDays(1));
260 quiet_mode_button_
->SetToggled(message_center()->IsQuietMode());
266 } // namespace message_center