OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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_BALLOONS_H_ | |
8 #define CHROME_BROWSER_NOTIFICATIONS_BALLOONS_H_ | |
9 | |
10 #include <deque> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/gfx/point.h" | |
15 #include "base/gfx/rect.h" | |
16 #include "base/gfx/size.h" | |
17 #include "base/scoped_ptr.h" | |
18 #include "chrome/browser/notifications/notification.h" | |
19 | |
20 class Balloon; | |
21 class BalloonView; | |
22 class Profile; | |
23 class SiteInstance; | |
24 | |
25 class BalloonCloseListener { | |
26 public: | |
27 virtual ~BalloonCloseListener() { } | |
28 | |
29 // Called when a balloon is closed. | |
30 virtual void OnBalloonClosed(Balloon* source) = 0; | |
31 }; | |
32 | |
33 class BalloonSpaceChangeListener { | |
34 public: | |
35 virtual ~BalloonSpaceChangeListener() { } | |
36 | |
37 // Called when there is more or less space for balloons due to | |
38 // monitor size changes or balloons disappearing. | |
39 virtual void OnBalloonSpaceChanged() = 0; | |
40 }; | |
41 | |
42 class BalloonCollectionInterface { | |
43 public: | |
44 virtual ~BalloonCollectionInterface() {} | |
45 | |
46 // Adds a new balloon for the specified notification. | |
47 virtual void Add(const Notification& notification, | |
48 Profile* profile, | |
49 SiteInstance* site_instance) = 0; | |
50 | |
51 // Is there room to add another notification? | |
52 virtual bool HasSpace() const = 0; | |
53 }; | |
54 | |
55 typedef std::deque<Balloon*> Balloons; | |
56 | |
57 // Represents a Notification on the screen. | |
58 class Balloon { | |
59 public: | |
60 Balloon(const Notification& notification, | |
61 Profile* profile, | |
62 SiteInstance* site_instance, | |
63 BalloonCloseListener* listener); | |
64 ~Balloon(); | |
65 | |
66 const Notification& notification() const { | |
67 return notification_; | |
68 } | |
69 | |
70 Profile* profile() const { | |
71 return profile_; | |
72 } | |
73 | |
74 SiteInstance* site_instance() const { | |
75 return site_instance_; | |
76 } | |
77 | |
78 void SetPosition(const gfx::Point& upper_left, bool reposition); | |
79 void SetSize(const gfx::Size& size); | |
80 void Show(); | |
81 void Close(); | |
82 | |
83 const gfx::Point& position() const; | |
84 const gfx::Size& size() const; | |
85 | |
86 private: | |
87 Profile* profile_; | |
88 SiteInstance* site_instance_; | |
89 Notification notification_; | |
90 BalloonCloseListener* close_listener_; | |
91 scoped_ptr<BalloonView> balloon_view_; | |
92 gfx::Point position_; | |
93 gfx::Size size_; | |
94 DISALLOW_COPY_AND_ASSIGN(Balloon); | |
95 }; | |
96 | |
97 class BalloonCollection : public BalloonCollectionInterface, | |
98 public BalloonCloseListener { | |
99 public: | |
100 explicit BalloonCollection(); | |
101 | |
102 BalloonSpaceChangeListener* space_change_listener() { return listener_; } | |
103 void set_space_change_listener(BalloonSpaceChangeListener* listener) { | |
104 listener_ = listener; | |
105 } | |
106 | |
107 // BalloonCollectionInterface overrides | |
108 virtual void Add(const Notification& notification, | |
109 Profile* profile, | |
110 SiteInstance* site_instance); | |
111 virtual void ShowAll(); | |
112 virtual void HideAll(); | |
113 virtual bool HasSpace() const; | |
114 | |
115 // BalloonCloseListener interface | |
116 virtual void OnBalloonClosed(Balloon* source); | |
117 | |
118 protected: | |
119 // Overridable by unit tests. | |
120 virtual Balloon* MakeBalloon(const Notification& notification, | |
121 Profile* profile, | |
122 SiteInstance* site_instance) { | |
123 return new Balloon(notification, profile, site_instance, this); | |
124 } | |
125 | |
126 private: | |
127 // The number of balloons being displayed. | |
128 int count() const { return balloons_.size(); } | |
129 | |
130 // Calculates layout values for the balloons including | |
131 // the scaling, the max/min sizes, and the upper left corner of each. | |
132 class Layout { | |
133 public: | |
134 Layout(); | |
135 | |
136 // Refresh the work area and balloon placement. | |
137 void OnDisplaySettingsChanged(); | |
138 | |
139 int min_balloon_width() const; | |
140 int max_balloon_width() const; | |
141 int min_balloon_height() const; | |
142 int max_balloon_height() const; | |
143 | |
144 // Returns both the total length available and the maximum | |
145 // allowed per balloon. | |
146 // | |
147 // The length may be a height or length depending on the way that | |
148 // balloons are laid out. | |
149 const void GetMaxLengths(int* max_balloon_length, int* total_length) const; | |
150 | |
151 // Scale the size to count in the system font factor. | |
152 int ScaleSize(int size) const; | |
153 | |
154 // Refresh the cached values for work area and drawing metrics. | |
155 // This is done automatically first time and the application should | |
156 // call this method to re-acquire metrics after any | |
157 // resolution or settings change. | |
158 // | |
159 // Return true if and only if a metric changed. | |
160 bool RefreshSystemMetrics(); | |
161 | |
162 // Returns the starting value for NextUpperLeftPosition. | |
163 gfx::Point GetStartPosition() const; | |
164 | |
165 // Compute the position for the next balloon. | |
166 // Modifies origin. | |
167 // Returns the position of the upper left coordinate for the given | |
168 // balloon. | |
169 gfx::Point NextPosition(const gfx::Size& balloon_size, | |
170 gfx::Point* origin) const; | |
171 | |
172 private: | |
173 enum Placement { | |
174 HORIZONTALLY_FROM_BOTTOM_LEFT, | |
175 HORIZONTALLY_FROM_BOTTOM_RIGHT, | |
176 VERTICALLY_FROM_TOP_RIGHT, | |
177 VERTICALLY_FROM_BOTTOM_RIGHT | |
178 }; | |
179 | |
180 // Minimum and maximum size of balloon | |
181 static const int kBalloonMinWidth = 300; | |
182 static const int kBalloonMaxWidth = 300; | |
183 static const int kBalloonMinHeight = 90; | |
184 static const int kBalloonMaxHeight = 120; | |
185 | |
186 static Placement placement_; | |
187 gfx::Rect work_area_; | |
188 double font_scale_factor_; | |
189 DISALLOW_COPY_AND_ASSIGN(Layout); | |
190 }; | |
191 | |
192 // Non-owned pointer to an object listening for space changes. | |
193 BalloonSpaceChangeListener* listener_; | |
194 | |
195 Balloons balloons_; | |
196 Layout layout_; | |
197 DISALLOW_COPY_AND_ASSIGN(BalloonCollection); | |
198 }; | |
199 | |
200 #endif // CHROME_BROWSER_NOTIFICATIONS_BALLOONS_H_ | |
OLD | NEW |