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
18namespace gfx {
19class Animation;
20class SlideAnimation;
21}
22
23namespace views {
24class View;
25}
26
27namespace message_center {
28
29class MessagePopupCollection;
30class MessageView;
31class 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.
37class 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(const 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  void UpdateContents(const Notification& notification,
54                      bool a11y_feedback_for_updates);
55
56  // Shows the new toast for the first time, animated.
57  // |origin| is the right-bottom corner of the toast.
58  void RevealWithAnimation(gfx::Point origin);
59
60  // Disconnectes the toast from the rest of the system immediately and start
61  // an animation. Once animation finishes, closes the widget.
62  void CloseWithAnimation();
63
64  void SetBoundsWithAnimation(gfx::Rect new_bounds);
65
66  // Origin and bounds are not 'instant', but rather 'current stable values',
67  // there could be animation in progress that targets these values.
68  gfx::Point origin() { return origin_; }
69  gfx::Rect bounds() { return gfx::Rect(origin_, preferred_size_); }
70
71  const std::string& id() { return id_; }
72
73  // Overridden from views::View:
74  virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
75  virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
76  virtual void Layout() OVERRIDE;
77  virtual gfx::Size GetPreferredSize() const OVERRIDE;
78  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
79
80 private:
81  // Overridden from MessageCenterController:
82  virtual void ClickOnNotification(const std::string& notification_id) OVERRIDE;
83  virtual void RemoveNotification(const std::string& notification_id,
84                                  bool by_user) OVERRIDE;
85  virtual scoped_ptr<ui::MenuModel> CreateMenuModel(
86      const NotifierId& notifier_id,
87      const base::string16& display_source) OVERRIDE;
88  virtual bool HasClickedListener(const std::string& notification_id) OVERRIDE;
89  virtual void ClickOnNotificationButton(const std::string& notification_id,
90                                         int button_index) OVERRIDE;
91
92  // Overridden from gfx::AnimationDelegate:
93  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
94  virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
95  virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE;
96
97  // Overridden from views::WidgetDelegate:
98  virtual views::View* GetContentsView() OVERRIDE;
99  virtual void WindowClosing() OVERRIDE;
100  virtual void OnDisplayChanged() OVERRIDE;
101  virtual void OnWorkAreaChanged() OVERRIDE;
102
103  // Initialization and update.
104  void CreateWidget(gfx::NativeView parent);
105
106  // Immediately moves the toast without any sort of delay or animation.
107  void SetBoundsInstantly(gfx::Rect new_bounds);
108
109  // Given the bounds of a toast on the screen, compute the bouds for that
110  // toast in 'closed' state. The 'closed' state is used as origin/destination
111  // in reveal/closing animations.
112  gfx::Rect GetClosedToastBounds(gfx::Rect bounds);
113
114  void StartFadeIn();
115  void StartFadeOut();  // Will call Widget::Close() when animation ends.
116  void OnBoundsAnimationEndedOrCancelled(const gfx::Animation* animation);
117
118  base::WeakPtr<MessagePopupCollection> collection_;
119
120  // Id if the corresponding Notification.
121  std::string id_;
122
123  scoped_ptr<gfx::SlideAnimation> bounds_animation_;
124  scoped_ptr<gfx::SlideAnimation> fade_animation_;
125
126  bool is_animating_bounds_;
127  gfx::Rect animated_bounds_start_;
128  gfx::Rect animated_bounds_end_;
129  // Started closing animation, will close at the end.
130  bool is_closing_;
131  // Closing animation - when it ends, close the widget. Weak, only used
132  // for referential equality.
133  gfx::Animation* closing_animation_;
134
135  gfx::Point origin_;
136  gfx::Size preferred_size_;
137
138  DISALLOW_COPY_AND_ASSIGN(ToastContentsView);
139};
140
141}  // namespace message_center
142
143#endif // UI_MESSAGE_CENTER_VIEWS_TOAST_CONTENTS_VIEW_H_
144