Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "ui/accessibility/ax_view_state.h" | 7 #include "ui/accessibility/ax_view_state.h" |
| 8 #include "ui/base/l10n/l10n_util.h" | 8 #include "ui/base/l10n/l10n_util.h" |
| 9 #include "ui/base/models/simple_menu_model.h" | 9 #include "ui/base/models/simple_menu_model.h" |
| 10 #include "ui/base/ui_base_switches_util.h" | 10 #include "ui/base/ui_base_switches_util.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" | 17 #include "ui/message_center/views/padded_button.h" |
| 18 #include "ui/resources/grit/ui_resources.h" | 18 #include "ui/resources/grit/ui_resources.h" |
| 19 #include "ui/strings/grit/ui_strings.h" | 19 #include "ui/strings/grit/ui_strings.h" |
| 20 #include "ui/views/background.h" | 20 #include "ui/views/background.h" |
| 21 #include "ui/views/controls/button/image_button.h" | 21 #include "ui/views/controls/button/image_button.h" |
| 22 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
| 23 #include "ui/views/controls/scroll_view.h" | 23 #include "ui/views/controls/scroll_view.h" |
| 24 #include "ui/views/focus/focus_manager.h" | 24 #include "ui/views/focus/focus_manager.h" |
| 25 #include "ui/views/painter.h" | 25 #include "ui/views/painter.h" |
| 26 #include "ui/views/shadow_border.h" | 26 #include "ui/views/shadow_border.h" |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const int kCloseIconTopPadding = 5; | |
|
dewittj
2016/05/13 20:28:35
nit: I think that constexpr is slightly preferred
xiyuan
2016/05/13 22:31:00
Done.
| |
| 31 const int kCloseIconRightPadding = 5; | |
| 32 | |
| 30 const int kShadowOffset = 1; | 33 const int kShadowOffset = 1; |
| 31 const int kShadowBlur = 4; | 34 const int kShadowBlur = 4; |
| 32 | 35 |
| 33 } // namespace | 36 } // namespace |
| 34 | 37 |
| 35 namespace message_center { | 38 namespace message_center { |
| 36 | 39 |
| 37 MessageView::MessageView(MessageCenterController* controller, | 40 MessageView::MessageView(MessageCenterController* controller, |
| 38 const std::string& notification_id, | 41 const Notification& notification) |
| 39 const NotifierId& notifier_id, | |
| 40 const gfx::ImageSkia& small_image, | |
| 41 const base::string16& display_source) | |
| 42 : controller_(controller), | 42 : controller_(controller), |
| 43 notification_id_(notification_id), | 43 notification_id_(notification.id()), |
| 44 notifier_id_(notifier_id), | 44 notifier_id_(notification.notifier_id()), |
| 45 background_view_(NULL), | 45 display_source_(notification.display_source()) { |
| 46 scroller_(NULL), | |
| 47 display_source_(display_source) { | |
| 48 SetFocusBehavior(FocusBehavior::ALWAYS); | 46 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 49 | 47 |
| 50 // Create the opaque background that's above the view's shadow. | 48 // Create the opaque background that's above the view's shadow. |
| 51 background_view_ = new views::View(); | 49 background_view_ = new views::View(); |
| 52 background_view_->set_background( | 50 background_view_->set_background( |
| 53 views::Background::CreateSolidBackground(kNotificationBackgroundColor)); | 51 views::Background::CreateSolidBackground(kNotificationBackgroundColor)); |
| 54 AddChildView(background_view_); | 52 AddChildView(background_view_); |
| 55 | 53 |
| 56 views::ImageView* small_image_view = new views::ImageView(); | 54 views::ImageView* small_image_view = new views::ImageView(); |
| 57 small_image_view->SetImage(small_image); | 55 small_image_view->SetImage(notification.small_image().AsImageSkia()); |
| 58 small_image_view->SetImageSize(gfx::Size(kSmallImageSize, kSmallImageSize)); | 56 small_image_view->SetImageSize(gfx::Size(kSmallImageSize, kSmallImageSize)); |
| 59 // The small image view should be added to view hierarchy by the derived | 57 // The small image view should be added to view hierarchy by the derived |
| 60 // class. This ensures that it is on top of other views. | 58 // class. This ensures that it is on top of other views. |
| 61 small_image_view->set_owned_by_client(); | 59 small_image_view->set_owned_by_client(); |
| 62 small_image_view_.reset(small_image_view); | 60 small_image_view_.reset(small_image_view); |
| 63 | 61 |
| 64 focus_painter_ = views::Painter::CreateSolidFocusPainter( | 62 focus_painter_ = views::Painter::CreateSolidFocusPainter( |
| 65 kFocusBorderColor, gfx::Insets(0, 1, 3, 2)); | 63 kFocusBorderColor, gfx::Insets(0, 1, 3, 2)); |
| 66 } | 64 } |
| 67 | 65 |
| 68 MessageView::~MessageView() { | 66 MessageView::~MessageView() { |
| 69 } | 67 } |
| 70 | 68 |
| 71 void MessageView::UpdateWithNotification(const Notification& notification) { | 69 void MessageView::UpdateWithNotification(const Notification& notification) { |
| 72 small_image_view_->SetImage(notification.small_image().AsImageSkia()); | 70 small_image_view_->SetImage(notification.small_image().AsImageSkia()); |
| 73 display_source_ = notification.display_source(); | 71 display_source_ = notification.display_source(); |
| 72 CreateOrUpdateCloseButtonView(notification); | |
| 74 } | 73 } |
| 75 | 74 |
| 76 // static | 75 // static |
| 77 gfx::Insets MessageView::GetShadowInsets() { | 76 gfx::Insets MessageView::GetShadowInsets() { |
| 78 return gfx::Insets(kShadowBlur / 2 - kShadowOffset, | 77 return gfx::Insets(kShadowBlur / 2 - kShadowOffset, |
| 79 kShadowBlur / 2, | 78 kShadowBlur / 2, |
| 80 kShadowBlur / 2 + kShadowOffset, | 79 kShadowBlur / 2 + kShadowOffset, |
| 81 kShadowBlur / 2); | 80 kShadowBlur / 2); |
| 82 } | 81 } |
| 83 | 82 |
| 84 void MessageView::CreateShadowBorder() { | 83 void MessageView::CreateShadowBorder() { |
| 85 SetBorder(std::unique_ptr<views::Border>(new views::ShadowBorder( | 84 SetBorder(std::unique_ptr<views::Border>(new views::ShadowBorder( |
| 86 gfx::ShadowValue(gfx::Vector2d(0, kShadowOffset), kShadowBlur, | 85 gfx::ShadowValue(gfx::Vector2d(0, kShadowOffset), kShadowBlur, |
| 87 message_center::kShadowColor)))); | 86 message_center::kShadowColor)))); |
| 88 } | 87 } |
| 89 | 88 |
| 90 bool MessageView::IsCloseButtonFocused() { | 89 bool MessageView::IsCloseButtonFocused() { |
| 91 // May be overridden by the owner of the close button. | 90 if (!close_button_) |
| 92 return false; | 91 return false; |
| 92 | |
| 93 views::FocusManager* focus_manager = GetFocusManager(); | |
| 94 return focus_manager && | |
| 95 focus_manager->GetFocusedView() == close_button_.get(); | |
| 93 } | 96 } |
| 94 | 97 |
| 95 void MessageView::RequestFocusOnCloseButton() { | 98 void MessageView::RequestFocusOnCloseButton() { |
| 96 // May be overridden by the owner of the close button. | 99 if (close_button_) |
| 100 close_button_->RequestFocus(); | |
| 97 } | 101 } |
| 98 | 102 |
| 99 bool MessageView::IsPinned() { | 103 bool MessageView::IsPinned() { |
| 100 return false; | 104 return !close_button_; |
| 101 } | 105 } |
| 102 | 106 |
| 103 void MessageView::GetAccessibleState(ui::AXViewState* state) { | 107 void MessageView::GetAccessibleState(ui::AXViewState* state) { |
| 104 state->role = ui::AX_ROLE_BUTTON; | 108 state->role = ui::AX_ROLE_BUTTON; |
| 105 state->name = accessible_name_; | 109 state->name = accessible_name_; |
| 106 } | 110 } |
| 107 | 111 |
| 108 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { | 112 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { |
| 109 if (!event.IsOnlyLeftMouseButton()) | 113 if (!event.IsOnlyLeftMouseButton()) |
| 110 return false; | 114 return false; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 134 // ui/views/controls/buttons/custom_button.cc for why. | 138 // ui/views/controls/buttons/custom_button.cc for why. |
| 135 if (event.flags() != ui::EF_NONE || event.flags() != ui::VKEY_SPACE) | 139 if (event.flags() != ui::EF_NONE || event.flags() != ui::VKEY_SPACE) |
| 136 return false; | 140 return false; |
| 137 | 141 |
| 138 controller_->ClickOnNotification(notification_id_); | 142 controller_->ClickOnNotification(notification_id_); |
| 139 return true; | 143 return true; |
| 140 } | 144 } |
| 141 | 145 |
| 142 void MessageView::OnPaint(gfx::Canvas* canvas) { | 146 void MessageView::OnPaint(gfx::Canvas* canvas) { |
| 143 DCHECK_EQ(this, small_image_view_->parent()); | 147 DCHECK_EQ(this, small_image_view_->parent()); |
| 148 DCHECK_EQ(this, close_button_->parent()); | |
| 144 SlideOutView::OnPaint(canvas); | 149 SlideOutView::OnPaint(canvas); |
| 145 views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 150 views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
| 146 } | 151 } |
| 147 | 152 |
| 148 void MessageView::OnFocus() { | 153 void MessageView::OnFocus() { |
| 149 SlideOutView::OnFocus(); | 154 SlideOutView::OnFocus(); |
| 150 // We paint a focus indicator. | 155 // We paint a focus indicator. |
| 151 SchedulePaint(); | 156 SchedulePaint(); |
| 152 } | 157 } |
| 153 | 158 |
| 154 void MessageView::OnBlur() { | 159 void MessageView::OnBlur() { |
| 155 SlideOutView::OnBlur(); | 160 SlideOutView::OnBlur(); |
| 156 // We paint a focus indicator. | 161 // We paint a focus indicator. |
| 157 SchedulePaint(); | 162 SchedulePaint(); |
| 158 } | 163 } |
| 159 | 164 |
| 160 void MessageView::Layout() { | 165 void MessageView::Layout() { |
| 161 gfx::Rect content_bounds = GetContentsBounds(); | 166 gfx::Rect content_bounds = GetContentsBounds(); |
| 162 | 167 |
| 163 // Background. | 168 // Background. |
| 164 background_view_->SetBoundsRect(content_bounds); | 169 background_view_->SetBoundsRect(content_bounds); |
| 165 | 170 |
| 171 // Close button. | |
| 172 if (close_button_) { | |
| 173 gfx::Rect content_bounds = GetContentsBounds(); | |
| 174 gfx::Size close_size(close_button_->GetPreferredSize()); | |
| 175 gfx::Rect close_rect(content_bounds.right() - close_size.width(), | |
| 176 content_bounds.y(), close_size.width(), | |
| 177 close_size.height()); | |
| 178 close_button_->SetBoundsRect(close_rect); | |
| 179 } | |
| 180 | |
| 166 gfx::Size small_image_size(small_image_view_->GetPreferredSize()); | 181 gfx::Size small_image_size(small_image_view_->GetPreferredSize()); |
| 167 gfx::Rect small_image_rect(small_image_size); | 182 gfx::Rect small_image_rect(small_image_size); |
| 168 small_image_rect.set_origin(gfx::Point( | 183 small_image_rect.set_origin(gfx::Point( |
| 169 content_bounds.right() - small_image_size.width() - kSmallImagePadding, | 184 content_bounds.right() - small_image_size.width() - kSmallImagePadding, |
| 170 content_bounds.bottom() - small_image_size.height() - | 185 content_bounds.bottom() - small_image_size.height() - |
| 171 kSmallImagePadding)); | 186 kSmallImagePadding)); |
| 172 small_image_view_->SetBoundsRect(small_image_rect); | 187 small_image_view_->SetBoundsRect(small_image_rect); |
| 173 } | 188 } |
| 174 | 189 |
| 175 void MessageView::OnGestureEvent(ui::GestureEvent* event) { | 190 void MessageView::OnGestureEvent(ui::GestureEvent* event) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 202 if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent()) | 217 if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent()) |
| 203 return; | 218 return; |
| 204 | 219 |
| 205 if (scroller_) | 220 if (scroller_) |
| 206 scroller_->OnGestureEvent(event); | 221 scroller_->OnGestureEvent(event); |
| 207 event->SetHandled(); | 222 event->SetHandled(); |
| 208 } | 223 } |
| 209 | 224 |
| 210 void MessageView::ButtonPressed(views::Button* sender, | 225 void MessageView::ButtonPressed(views::Button* sender, |
| 211 const ui::Event& event) { | 226 const ui::Event& event) { |
| 227 if (close_button_ && sender == close_button_.get()) { | |
| 228 controller()->RemoveNotification(notification_id(), true); // By user. | |
| 229 } | |
| 212 } | 230 } |
| 213 | 231 |
| 214 void MessageView::OnSlideOut() { | 232 void MessageView::OnSlideOut() { |
| 215 controller_->RemoveNotification(notification_id_, true); // By user. | 233 controller_->RemoveNotification(notification_id_, true); // By user. |
| 216 } | 234 } |
| 217 | 235 |
| 218 void MessageView::SetDrawBackgroundAsActive(bool active) { | 236 void MessageView::SetDrawBackgroundAsActive(bool active) { |
| 219 if (!switches::IsTouchFeedbackEnabled()) | 237 if (!switches::IsTouchFeedbackEnabled()) |
| 220 return; | 238 return; |
| 221 background_view_->background()-> | 239 background_view_->background()-> |
| 222 SetNativeControlColor(active ? kHoveredButtonBackgroundColor : | 240 SetNativeControlColor(active ? kHoveredButtonBackgroundColor : |
| 223 kNotificationBackgroundColor); | 241 kNotificationBackgroundColor); |
| 224 SchedulePaint(); | 242 SchedulePaint(); |
| 225 } | 243 } |
| 226 | 244 |
| 245 void MessageView::CreateOrUpdateCloseButtonView( | |
| 246 const Notification& notification) { | |
| 247 set_slide_out_enabled(!notification.pinned()); | |
| 248 | |
| 249 if (!notification.pinned() && !close_button_) { | |
| 250 PaddedButton* close = new PaddedButton(this); | |
| 251 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding); | |
| 252 close->SetNormalImage(IDR_NOTIFICATION_CLOSE); | |
| 253 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER); | |
| 254 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED); | |
| 255 close->set_animate_on_state_change(false); | |
| 256 close->SetAccessibleName(l10n_util::GetStringUTF16( | |
| 257 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME)); | |
| 258 close->set_owned_by_client(); | |
| 259 AddChildView(close); | |
| 260 close_button_.reset(close); | |
| 261 } else if (notification.pinned() && close_button_) { | |
| 262 close_button_.reset(); | |
| 263 } | |
| 264 } | |
| 265 | |
| 227 } // namespace message_center | 266 } // namespace message_center |
| OLD | NEW |