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"
27 #include "ui/views/painter.h"
29 namespace message_center
{
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;
40 // NotificationCenterButton ////////////////////////////////////////////////////
41 ////////////////////////////////////////////////////////////////////////////////
42 class NotificationCenterButton
: public views::ToggleImageButton
{
44 NotificationCenterButton(views::ButtonListener
* listener
,
49 void set_size(gfx::Size size
) { size_
= size
; }
52 // Overridden from views::View:
53 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
58 DISALLOW_COPY_AND_ASSIGN(NotificationCenterButton
);
61 NotificationCenterButton::NotificationCenterButton(
62 views::ButtonListener
* listener
,
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
);
75 SetTooltipText(resource_bundle
.GetLocalizedString(text_id
));
78 set_request_focus_on_press(false);
80 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
82 gfx::Insets(1, 2, 2, 2)));
85 gfx::Size
NotificationCenterButton::GetPreferredSize() { return size_
; }
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
),
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);
105 views::Background::CreateSolidBackground(kMessageCenterBackgroundColor
));
107 ui::ResourceBundle
& resource_bundle
= ui::ResourceBundle::GetSharedInstance();
109 title_arrow_
= new NotificationCenterButton(this,
110 IDR_NOTIFICATION_ARROW
,
111 IDR_NOTIFICATION_ARROW_HOVER
,
112 IDR_NOTIFICATION_ARROW_PRESSED
,
114 title_arrow_
->set_size(gfx::Size(kChevronWidth
, kButtonSize
));
116 // Keyboardists can use the gear button to switch modes.
117 title_arrow_
->SetFocusable(false);
118 AddChildView(title_arrow_
);
120 notification_label_
= new views::Label(
121 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_FOOTER_TITLE
));
122 notification_label_
->SetAutoColorReadabilityEnabled(false);
123 notification_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
124 notification_label_
->SetEnabledColor(kRegularTextColor
);
125 AddChildView(notification_label_
);
127 button_container_
= new views::View
;
128 button_container_
->SetLayoutManager(
129 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0, 0));
130 quiet_mode_button_
= new NotificationCenterButton(
132 IDR_NOTIFICATION_DO_NOT_DISTURB
,
133 IDR_NOTIFICATION_DO_NOT_DISTURB_HOVER
,
134 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
,
135 IDS_MESSAGE_CENTER_QUIET_MODE_BUTTON_TOOLTIP
);
136 quiet_mode_button_
->SetToggledImage(
137 views::Button::STATE_NORMAL
,
138 resource_bundle
.GetImageSkiaNamed(
139 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
));
140 quiet_mode_button_
->SetToggledImage(
141 views::Button::STATE_HOVERED
,
142 resource_bundle
.GetImageSkiaNamed(
143 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
));
144 quiet_mode_button_
->SetToggledImage(
145 views::Button::STATE_PRESSED
,
146 resource_bundle
.GetImageSkiaNamed(
147 IDR_NOTIFICATION_DO_NOT_DISTURB_PRESSED
));
148 quiet_mode_button_
->SetToggled(message_center
->IsQuietMode());
149 button_container_
->AddChildView(quiet_mode_button_
);
152 new NotificationCenterButton(this,
153 IDR_NOTIFICATION_CLEAR_ALL
,
154 IDR_NOTIFICATION_CLEAR_ALL_HOVER
,
155 IDR_NOTIFICATION_CLEAR_ALL_PRESSED
,
156 IDS_MESSAGE_CENTER_CLEAR_ALL
);
157 close_all_button_
->SetImage(
158 views::Button::STATE_DISABLED
,
159 resource_bundle
.GetImageSkiaNamed(IDR_NOTIFICATION_CLEAR_ALL_DISABLED
));
160 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
);
182 layout
->SetInsets(kFooterTopMargin
,
185 std::max(0, kFooterRightMargin
- image_margin
));
186 views::ColumnSet
* column
= layout
->AddColumnSet(0);
187 if (title_arrow_
->visible()) {
188 // Column for the left-arrow used to back out of settings.
189 column
->AddColumn(views::GridLayout::LEADING
,
190 views::GridLayout::CENTER
,
192 views::GridLayout::USE_PREF
,
196 column
->AddPaddingColumn(0.0f
, 10);
199 // Column for the label "Notifications".
200 column
->AddColumn(views::GridLayout::LEADING
,
201 views::GridLayout::CENTER
,
203 views::GridLayout::USE_PREF
,
207 // Fills in the remaining space between "Notifications" and buttons.
208 column
->AddPaddingColumn(1.0f
, image_margin
);
210 // The button area column.
211 column
->AddColumn(views::GridLayout::LEADING
,
212 views::GridLayout::CENTER
,
214 views::GridLayout::USE_PREF
,
218 layout
->StartRow(0, 0);
219 if (title_arrow_
->visible())
220 layout
->AddView(title_arrow_
);
221 layout
->AddView(notification_label_
);
222 layout
->AddView(button_container_
);
225 MessageCenterButtonBar::~MessageCenterButtonBar() {}
227 void MessageCenterButtonBar::SetAllButtonsEnabled(bool enabled
) {
228 if (close_all_button_
)
229 close_all_button_
->SetEnabled(enabled
);
230 settings_button_
->SetEnabled(enabled
);
231 quiet_mode_button_
->SetEnabled(enabled
);
234 void MessageCenterButtonBar::SetCloseAllButtonEnabled(bool enabled
) {
235 if (close_all_button_
)
236 close_all_button_
->SetEnabled(enabled
);
239 void MessageCenterButtonBar::SetBackArrowVisible(bool visible
) {
241 title_arrow_
->SetVisible(visible
);
242 ViewVisibilityChanged();
246 void MessageCenterButtonBar::ChildVisibilityChanged(views::View
* child
) {
250 void MessageCenterButtonBar::ButtonPressed(views::Button
* sender
,
251 const ui::Event
& event
) {
252 if (sender
== close_all_button_
) {
253 message_center_view()->ClearAllNotifications();
254 } else if (sender
== settings_button_
|| sender
== title_arrow_
) {
255 MessageCenterView
* center_view
= message_center_view();
256 center_view
->SetSettingsVisible(!center_view
->settings_visible());
257 } else if (sender
== quiet_mode_button_
) {
258 if (message_center()->IsQuietMode())
259 message_center()->SetQuietMode(false);
261 message_center()->EnterQuietModeWithExpire(base::TimeDelta::FromDays(1));
262 quiet_mode_button_
->SetToggled(message_center()->IsQuietMode());
268 } // namespace message_center