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. 4 5 #ifndef UI_MESSAGE_CENTER_VIEWS_TOAST_CONTENTS_VIEW_H_ 6 #define UI_MESSAGE_CENTER_VIEWS_TOAST_CONTENTS_VIEW_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/memory/weak_ptr.h" 10 #include "ui/base/animation/animation_delegate.h" 11 #include "ui/gfx/native_widget_types.h" 12 #include "ui/gfx/point.h" 13 #include "ui/gfx/rect.h" 14 #include "ui/gfx/size.h" 15 #include "ui/views/widget/widget_delegate.h" 16 17 namespace ui { 18 class Animation; 19 class SlideAnimation; 20 } 21 22 namespace views { 23 class View; 24 } 25 26 namespace message_center { 27 28 class MessageCenter; 29 class MessagePopupCollection; 30 class MessageView; 31 class Notification; 32 33 34 class ToastContentsView : public views::WidgetDelegateView, 35 public ui::AnimationDelegate { 36 public: 37 ToastContentsView(const Notification* notification, 38 base::WeakPtr<MessagePopupCollection> collection, 39 MessageCenter* message_center); 40 virtual ~ToastContentsView(); 41 42 // Initialization and update. 43 views::Widget* CreateWidget(gfx::NativeView parent); 44 void SetContents(MessageView* view); 45 46 // Shows the new toast for the first time, animated. 47 // |origin| is the right-bottom corner of the toast. 48 void RevealWithAnimation(gfx::Point origin); 49 // Disconnectes the toast from the rest of the system immediately and start 50 // an animation. Once animation finishes, closes the widget. 51 void CloseWithAnimation(bool mark_as_shown); 52 53 void SetBoundsInstantly(gfx::Rect new_bounds); 54 void SetBoundsWithAnimation(gfx::Rect new_bounds); 55 56 // Origin and bounds are not 'instant', but rather 'current stable values', 57 // there could be animation in progress that targets these values. 58 gfx::Point origin() { return origin_; } 59 gfx::Rect bounds() { return gfx::Rect(origin_, preferred_size_); } 60 61 const std::string& id() { return id_; } 62 63 // Computes the size of the toast assuming it will host the given view. 64 static gfx::Size GetToastSizeForView(views::View* view); 65 66 // Overridden from views::View: 67 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; 68 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; 69 virtual void Layout() OVERRIDE; 70 virtual gfx::Size GetPreferredSize() OVERRIDE; 71 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 72 73 private: 74 // Overridden from ui::AnimationDelegate: 75 virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE; 76 virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE; 77 virtual void AnimationCanceled(const ui::Animation* animation) OVERRIDE; 78 79 // Overridden from views::WidgetDelegate: 80 virtual views::View* GetContentsView() OVERRIDE; 81 virtual void WindowClosing() OVERRIDE; 82 virtual bool CanActivate() const OVERRIDE; 83 virtual void OnDisplayChanged() OVERRIDE; 84 virtual void OnWorkAreaChanged() OVERRIDE; 85 86 // Given the bounds of a toast on the screen, compute the bouds for that 87 // toast in 'closed' state. The 'closed' state is used as origin/destination 88 // in reveal/closing animations. 89 gfx::Rect GetClosedToastBounds(gfx::Rect bounds); 90 91 void StartFadeIn(); 92 void StartFadeOut(); // Will call Widget::Close() when animation ends. 93 void OnBoundsAnimationEndedOrCancelled(const ui::Animation* animation); 94 95 base::WeakPtr<MessagePopupCollection> collection_; 96 MessageCenter* message_center_; 97 98 // Id if the corresponding Notification. 99 std::string id_; 100 101 scoped_ptr<ui::SlideAnimation> bounds_animation_; 102 scoped_ptr<ui::SlideAnimation> fade_animation_; 103 104 bool is_animating_bounds_; 105 gfx::Rect animated_bounds_start_; 106 gfx::Rect animated_bounds_end_; 107 // Started closing animation, will close at the end. 108 bool is_closing_; 109 // Closing animation - when it ends, close the widget. Weak, only used 110 // for referential equality. 111 ui::Animation* closing_animation_; 112 113 gfx::Point origin_; 114 gfx::Size preferred_size_; 115 116 DISALLOW_COPY_AND_ASSIGN(ToastContentsView); 117 }; 118 119 } // namespace message_center 120 121 #endif // UI_MESSAGE_CENTER_VIEWS_TOAST_CONTENTS_VIEW_H_ 122