Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: ui/message_center/views/message_view.cc

Issue 2547433002: Transfer responsibility for providing a close button for a notification to each implementation of M… (Closed)
Patch Set: WrapUnique -> MakeUnique Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/message_center/views/message_view.h ('k') | ui/message_center/views/notification_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/message_center/views/message_view.h" 5 #include "ui/message_center/views/message_view.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/accessibility/ax_node_data.h" 8 #include "ui/accessibility/ax_node_data.h"
9 #include "ui/base/l10n/l10n_util.h" 9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/base/models/simple_menu_model.h" 10 #include "ui/base/models/simple_menu_model.h"
11 #include "ui/compositor/scoped_layer_animation_settings.h" 11 #include "ui/compositor/scoped_layer_animation_settings.h"
12 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/shadow_value.h" 13 #include "ui/gfx/shadow_value.h"
14 #include "ui/message_center/message_center.h" 14 #include "ui/message_center/message_center.h"
15 #include "ui/message_center/message_center_style.h" 15 #include "ui/message_center/message_center_style.h"
16 #include "ui/message_center/views/message_center_controller.h" 16 #include "ui/message_center/views/message_center_controller.h"
17 #include "ui/message_center/views/padded_button.h"
18 #include "ui/resources/grit/ui_resources.h" 17 #include "ui/resources/grit/ui_resources.h"
19 #include "ui/strings/grit/ui_strings.h" 18 #include "ui/strings/grit/ui_strings.h"
20 #include "ui/views/background.h" 19 #include "ui/views/background.h"
21 #include "ui/views/controls/button/image_button.h" 20 #include "ui/views/controls/button/image_button.h"
22 #include "ui/views/controls/image_view.h" 21 #include "ui/views/controls/image_view.h"
23 #include "ui/views/controls/scroll_view.h" 22 #include "ui/views/controls/scroll_view.h"
24 #include "ui/views/focus/focus_manager.h" 23 #include "ui/views/focus/focus_manager.h"
25 #include "ui/views/painter.h" 24 #include "ui/views/painter.h"
26 #include "ui/views/shadow_border.h" 25 #include "ui/views/shadow_border.h"
27 26
28 namespace { 27 namespace {
29 28
30 constexpr int kCloseIconTopPadding = 5;
31 constexpr int kCloseIconRightPadding = 5;
32
33 constexpr int kShadowOffset = 1; 29 constexpr int kShadowOffset = 1;
34 constexpr int kShadowBlur = 4; 30 constexpr int kShadowBlur = 4;
35 31
36 // Creates a text for spoken feedback from the data contained in the 32 // Creates a text for spoken feedback from the data contained in the
37 // notification. 33 // notification.
38 base::string16 CreateAccessibleName( 34 base::string16 CreateAccessibleName(
39 const message_center::Notification& notification) { 35 const message_center::Notification& notification) {
40 if (!notification.accessible_name().empty()) 36 if (!notification.accessible_name().empty())
41 return notification.accessible_name(); 37 return notification.accessible_name();
42 38
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 81
86 accessible_name_ = CreateAccessibleName(notification); 82 accessible_name_ = CreateAccessibleName(notification);
87 } 83 }
88 84
89 MessageView::~MessageView() { 85 MessageView::~MessageView() {
90 } 86 }
91 87
92 void MessageView::UpdateWithNotification(const Notification& notification) { 88 void MessageView::UpdateWithNotification(const Notification& notification) {
93 small_image_view_->SetImage(notification.small_image().AsImageSkia()); 89 small_image_view_->SetImage(notification.small_image().AsImageSkia());
94 display_source_ = notification.display_source(); 90 display_source_ = notification.display_source();
95 CreateOrUpdateCloseButtonView(notification);
96 accessible_name_ = CreateAccessibleName(notification); 91 accessible_name_ = CreateAccessibleName(notification);
97 } 92 }
98 93
99 // static 94 // static
100 gfx::Insets MessageView::GetShadowInsets() { 95 gfx::Insets MessageView::GetShadowInsets() {
101 return gfx::Insets(kShadowBlur / 2 - kShadowOffset, 96 return gfx::Insets(kShadowBlur / 2 - kShadowOffset,
102 kShadowBlur / 2, 97 kShadowBlur / 2,
103 kShadowBlur / 2 + kShadowOffset, 98 kShadowBlur / 2 + kShadowOffset,
104 kShadowBlur / 2); 99 kShadowBlur / 2);
105 } 100 }
106 101
107 void MessageView::CreateShadowBorder() { 102 void MessageView::CreateShadowBorder() {
108 SetBorder(std::unique_ptr<views::Border>(new views::ShadowBorder( 103 SetBorder(std::unique_ptr<views::Border>(new views::ShadowBorder(
109 gfx::ShadowValue(gfx::Vector2d(0, kShadowOffset), kShadowBlur, 104 gfx::ShadowValue(gfx::Vector2d(0, kShadowOffset), kShadowBlur,
110 message_center::kShadowColor)))); 105 message_center::kShadowColor))));
111 } 106 }
112 107
113 bool MessageView::IsCloseButtonFocused() {
114 if (!close_button_)
115 return false;
116
117 views::FocusManager* focus_manager = GetFocusManager();
118 return focus_manager &&
119 focus_manager->GetFocusedView() == close_button_.get();
120 }
121
122 void MessageView::RequestFocusOnCloseButton() {
123 if (close_button_)
124 close_button_->RequestFocus();
125 }
126
127 bool MessageView::IsPinned() {
128 return !close_button_;
129 }
130
131 void MessageView::GetAccessibleNodeData(ui::AXNodeData* node_data) { 108 void MessageView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
132 node_data->role = ui::AX_ROLE_BUTTON; 109 node_data->role = ui::AX_ROLE_BUTTON;
133 node_data->SetName(accessible_name_); 110 node_data->SetName(accessible_name_);
134 } 111 }
135 112
136 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { 113 bool MessageView::OnMousePressed(const ui::MouseEvent& event) {
137 if (!event.IsOnlyLeftMouseButton()) 114 if (!event.IsOnlyLeftMouseButton())
138 return false; 115 return false;
139 116
140 controller_->ClickOnNotification(notification_id_); 117 controller_->ClickOnNotification(notification_id_);
(...skipping 21 matching lines...) Expand all
162 // ui/views/controls/buttons/custom_button.cc for why. 139 // ui/views/controls/buttons/custom_button.cc for why.
163 if (event.flags() != ui::EF_NONE || event.key_code() != ui::VKEY_SPACE) 140 if (event.flags() != ui::EF_NONE || event.key_code() != ui::VKEY_SPACE)
164 return false; 141 return false;
165 142
166 controller_->ClickOnNotification(notification_id_); 143 controller_->ClickOnNotification(notification_id_);
167 return true; 144 return true;
168 } 145 }
169 146
170 void MessageView::OnPaint(gfx::Canvas* canvas) { 147 void MessageView::OnPaint(gfx::Canvas* canvas) {
171 DCHECK_EQ(this, small_image_view_->parent()); 148 DCHECK_EQ(this, small_image_view_->parent());
172 DCHECK_EQ(this, close_button_->parent());
173 SlideOutView::OnPaint(canvas); 149 SlideOutView::OnPaint(canvas);
174 views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); 150 views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
175 } 151 }
176 152
177 void MessageView::OnFocus() { 153 void MessageView::OnFocus() {
178 SlideOutView::OnFocus(); 154 SlideOutView::OnFocus();
179 // We paint a focus indicator. 155 // We paint a focus indicator.
180 SchedulePaint(); 156 SchedulePaint();
181 } 157 }
182 158
183 void MessageView::OnBlur() { 159 void MessageView::OnBlur() {
184 SlideOutView::OnBlur(); 160 SlideOutView::OnBlur();
185 // We paint a focus indicator. 161 // We paint a focus indicator.
186 SchedulePaint(); 162 SchedulePaint();
187 } 163 }
188 164
189 void MessageView::Layout() { 165 void MessageView::Layout() {
190 gfx::Rect content_bounds = GetContentsBounds(); 166 gfx::Rect content_bounds = GetContentsBounds();
191 167
192 // Background. 168 // Background.
193 background_view_->SetBoundsRect(content_bounds); 169 background_view_->SetBoundsRect(content_bounds);
194 170
195 // Close button.
196 if (close_button_) {
197 gfx::Rect content_bounds = GetContentsBounds();
198 gfx::Size close_size(close_button_->GetPreferredSize());
199 gfx::Rect close_rect(content_bounds.right() - close_size.width(),
200 content_bounds.y(), close_size.width(),
201 close_size.height());
202 close_button_->SetBoundsRect(close_rect);
203 }
204
205 gfx::Size small_image_size(small_image_view_->GetPreferredSize()); 171 gfx::Size small_image_size(small_image_view_->GetPreferredSize());
206 gfx::Rect small_image_rect(small_image_size); 172 gfx::Rect small_image_rect(small_image_size);
207 small_image_rect.set_origin(gfx::Point( 173 small_image_rect.set_origin(gfx::Point(
208 content_bounds.right() - small_image_size.width() - kSmallImagePadding, 174 content_bounds.right() - small_image_size.width() - kSmallImagePadding,
209 content_bounds.bottom() - small_image_size.height() - 175 content_bounds.bottom() - small_image_size.height() -
210 kSmallImagePadding)); 176 kSmallImagePadding));
211 small_image_view_->SetBoundsRect(small_image_rect); 177 small_image_view_->SetBoundsRect(small_image_rect);
212 } 178 }
213 179
214 void MessageView::OnGestureEvent(ui::GestureEvent* event) { 180 void MessageView::OnGestureEvent(ui::GestureEvent* event) {
(...skipping 24 matching lines...) Expand all
239 // slide-out behavior. See http://crbug.com/172991 205 // slide-out behavior. See http://crbug.com/172991
240 206
241 if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent()) 207 if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent())
242 return; 208 return;
243 209
244 if (scroller_) 210 if (scroller_)
245 scroller_->OnGestureEvent(event); 211 scroller_->OnGestureEvent(event);
246 event->SetHandled(); 212 event->SetHandled();
247 } 213 }
248 214
249 void MessageView::ButtonPressed(views::Button* sender,
250 const ui::Event& event) {
251 if (close_button_ && sender == close_button_.get()) {
252 controller()->RemoveNotification(notification_id(), true); // By user.
253 }
254 }
255
256 void MessageView::OnSlideOut() { 215 void MessageView::OnSlideOut() {
257 controller_->RemoveNotification(notification_id_, true); // By user. 216 controller_->RemoveNotification(notification_id_, true); // By user.
258 } 217 }
259 218
260 void MessageView::SetDrawBackgroundAsActive(bool active) { 219 void MessageView::SetDrawBackgroundAsActive(bool active) {
261 background_view_->background()-> 220 background_view_->background()->
262 SetNativeControlColor(active ? kHoveredButtonBackgroundColor : 221 SetNativeControlColor(active ? kHoveredButtonBackgroundColor :
263 kNotificationBackgroundColor); 222 kNotificationBackgroundColor);
264 SchedulePaint(); 223 SchedulePaint();
265 } 224 }
266 225
267 void MessageView::CreateOrUpdateCloseButtonView(
268 const Notification& notification) {
269 set_slide_out_enabled(!notification.pinned());
270
271 if (!notification.pinned() && !close_button_) {
272 PaddedButton* close = new PaddedButton(this);
273 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding);
274 close->SetNormalImage(IDR_NOTIFICATION_CLOSE);
275 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER);
276 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED);
277 close->set_animate_on_state_change(false);
278 close->SetAccessibleName(l10n_util::GetStringUTF16(
279 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME));
280 close->SetTooltipText(l10n_util::GetStringUTF16(
281 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_TOOLTIP));
282 close->set_owned_by_client();
283 AddChildView(close);
284 close_button_.reset(close);
285 } else if (notification.pinned() && close_button_) {
286 close_button_.reset();
287 }
288 }
289
290 } // namespace message_center 226 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/views/message_view.h ('k') | ui/message_center/views/notification_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698