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
3 // found in the LICENSE file.
5 #include "ui/message_center/views/toast_contents_view.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "ui/accessibility/ax_view_state.h"
13 #include "ui/gfx/animation/animation_delegate.h"
14 #include "ui/gfx/animation/slide_animation.h"
15 #include "ui/gfx/display.h"
16 #include "ui/gfx/screen.h"
17 #include "ui/message_center/message_center_style.h"
18 #include "ui/message_center/notification.h"
19 #include "ui/message_center/views/message_popup_collection.h"
20 #include "ui/message_center/views/message_view.h"
21 #include "ui/views/background.h"
22 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
27 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
32 namespace message_center {
35 // The width of a toast before animated reveal and after closing.
36 const int kClosedToastWidth = 5;
38 // FadeIn/Out look a bit better if they are slightly longer then default slide.
39 const int kFadeInOutDuration = 200;
44 gfx::Size ToastContentsView::GetToastSizeForView(const views::View* view) {
45 int width = kNotificationWidth + view->GetInsets().width();
46 return gfx::Size(width, view->GetHeightForWidth(width));
49 ToastContentsView::ToastContentsView(
50 const std::string& notification_id,
51 base::WeakPtr<MessagePopupCollection> collection)
52 : collection_(collection),
54 is_animating_bounds_(false),
56 closing_animation_(NULL) {
57 set_notify_enter_exit_on_child(true);
58 // Sets the transparent background. Then, when the message view is slid out,
59 // the whole toast seems to slide although the actual bound of the widget
60 // remains. This is hacky but easier to keep the consistency.
61 set_background(views::Background::CreateSolidBackground(0, 0, 0, 0));
63 fade_animation_.reset(new gfx::SlideAnimation(this));
64 fade_animation_->SetSlideDuration(kFadeInOutDuration);
66 CreateWidget(collection->parent());
69 // This is destroyed when the toast window closes.
70 ToastContentsView::~ToastContentsView() {
72 collection_->ForgetToast(this);
75 void ToastContentsView::SetContents(MessageView* view,
76 bool a11y_feedback_for_updates) {
77 bool already_has_contents = child_count() > 0;
78 RemoveAllChildViews(true);
80 preferred_size_ = GetToastSizeForView(view);
83 // 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.
85 // The notification type should be ALERT, otherwise the accessibility message
86 // won't be read for this view which returns ROLE_WINDOW.
87 if (already_has_contents && a11y_feedback_for_updates)
88 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false);
91 void ToastContentsView::UpdateContents(const Notification& notification,
92 bool a11y_feedback_for_updates) {
93 DCHECK_GT(child_count(), 0);
94 MessageView* message_view = static_cast<MessageView*>(child_at(0));
95 message_view->UpdateWithNotification(notification);
96 if (a11y_feedback_for_updates)
97 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false);
100 void ToastContentsView::RevealWithAnimation(gfx::Point origin) {
101 // Place/move the toast widgets. Currently it stacks the widgets from the
102 // right-bottom of the work area.
103 // TODO(mukai): allow to specify the placement policy from outside of this
104 // class. The policy should be specified from preference on Windows, or
105 // the launcher alignment on ChromeOS.
106 origin_ = gfx::Point(origin.x() - preferred_size_.width(),
107 origin.y() - preferred_size_.height());
109 gfx::Rect stable_bounds(origin_, preferred_size_);
111 SetBoundsInstantly(GetClosedToastBounds(stable_bounds));
113 SetBoundsWithAnimation(stable_bounds);
116 void ToastContentsView::CloseWithAnimation() {
123 void ToastContentsView::SetBoundsInstantly(gfx::Rect new_bounds) {
124 if (new_bounds == bounds())
127 origin_ = new_bounds.origin();
130 GetWidget()->SetBounds(new_bounds);
133 void ToastContentsView::SetBoundsWithAnimation(gfx::Rect new_bounds) {
134 if (new_bounds == bounds())
137 origin_ = new_bounds.origin();
141 // This picks up the current bounds, so if there was a previous animation
142 // half-done, the next one will pick up from the current location.
143 // This is the only place that should query current location of the Widget
144 // on screen, the rest should refer to the bounds_.
145 animated_bounds_start_ = GetWidget()->GetWindowBoundsInScreen();
146 animated_bounds_end_ = new_bounds;
149 collection_->IncrementDeferCounter();
151 if (bounds_animation_.get())
152 bounds_animation_->Stop();
154 bounds_animation_.reset(new gfx::SlideAnimation(this));
155 bounds_animation_->Show();
158 void ToastContentsView::StartFadeIn() {
159 // The decrement is done in OnBoundsAnimationEndedOrCancelled callback.
161 collection_->IncrementDeferCounter();
162 fade_animation_->Stop();
164 GetWidget()->SetOpacity(0);
165 GetWidget()->ShowInactive();
166 fade_animation_->Reset(0);
167 fade_animation_->Show();
170 void ToastContentsView::StartFadeOut() {
171 // The decrement is done in OnBoundsAnimationEndedOrCancelled callback.
173 collection_->IncrementDeferCounter();
174 fade_animation_->Stop();
176 closing_animation_ = (is_closing_ ? fade_animation_.get() : NULL);
177 fade_animation_->Reset(1);
178 fade_animation_->Hide();
181 void ToastContentsView::OnBoundsAnimationEndedOrCancelled(
182 const gfx::Animation* animation) {
183 if (is_closing_ && closing_animation_ == animation && GetWidget()) {
184 views::Widget* widget = GetWidget();
186 // TODO(dewittj): This is a workaround to prevent a nasty bug where
187 // closing a transparent widget doesn't actually remove the window,
188 // causing entire areas of the screen to become unresponsive to clicks.
189 // See crbug.com/243469
192 widget->SetOpacity(0xFF);
198 // This cannot be called before GetWidget()->Close(). Decrementing defer count
199 // will invoke update, which may invoke another close animation with
200 // incrementing defer counter. Close() after such process will cause a
201 // mismatch between increment/decrement. See crbug.com/238477
203 collection_->DecrementDeferCounter();
206 // gfx::AnimationDelegate
207 void ToastContentsView::AnimationProgressed(const gfx::Animation* animation) {
208 if (animation == bounds_animation_.get()) {
209 gfx::Rect current(animation->CurrentValueBetween(
210 animated_bounds_start_, animated_bounds_end_));
211 GetWidget()->SetBounds(current);
212 } else if (animation == fade_animation_.get()) {
213 unsigned char opacity =
214 static_cast<unsigned char>(fade_animation_->GetCurrentValue() * 255);
215 GetWidget()->SetOpacity(opacity);
219 void ToastContentsView::AnimationEnded(const gfx::Animation* animation) {
220 OnBoundsAnimationEndedOrCancelled(animation);
223 void ToastContentsView::AnimationCanceled(
224 const gfx::Animation* animation) {
225 OnBoundsAnimationEndedOrCancelled(animation);
228 // views::WidgetDelegate
229 views::View* ToastContentsView::GetContentsView() {
233 void ToastContentsView::WindowClosing() {
234 if (!is_closing_ && collection_.get())
235 collection_->ForgetToast(this);
238 void ToastContentsView::OnDisplayChanged() {
239 views::Widget* widget = GetWidget();
243 gfx::NativeView native_view = widget->GetNativeView();
244 if (!native_view || !collection_.get())
247 collection_->OnDisplayMetricsChanged(
248 Screen::GetScreenFor(native_view)->GetDisplayNearestWindow(native_view));
251 void ToastContentsView::OnWorkAreaChanged() {
252 views::Widget* widget = GetWidget();
256 gfx::NativeView native_view = widget->GetNativeView();
257 if (!native_view || !collection_.get())
260 collection_->OnDisplayMetricsChanged(
261 Screen::GetScreenFor(native_view)->GetDisplayNearestWindow(native_view));
265 void ToastContentsView::OnMouseEntered(const ui::MouseEvent& event) {
267 collection_->OnMouseEntered(this);
270 void ToastContentsView::OnMouseExited(const ui::MouseEvent& event) {
272 collection_->OnMouseExited(this);
275 void ToastContentsView::Layout() {
276 if (child_count() > 0) {
277 child_at(0)->SetBounds(
278 0, 0, preferred_size_.width(), preferred_size_.height());
282 gfx::Size ToastContentsView::GetPreferredSize() const {
283 return child_count() ? GetToastSizeForView(child_at(0)) : gfx::Size();
286 void ToastContentsView::GetAccessibleState(ui::AXViewState* state) {
287 if (child_count() > 0)
288 child_at(0)->GetAccessibleState(state);
289 state->role = ui::AX_ROLE_WINDOW;
292 void ToastContentsView::ClickOnNotification(
293 const std::string& notification_id) {
295 collection_->ClickOnNotification(notification_id);
298 void ToastContentsView::RemoveNotification(
299 const std::string& notification_id,
302 collection_->RemoveNotification(notification_id, by_user);
305 scoped_ptr<ui::MenuModel> ToastContentsView::CreateMenuModel(
306 const NotifierId& notifier_id,
307 const base::string16& display_source) {
308 // Should not reach, the context menu should be handled in
309 // MessagePopupCollection.
311 return scoped_ptr<ui::MenuModel>();
314 bool ToastContentsView::HasClickedListener(
315 const std::string& notification_id) {
318 return collection_->HasClickedListener(notification_id);
321 void ToastContentsView::ClickOnNotificationButton(
322 const std::string& notification_id,
325 collection_->ClickOnNotificationButton(notification_id, button_index);
328 void ToastContentsView::CreateWidget(gfx::NativeView parent) {
329 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
330 params.keep_on_top = true;
332 params.parent = parent;
333 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
334 params.delegate = this;
335 views::Widget* widget = new views::Widget();
336 widget->set_focus_on_creation(false);
339 // We want to ensure that this toast always goes to the native desktop,
340 // not the Ash desktop (since there is already another toast contents view
343 params.native_widget = new views::DesktopNativeWidgetAura(widget);
346 widget->Init(params);
349 gfx::Rect ToastContentsView::GetClosedToastBounds(gfx::Rect bounds) {
350 return gfx::Rect(bounds.x() + bounds.width() - kClosedToastWidth,
356 } // namespace message_center