• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gfx/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/message_center/views/message_center_controller.h"
16 #include "ui/views/widget/widget_delegate.h"
17 
18 namespace gfx {
19 class Animation;
20 class SlideAnimation;
21 }
22 
23 namespace views {
24 class View;
25 }
26 
27 namespace message_center {
28 
29 class MessagePopupCollection;
30 class MessageView;
31 class Notification;
32 
33 // The widget host for a popup. Also implements MessageCenterController
34 // which delegates over to MessagePopupCollection, but takes care about
35 // checking the weakref since MessagePopupCollection may disappear before
36 // widget/views are closed/destructed.
37 class ToastContentsView : public views::WidgetDelegateView,
38                           public MessageCenterController,
39                           public gfx::AnimationDelegate {
40  public:
41   // Computes the size of a toast assuming it will host the given view.
42   static gfx::Size GetToastSizeForView(views::View* view);
43 
44   ToastContentsView(const std::string& notification_id,
45                     base::WeakPtr<MessagePopupCollection> collection);
46   virtual ~ToastContentsView();
47 
48   // Sets the inner view of the toast. If it has contents already,
49   // |a11y_feedback_for_updates| causes the view to notify that the
50   // accessibility message should be read after this update.
51   void SetContents(MessageView* view, bool a11y_feedback_for_updates);
52 
53   // Shows the new toast for the first time, animated.
54   // |origin| is the right-bottom corner of the toast.
55   void RevealWithAnimation(gfx::Point origin);
56 
57   // Disconnectes the toast from the rest of the system immediately and start
58   // an animation. Once animation finishes, closes the widget.
59   void CloseWithAnimation();
60 
61   void SetBoundsWithAnimation(gfx::Rect new_bounds);
62 
63   // Origin and bounds are not 'instant', but rather 'current stable values',
64   // there could be animation in progress that targets these values.
origin()65   gfx::Point origin() { return origin_; }
bounds()66   gfx::Rect bounds() { return gfx::Rect(origin_, preferred_size_); }
67 
id()68   const std::string& id() { return id_; }
69 
70   // Overridden from views::View:
71   virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
72   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
73   virtual void Layout() OVERRIDE;
74   virtual gfx::Size GetPreferredSize() OVERRIDE;
75   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
76 
77  private:
78   // Overridden from MessageCenterController:
79   virtual void ClickOnNotification(const std::string& notification_id) OVERRIDE;
80   virtual void RemoveNotification(const std::string& notification_id,
81                                   bool by_user) OVERRIDE;
82   virtual void DisableNotificationsFromThisSource(
83       const NotifierId& notifier_id) OVERRIDE;
84   virtual void ShowNotifierSettingsBubble() OVERRIDE;
85   virtual bool HasClickedListener(const std::string& notification_id) OVERRIDE;
86   virtual void ClickOnNotificationButton(const std::string& notification_id,
87                                          int button_index) OVERRIDE;
88   virtual void ExpandNotification(const std::string& notification_id) OVERRIDE;
89   virtual void GroupBodyClicked(const std::string& last_notification_id)
90       OVERRIDE;
91   virtual void ExpandGroup(const NotifierId& notifier_id) OVERRIDE;
92   virtual void RemoveGroup(const NotifierId& notifier_id) OVERRIDE;
93 
94   // Overridden from gfx::AnimationDelegate:
95   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
96   virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
97   virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE;
98 
99   // Overridden from views::WidgetDelegate:
100   virtual views::View* GetContentsView() OVERRIDE;
101   virtual void WindowClosing() OVERRIDE;
102   virtual bool CanActivate() const OVERRIDE;
103   virtual void OnDisplayChanged() OVERRIDE;
104   virtual void OnWorkAreaChanged() OVERRIDE;
105 
106   // Initialization and update.
107   void CreateWidget(gfx::NativeView parent);
108 
109   // Immediately moves the toast without any sort of delay or animation.
110   void SetBoundsInstantly(gfx::Rect new_bounds);
111 
112   // Given the bounds of a toast on the screen, compute the bouds for that
113   // toast in 'closed' state. The 'closed' state is used as origin/destination
114   // in reveal/closing animations.
115   gfx::Rect GetClosedToastBounds(gfx::Rect bounds);
116 
117   void StartFadeIn();
118   void StartFadeOut();  // Will call Widget::Close() when animation ends.
119   void OnBoundsAnimationEndedOrCancelled(const gfx::Animation* animation);
120 
121   base::WeakPtr<MessagePopupCollection> collection_;
122 
123   // Id if the corresponding Notification.
124   std::string id_;
125 
126   scoped_ptr<gfx::SlideAnimation> bounds_animation_;
127   scoped_ptr<gfx::SlideAnimation> fade_animation_;
128 
129   bool is_animating_bounds_;
130   gfx::Rect animated_bounds_start_;
131   gfx::Rect animated_bounds_end_;
132   // Started closing animation, will close at the end.
133   bool is_closing_;
134   // Closing animation - when it ends, close the widget. Weak, only used
135   // for referential equality.
136   gfx::Animation* closing_animation_;
137 
138   gfx::Point origin_;
139   gfx::Size preferred_size_;
140 
141   DISALLOW_COPY_AND_ASSIGN(ToastContentsView);
142 };
143 
144 }  // namespace message_center
145 
146 #endif // UI_MESSAGE_CENTER_VIEWS_TOAST_CONTENTS_VIEW_H_
147