| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/toast_contents_view.h" | 5 #include "ui/message_center/views/toast_contents_view.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 ToastContentsView::~ToastContentsView() { | 70 ToastContentsView::~ToastContentsView() { |
| 71 if (collection_) | 71 if (collection_) |
| 72 collection_->ForgetToast(this); | 72 collection_->ForgetToast(this); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void ToastContentsView::SetContents(MessageView* view, | 75 void ToastContentsView::SetContents(MessageView* view, |
| 76 bool a11y_feedback_for_updates) { | 76 bool a11y_feedback_for_updates) { |
| 77 bool already_has_contents = child_count() > 0; | 77 bool already_has_contents = child_count() > 0; |
| 78 RemoveAllChildViews(true); | 78 RemoveAllChildViews(true); |
| 79 AddChildView(view); | 79 AddChildView(view); |
| 80 preferred_size_ = GetToastSizeForView(view); | 80 UpdatePreferredSize(); |
| 81 Layout(); | |
| 82 | 81 |
| 83 // If it has the contents already, this invocation means an update of the | 82 // If it has the contents already, this invocation means an update of the |
| 84 // popup toast, and the new contents should be read through a11y feature. | 83 // popup toast, and the new contents should be read through a11y feature. |
| 85 // The notification type should be ALERT, otherwise the accessibility message | 84 // The notification type should be ALERT, otherwise the accessibility message |
| 86 // won't be read for this view which returns ROLE_WINDOW. | 85 // won't be read for this view which returns ROLE_WINDOW. |
| 87 if (already_has_contents && a11y_feedback_for_updates) | 86 if (already_has_contents && a11y_feedback_for_updates) |
| 88 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false); | 87 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false); |
| 89 } | 88 } |
| 90 | 89 |
| 91 void ToastContentsView::UpdateContents(const Notification& notification, | 90 void ToastContentsView::UpdateContents(const Notification& notification, |
| 92 bool a11y_feedback_for_updates) { | 91 bool a11y_feedback_for_updates) { |
| 93 DCHECK_GT(child_count(), 0); | 92 DCHECK_GT(child_count(), 0); |
| 94 MessageView* message_view = static_cast<MessageView*>(child_at(0)); | 93 MessageView* message_view = static_cast<MessageView*>(child_at(0)); |
| 95 message_view->UpdateWithNotification(notification); | 94 message_view->UpdateWithNotification(notification); |
| 96 gfx::Size new_size = GetToastSizeForView(message_view); | 95 UpdatePreferredSize(); |
| 97 if (preferred_size_ != new_size) { | |
| 98 preferred_size_ = new_size; | |
| 99 Layout(); | |
| 100 } | |
| 101 if (a11y_feedback_for_updates) | 96 if (a11y_feedback_for_updates) |
| 102 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false); | 97 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false); |
| 103 } | 98 } |
| 104 | 99 |
| 105 void ToastContentsView::RevealWithAnimation(gfx::Point origin) { | 100 void ToastContentsView::RevealWithAnimation(gfx::Point origin) { |
| 106 // Place/move the toast widgets. Currently it stacks the widgets from the | 101 // Place/move the toast widgets. Currently it stacks the widgets from the |
| 107 // right-bottom of the work area. | 102 // right-bottom of the work area. |
| 108 // TODO(mukai): allow to specify the placement policy from outside of this | 103 // TODO(mukai): allow to specify the placement policy from outside of this |
| 109 // class. The policy should be specified from preference on Windows, or | 104 // class. The policy should be specified from preference on Windows, or |
| 110 // the launcher alignment on ChromeOS. | 105 // the launcher alignment on ChromeOS. |
| 111 origin_ = gfx::Point(origin.x() - preferred_size_.width(), | 106 origin_ = gfx::Point(origin.x() - preferred_size_.width(), |
| 112 origin.y() - preferred_size_.height()); | 107 origin.y() - preferred_size_.height()); |
| 113 | 108 |
| 114 gfx::Rect stable_bounds(origin_, preferred_size_); | 109 gfx::Rect stable_bounds(origin_, preferred_size_); |
| 115 | 110 |
| 116 SetBoundsInstantly(GetClosedToastBounds(stable_bounds)); | 111 SetBoundsInstantly(GetClosedToastBounds(stable_bounds)); |
| 117 StartFadeIn(); | 112 StartFadeIn(); |
| 118 SetBoundsWithAnimation(stable_bounds); | 113 SetBoundsWithAnimation(stable_bounds); |
| 119 } | 114 } |
| 120 | 115 |
| 121 void ToastContentsView::CloseWithAnimation() { | 116 void ToastContentsView::CloseWithAnimation() { |
| 122 if (is_closing_) | 117 if (is_closing_) |
| 123 return; | 118 return; |
| 124 is_closing_ = true; | 119 is_closing_ = true; |
| 125 StartFadeOut(); | 120 StartFadeOut(); |
| 126 } | 121 } |
| 127 | 122 |
| 128 void ToastContentsView::SetBoundsInstantly(gfx::Rect new_bounds) { | 123 void ToastContentsView::SetBoundsInstantly(gfx::Rect new_bounds) { |
| 129 if (new_bounds == bounds()) | 124 DCHECK(new_bounds.size().width() <= preferred_size_.width() && |
| 125 new_bounds.size().height() <= preferred_size_.height()) |
| 126 << "we can not display widget bigger than notification"; |
| 127 |
| 128 if (!GetWidget()) |
| 129 return; |
| 130 |
| 131 if (new_bounds == GetWidget()->GetWindowBoundsInScreen()) |
| 130 return; | 132 return; |
| 131 | 133 |
| 132 origin_ = new_bounds.origin(); | 134 origin_ = new_bounds.origin(); |
| 133 if (!GetWidget()) | |
| 134 return; | |
| 135 GetWidget()->SetBounds(new_bounds); | 135 GetWidget()->SetBounds(new_bounds); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void ToastContentsView::SetBoundsWithAnimation(gfx::Rect new_bounds) { | 138 void ToastContentsView::SetBoundsWithAnimation(gfx::Rect new_bounds) { |
| 139 if (new_bounds == bounds()) | 139 DCHECK(new_bounds.size().width() <= preferred_size_.width() && |
| 140 new_bounds.size().height() <= preferred_size_.height()) |
| 141 << "we can not display widget bigger than notification"; |
| 142 |
| 143 if (!GetWidget()) |
| 144 return; |
| 145 |
| 146 if (new_bounds == animated_bounds_end_) |
| 140 return; | 147 return; |
| 141 | 148 |
| 142 origin_ = new_bounds.origin(); | 149 origin_ = new_bounds.origin(); |
| 143 if (!GetWidget()) | |
| 144 return; | |
| 145 | |
| 146 // This picks up the current bounds, so if there was a previous animation | 150 // This picks up the current bounds, so if there was a previous animation |
| 147 // half-done, the next one will pick up from the current location. | 151 // half-done, the next one will pick up from the current location. |
| 148 // This is the only place that should query current location of the Widget | 152 // This is the only place that should query current location of the Widget |
| 149 // on screen, the rest should refer to the bounds_. | 153 // on screen, the rest should refer to the bounds_. |
| 150 animated_bounds_start_ = GetWidget()->GetWindowBoundsInScreen(); | 154 animated_bounds_start_ = GetWidget()->GetWindowBoundsInScreen(); |
| 151 animated_bounds_end_ = new_bounds; | 155 animated_bounds_end_ = new_bounds; |
| 152 | 156 |
| 153 if (collection_) | 157 if (collection_) |
| 154 collection_->IncrementDeferCounter(); | 158 collection_->IncrementDeferCounter(); |
| 155 | 159 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 if (child_count() > 0) { | 285 if (child_count() > 0) { |
| 282 child_at(0)->SetBounds( | 286 child_at(0)->SetBounds( |
| 283 0, 0, preferred_size_.width(), preferred_size_.height()); | 287 0, 0, preferred_size_.width(), preferred_size_.height()); |
| 284 } | 288 } |
| 285 } | 289 } |
| 286 | 290 |
| 287 gfx::Size ToastContentsView::GetPreferredSize() const { | 291 gfx::Size ToastContentsView::GetPreferredSize() const { |
| 288 return child_count() ? GetToastSizeForView(child_at(0)) : gfx::Size(); | 292 return child_count() ? GetToastSizeForView(child_at(0)) : gfx::Size(); |
| 289 } | 293 } |
| 290 | 294 |
| 295 void ToastContentsView::UpdatePreferredSize() { |
| 296 DCHECK_GT(child_count(), 0); |
| 297 gfx::Size new_size = GetToastSizeForView(child_at(0)); |
| 298 if (preferred_size_ == new_size) |
| 299 return; |
| 300 // Growing notifications instantly can cause notification's to overlap |
| 301 // And shrinking with animation, leaves area, where nothing is drawn |
| 302 const bool change_instantly = preferred_size_.width() > new_size.width() || |
| 303 preferred_size_.height() > new_size.height(); |
| 304 preferred_size_ = new_size; |
| 305 Layout(); |
| 306 if (change_instantly) { |
| 307 SetBoundsInstantly(bounds()); |
| 308 return; |
| 309 } |
| 310 SetBoundsWithAnimation(bounds()); |
| 311 } |
| 312 |
| 291 void ToastContentsView::GetAccessibleState(ui::AXViewState* state) { | 313 void ToastContentsView::GetAccessibleState(ui::AXViewState* state) { |
| 292 if (child_count() > 0) | 314 if (child_count() > 0) |
| 293 child_at(0)->GetAccessibleState(state); | 315 child_at(0)->GetAccessibleState(state); |
| 294 state->role = ui::AX_ROLE_WINDOW; | 316 state->role = ui::AX_ROLE_WINDOW; |
| 295 } | 317 } |
| 296 | 318 |
| 297 void ToastContentsView::ClickOnNotification( | 319 void ToastContentsView::ClickOnNotification( |
| 298 const std::string& notification_id) { | 320 const std::string& notification_id) { |
| 299 if (collection_) | 321 if (collection_) |
| 300 collection_->ClickOnNotification(notification_id); | 322 collection_->ClickOnNotification(notification_id); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 } | 380 } |
| 359 | 381 |
| 360 gfx::Rect ToastContentsView::GetClosedToastBounds(gfx::Rect bounds) { | 382 gfx::Rect ToastContentsView::GetClosedToastBounds(gfx::Rect bounds) { |
| 361 return gfx::Rect(bounds.x() + bounds.width() - kClosedToastWidth, | 383 return gfx::Rect(bounds.x() + bounds.width() - kClosedToastWidth, |
| 362 bounds.y(), | 384 bounds.y(), |
| 363 kClosedToastWidth, | 385 kClosedToastWidth, |
| 364 bounds.height()); | 386 bounds.height()); |
| 365 } | 387 } |
| 366 | 388 |
| 367 } // namespace message_center | 389 } // namespace message_center |
| OLD | NEW |