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 #ifndef UI_MESSAGE_CENTER_VIEWS_MESSAGE_POPUP_COLLECTION_H_
6 #define UI_MESSAGE_CENTER_VIEWS_MESSAGE_POPUP_COLLECTION_H_
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/timer/timer.h"
15 #include "ui/gfx/display.h"
16 #include "ui/gfx/display_observer.h"
17 #include "ui/gfx/native_widget_types.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/message_center/message_center_export.h"
20 #include "ui/message_center/message_center_observer.h"
21 #include "ui/views/widget/widget_observer.h"
32 class WebNotificationTrayTest
;
33 FORWARD_DECLARE_TEST(WebNotificationTrayTest
, ManyPopupNotifications
);
36 namespace message_center
{
38 class MessagePopupCollectionTest
;
39 class MessagePopupCollectionWidgetsTest
;
43 class MessageCenterTray
;
44 class ToastContentsView
;
47 POPUP_ALIGNMENT_TOP
= 1 << 0,
48 POPUP_ALIGNMENT_LEFT
= 1 << 1,
49 POPUP_ALIGNMENT_BOTTOM
= 1 << 2,
50 POPUP_ALIGNMENT_RIGHT
= 1 << 3,
53 // Container for popup toasts. Because each toast is a frameless window rather
54 // than a view in a bubble, now the container just manages all of those toasts.
55 // This is similar to chrome/browser/notifications/balloon_collection, but the
56 // contents of each toast are for the message center and layout strategy would
57 // be slightly different.
58 class MESSAGE_CENTER_EXPORT MessagePopupCollection
59 : public MessageCenterObserver
,
60 public gfx::DisplayObserver
,
61 public base::SupportsWeakPtr
<MessagePopupCollection
> {
63 // |parent| specifies the parent widget of the toast windows. The default
64 // parent will be used for NULL. Usually each icon is spacing against its
65 // predecessor. If |first_item_has_no_margin| is set however the first item
66 // does not space against the tray.
67 MessagePopupCollection(gfx::NativeView parent
,
68 MessageCenter
* message_center
,
69 MessageCenterTray
* tray
,
70 bool first_item_has_no_margin
);
71 virtual ~MessagePopupCollection();
73 // Called by ToastContentsView when its window is closed.
74 void RemoveToast(ToastContentsView
* toast
);
76 // Since these events are really coming from individual toast widgets,
77 // it helps to be able to keep track of the sender.
78 void OnMouseEntered(ToastContentsView
* toast_entered
);
79 void OnMouseExited(ToastContentsView
* toast_exited
);
81 // Invoked by toasts when they start/finish their animations.
82 // While "defer counter" is greater then zero, the popup collection does
83 // not perform updates. It is used to wait for various animations and user
84 // actions like serial closing of the toasts, when the remaining toasts "flow
86 void IncrementDeferCounter();
87 void DecrementDeferCounter();
89 // Runs the next step in update/animate sequence, if the defer counter is not
90 // zero. Otherwise, simply waits when it becomes zero.
91 void DoUpdateIfPossible();
93 // Updates |work_area_| and re-calculates the alignment of notification toasts
94 // rearranging them if necessary.
95 // This is separated from methods from OnDisplayBoundsChanged(), since
96 // sometimes the display info has to be specified directly. One example is
97 // shelf's auto-hide change. When the shelf in ChromeOS is temporarily shown
98 // from auto hide status, it doesn't change the display's work area but the
99 // actual work area for toasts should be resized.
100 void SetDisplayInfo(const gfx::Rect
& work_area
,
101 const gfx::Rect
& screen_bounds
);
103 // Overridden from gfx::DislayObserver:
104 virtual void OnDisplayBoundsChanged(const gfx::Display
& display
) OVERRIDE
;
105 virtual void OnDisplayAdded(const gfx::Display
& new_display
) OVERRIDE
;
106 virtual void OnDisplayRemoved(const gfx::Display
& old_display
) OVERRIDE
;
109 FRIEND_TEST_ALL_PREFIXES(ash::WebNotificationTrayTest
,
110 ManyPopupNotifications
);
111 friend class test::MessagePopupCollectionTest
;
112 friend class test::MessagePopupCollectionWidgetsTest
;
113 friend class ash::WebNotificationTrayTest
;
114 typedef std::list
<ToastContentsView
*> Toasts
;
116 void CloseAllWidgets();
118 // Returns the x-origin for the given toast bounds in the current work area.
119 int GetToastOriginX(const gfx::Rect
& toast_bounds
);
121 // Iterates toasts and starts closing the expired ones.
124 // Creates new widgets for new toast notifications, and updates |toasts_| and
125 // |widgets_| correctly.
126 void UpdateWidgets();
128 // Repositions all of the widgets based on the current work area.
129 void RepositionWidgets();
131 // Repositions widgets to the top edge of the notification toast that was
132 // just removed, so that the user can click close button without mouse moves.
133 // See crbug.com/224089
134 void RepositionWidgetsWithTarget();
136 void ComputePopupAlignment(gfx::Rect work_area
, gfx::Rect screen_bounds
);
138 // The base line is an (imaginary) line that would touch the bottom of the
139 // next created notification if bottom-aligned or its top if top-aligned.
140 int GetBaseLine(ToastContentsView
* last_toast
);
142 // Overridden from MessageCenterObserver:
143 virtual void OnNotificationAdded(const std::string
& notification_id
) OVERRIDE
;
144 virtual void OnNotificationRemoved(const std::string
& notification_id
,
145 bool by_user
) OVERRIDE
;
146 virtual void OnNotificationUpdated(
147 const std::string
& notification_id
) OVERRIDE
;
149 ToastContentsView
* FindToast(const std::string
& notification_id
);
151 // While the toasts are animated, avoid updating the collection, to reduce
152 // user confusion. Instead, update the collection when all animations are
153 // done. This method is run when defer counter is zero, may initiate next
154 // update/animation step.
155 void PerformDeferredTasks();
156 void OnDeferTimerExpired();
158 // "ForTest" methods.
159 views::Widget
* GetWidgetForTest(const std::string
& id
);
160 void RunLoopForTest();
161 gfx::Rect
GetToastRectAt(size_t index
);
163 gfx::NativeView parent_
;
164 MessageCenter
* message_center_
;
165 MessageCenterTray
* tray_
;
167 gfx::Rect work_area_
;
170 // Specifies which corner of the screen popups should show up. This should
171 // ideally be the same corner the notification area (systray) is at.
172 PopupAlignment alignment_
;
176 // This is only used to compare with incoming events, do not assume that
177 // the toast will be valid if this pointer is non-NULL.
178 ToastContentsView
* latest_toast_entered_
;
180 // Denotes a mode when user is clicking the Close button of toasts in a
181 // sequence, w/o moving the mouse. We reposition the toasts so the next one
182 // happens to be right under the mouse, and the user can just dispose of
183 // multipel toasts by clicking. The mode ends when defer_timer_ expires.
184 bool user_is_closing_toasts_by_clicking_
;
185 scoped_ptr
<base::OneShotTimer
<MessagePopupCollection
> > defer_timer_
;
186 // The top edge to align the position of the next toast during 'close by
188 // Only to be used when user_is_closing_toasts_by_clicking_ is true.
189 int target_top_edge_
;
191 // Weak, only exists temporarily in tests.
192 scoped_ptr
<base::RunLoop
> run_loop_for_test_
;
194 // True if the first item should not have spacing against the tray.
195 bool first_item_has_no_margin_
;
197 DISALLOW_COPY_AND_ASSIGN(MessagePopupCollection
);
200 } // namespace message_center
202 #endif // UI_MESSAGE_CENTER_VIEWS_MESSAGE_POPUP_COLLECTION_H_