OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // Handles the visible notification (or balloons). | |
6 | |
7 #ifndef CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_ | |
8 #define CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_ | |
9 | |
10 #include <deque> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "chrome/browser/notifications/balloon_collection.h" | |
17 #include "chrome/browser/notifications/balloon_collection_base.h" | |
18 #include "content/public/browser/notification_observer.h" | |
19 #include "content/public/browser/notification_registrar.h" | |
20 #include "ui/gfx/point.h" | |
21 #include "ui/gfx/rect.h" | |
22 | |
23 // A balloon collection represents a set of notification balloons being | |
24 // shown on the screen. It positions new notifications according to | |
25 // a layout, and monitors for balloons being closed, which it reports | |
26 // up to its parent, the notification UI manager. | |
27 class BalloonCollectionImpl : public BalloonCollection, | |
28 public content::NotificationObserver, | |
29 public base::MessageLoopForUI::Observer { | |
30 public: | |
31 BalloonCollectionImpl(); | |
32 virtual ~BalloonCollectionImpl(); | |
33 | |
34 // BalloonCollection interface. | |
35 virtual void Add(const Notification& notification, | |
36 Profile* profile) OVERRIDE; | |
37 virtual const Notification* FindById(const std::string& id) const OVERRIDE; | |
38 virtual bool RemoveById(const std::string& id) OVERRIDE; | |
39 virtual bool RemoveBySourceOrigin(const GURL& source_origin) OVERRIDE; | |
40 virtual bool RemoveByProfile(Profile* profile) OVERRIDE; | |
41 virtual void RemoveAll() OVERRIDE; | |
42 virtual bool HasSpace() const OVERRIDE; | |
43 virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size) OVERRIDE; | |
44 virtual void SetPositionPreference(PositionPreference position) OVERRIDE; | |
45 virtual void DisplayChanged() OVERRIDE; | |
46 virtual void OnBalloonClosed(Balloon* source) OVERRIDE; | |
47 virtual const Balloons& GetActiveBalloons() OVERRIDE; | |
48 | |
49 // content::NotificationObserver interface. | |
50 virtual void Observe(int type, | |
51 const content::NotificationSource& source, | |
52 const content::NotificationDetails& details) OVERRIDE; | |
53 | |
54 // MessageLoopForUI::Observer interface. | |
55 virtual void WillProcessEvent(GdkEvent* event) OVERRIDE; | |
56 virtual void DidProcessEvent(GdkEvent* event) OVERRIDE; | |
57 | |
58 // base_ is embedded, so this is a simple accessor for the number of | |
59 // balloons in the collection. | |
60 int count() const { return base_.count(); } | |
61 | |
62 static int min_balloon_width() { return Layout::min_balloon_width(); } | |
63 static int max_balloon_width() { return Layout::max_balloon_width(); } | |
64 static int min_balloon_height() { return Layout::min_balloon_height(); } | |
65 static int max_balloon_height() { return Layout::max_balloon_height(); } | |
66 | |
67 protected: | |
68 // Calculates layout values for the balloons including | |
69 // the scaling, the max/min sizes, and the upper left corner of each. | |
70 class Layout { | |
71 public: | |
72 Layout(); | |
73 | |
74 // These enumerations all are based on a screen orientation where | |
75 // the origin is the top-left. | |
76 enum Placement { | |
77 INVALID, | |
78 VERTICALLY_FROM_TOP_LEFT, | |
79 VERTICALLY_FROM_TOP_RIGHT, | |
80 VERTICALLY_FROM_BOTTOM_LEFT, | |
81 VERTICALLY_FROM_BOTTOM_RIGHT | |
82 }; | |
83 | |
84 // Refresh the work area and balloon placement. | |
85 void OnDisplaySettingsChanged(); | |
86 | |
87 // TODO(johnnyg): Scale the size to account for the system font factor. | |
88 static int min_balloon_width() { return kBalloonMinWidth; } | |
89 static int max_balloon_width() { return kBalloonMaxWidth; } | |
90 static int min_balloon_height() { return kBalloonMinHeight; } | |
91 static int max_balloon_height() { return kBalloonMaxHeight; } | |
92 | |
93 // Utility function constrains the input rectangle to the min and max sizes. | |
94 static gfx::Size ConstrainToSizeLimits(const gfx::Size& rect); | |
95 | |
96 void set_placement(Placement placement) { placement_ = placement; } | |
97 | |
98 // Returns both the total space available and the maximum | |
99 // allowed per balloon. | |
100 // | |
101 // The size may be a height or length depending on the way that | |
102 // balloons are laid out. | |
103 void GetMaxLinearSize(int* max_balloon_size, int* total_size) const; | |
104 | |
105 // Refresh the cached values for work area and drawing metrics. | |
106 // The application should call this method to re-acquire metrics after | |
107 // any resolution or settings change. | |
108 // Returns true if and only if a metric changed. | |
109 bool RefreshSystemMetrics(); | |
110 | |
111 // Returns the origin for the sequence of balloons depending on layout. | |
112 // Should not be used to place a balloon -- only to call NextPosition(). | |
113 gfx::Point GetLayoutOrigin() const; | |
114 | |
115 // Compute the position for the next balloon. | |
116 // Start with *position_iterator = GetLayoutOrigin() and call repeatedly | |
117 // to get a sequence of positions. Return value is the upper-left coordinate | |
118 // for each next balloon. | |
119 gfx::Point NextPosition(const gfx::Size& balloon_size, | |
120 gfx::Point* position_iterator) const; | |
121 | |
122 // Return a offscreen location which is offscreen for this layout, | |
123 // to be used as the initial position for an animation into view. | |
124 gfx::Point OffScreenLocation() const; | |
125 | |
126 // Returns true if the layout requires offsetting for keeping the close | |
127 // buttons under the cursor during rapid-close interaction. | |
128 bool RequiresOffsets() const; | |
129 | |
130 // Returns true if there is change to the offset that requires the balloons | |
131 // to be repositioned. | |
132 bool ComputeOffsetToMoveAbovePanels(); | |
133 | |
134 void enable_computing_panel_offset() { | |
135 need_to_compute_panel_offset_ = true; | |
136 } | |
137 | |
138 private: | |
139 // Layout parameters | |
140 int VerticalEdgeMargin() const; | |
141 int HorizontalEdgeMargin() const; | |
142 int InterBalloonMargin() const; | |
143 | |
144 bool NeedToMoveAboveLeftSidePanels() const; | |
145 bool NeedToMoveAboveRightSidePanels() const; | |
146 | |
147 // Minimum and maximum size of balloon content. | |
148 static const int kBalloonMinWidth = 300; | |
149 static const int kBalloonMaxWidth = 300; | |
150 static const int kBalloonMinHeight = 24; | |
151 static const int kBalloonMaxHeight = 160; | |
152 | |
153 Placement placement_; | |
154 gfx::Rect work_area_; | |
155 | |
156 // The flag that indicates that the panel offset computation should be | |
157 // performed. | |
158 bool need_to_compute_panel_offset_; | |
159 | |
160 // The offset that guarantees that the notificaitions shown in the | |
161 // lower-right or lower-left corner of the screen will go above currently | |
162 // shown panels and will not be obscured by them. | |
163 int offset_to_move_above_panels_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(Layout); | |
166 }; | |
167 | |
168 // Creates a new balloon. Overridable by derived classes and unit tests. | |
169 // The caller is responsible for freeing the pointer returned. | |
170 virtual Balloon* MakeBalloon(const Notification& notification, | |
171 Profile* profile); | |
172 | |
173 // Protected implementation of Add with additional add_to_front parameter | |
174 // for use by derived classes. | |
175 void AddImpl(const Notification& notification, | |
176 Profile* profile, | |
177 bool add_to_front); | |
178 | |
179 // Gets a bounding box for all the current balloons in screen coordinates. | |
180 gfx::Rect GetBalloonsBoundingBox() const; | |
181 | |
182 BalloonCollectionBase& base() { return base_; } | |
183 Layout& layout() { return layout_; } | |
184 | |
185 private: | |
186 // Adjusts the positions of the balloons (e.g., when one is closed). | |
187 // Implemented by each platform for specific UI requirements. | |
188 void PositionBalloons(bool is_reposition); | |
189 | |
190 // Cross-platform internal implementation for PositionBalloons. | |
191 void PositionBalloonsInternal(bool is_reposition); | |
192 | |
193 // Base implementation for the collection of active balloons. | |
194 BalloonCollectionBase base_; | |
195 | |
196 // The layout parameters for balloons in this collection. | |
197 Layout layout_; | |
198 | |
199 content::NotificationRegistrar registrar_; | |
200 | |
201 // Start and stop observing all UI events. | |
202 void AddMessageLoopObserver(); | |
203 void RemoveMessageLoopObserver(); | |
204 | |
205 // Cancel all offset and reposition the balloons normally. | |
206 void CancelOffsets(); | |
207 | |
208 // Handles a mouse motion while the balloons are temporarily offset. | |
209 void HandleMouseMoveEvent(); | |
210 | |
211 // Is the current cursor in the balloon area? | |
212 bool IsCursorInBalloonCollection() const; | |
213 | |
214 // Factory for generating delayed reposition tasks on mouse motion. | |
215 base::WeakPtrFactory<BalloonCollectionImpl> reposition_factory_; | |
216 | |
217 // Is the balloon collection currently listening for UI events? | |
218 bool added_as_message_loop_observer_; | |
219 | |
220 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl); | |
221 }; | |
222 | |
223 #endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_ | |
OLD | NEW |