Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: chrome/browser/notifications/balloon_collection.cc

Issue 6400006: Fix style problems in Balloon stuff (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/balloon_collection_impl.h" 5 #include "chrome/browser/notifications/balloon_collection.h"
6 6
7 #include "base/logging.h" 7 BalloonCollection::BalloonCollection()
8 #include "base/stl_util-inl.h" 8 : space_change_listener_(NULL) {
9 #include "chrome/browser/notifications/balloon.h"
10 #include "chrome/browser/notifications/balloon_host.h"
11 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/ui/window_sizer.h"
13 #include "gfx/rect.h"
14 #include "gfx/size.h"
15
16 namespace {
17
18 // Portion of the screen allotted for notifications. When notification balloons
19 // extend over this, no new notifications are shown until some are closed.
20 const double kPercentBalloonFillFactor = 0.7;
21
22 // Allow at least this number of balloons on the screen.
23 const int kMinAllowedBalloonCount = 2;
24
25 // Delay from the mouse leaving the balloon collection before
26 // there is a relayout, in milliseconds.
27 const int kRepositionDelay = 300;
28
29 } // namespace
30
31 BalloonCollectionImpl::BalloonCollectionImpl()
32 #if USE_OFFSETS
33 : ALLOW_THIS_IN_INITIALIZER_LIST(reposition_factory_(this)),
34 added_as_message_loop_observer_(false)
35 #endif
36 {
37
38 SetPositionPreference(BalloonCollection::DEFAULT_POSITION);
39 } 9 }
40 10
41 BalloonCollectionImpl::~BalloonCollectionImpl() { 11 BalloonCollection::~BalloonCollection() {
42 } 12 }
43
44 void BalloonCollectionImpl::Add(const Notification& notification,
45 Profile* profile) {
46 Balloon* new_balloon = MakeBalloon(notification, profile);
47 // The +1 on width is necessary because width is fixed on notifications,
48 // so since we always have the max size, we would always hit the scrollbar
49 // condition. We are only interested in comparing height to maximum.
50 new_balloon->set_min_scrollbar_size(gfx::Size(1 + layout_.max_balloon_width(),
51 layout_.max_balloon_height()));
52 new_balloon->SetPosition(layout_.OffScreenLocation(), false);
53 new_balloon->Show();
54 #if USE_OFFSETS
55 int count = base_.count();
56 if (count > 0)
57 new_balloon->set_offset(base_.balloons()[count - 1]->offset());
58 #endif
59 base_.Add(new_balloon);
60 PositionBalloons(false);
61
62 // There may be no listener in a unit test.
63 if (space_change_listener_)
64 space_change_listener_->OnBalloonSpaceChanged();
65
66 // This is used only for testing.
67 if (on_collection_changed_callback_.get())
68 on_collection_changed_callback_->Run();
69 }
70
71 bool BalloonCollectionImpl::RemoveById(const std::string& id) {
72 return base_.CloseById(id);
73 }
74
75 bool BalloonCollectionImpl::RemoveBySourceOrigin(const GURL& origin) {
76 return base_.CloseAllBySourceOrigin(origin);
77 }
78
79 void BalloonCollectionImpl::RemoveAll() {
80 base_.CloseAll();
81 }
82
83 bool BalloonCollectionImpl::HasSpace() const {
84 int count = base_.count();
85 if (count < kMinAllowedBalloonCount)
86 return true;
87
88 int max_balloon_size = 0;
89 int total_size = 0;
90 layout_.GetMaxLinearSize(&max_balloon_size, &total_size);
91
92 int current_max_size = max_balloon_size * count;
93 int max_allowed_size = static_cast<int>(total_size *
94 kPercentBalloonFillFactor);
95 return current_max_size < max_allowed_size - max_balloon_size;
96 }
97
98 void BalloonCollectionImpl::ResizeBalloon(Balloon* balloon,
99 const gfx::Size& size) {
100 balloon->set_content_size(Layout::ConstrainToSizeLimits(size));
101 PositionBalloons(true);
102 }
103
104 void BalloonCollectionImpl::DisplayChanged() {
105 layout_.RefreshSystemMetrics();
106 PositionBalloons(true);
107 }
108
109 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) {
110 // We want to free the balloon when finished.
111 const Balloons& balloons = base_.balloons();
112 Balloons::const_iterator it = balloons.begin();
113
114 #if USE_OFFSETS
115 gfx::Point offset;
116 bool apply_offset = false;
117 while (it != balloons.end()) {
118 if (*it == source) {
119 ++it;
120 if (it != balloons.end()) {
121 apply_offset = true;
122 offset.set_y((source)->offset().y() - (*it)->offset().y() +
123 (*it)->content_size().height() - source->content_size().height());
124 }
125 } else {
126 if (apply_offset)
127 (*it)->add_offset(offset);
128 ++it;
129 }
130 }
131 // Start listening for UI events so we cancel the offset when the mouse
132 // leaves the balloon area.
133 if (apply_offset)
134 AddMessageLoopObserver();
135 #endif
136
137 base_.Remove(source);
138 PositionBalloons(true);
139
140 // There may be no listener in a unit test.
141 if (space_change_listener_)
142 space_change_listener_->OnBalloonSpaceChanged();
143
144 // This is used only for testing.
145 if (on_collection_changed_callback_.get())
146 on_collection_changed_callback_->Run();
147 }
148
149 void BalloonCollectionImpl::PositionBalloonsInternal(bool reposition) {
150 const Balloons& balloons = base_.balloons();
151
152 layout_.RefreshSystemMetrics();
153 gfx::Point origin = layout_.GetLayoutOrigin();
154 for (Balloons::const_iterator it = balloons.begin();
155 it != balloons.end();
156 ++it) {
157 gfx::Point upper_left = layout_.NextPosition((*it)->GetViewSize(), &origin);
158 (*it)->SetPosition(upper_left, reposition);
159 }
160 }
161
162 gfx::Rect BalloonCollectionImpl::GetBalloonsBoundingBox() const {
163 // Start from the layout origin.
164 gfx::Rect bounds = gfx::Rect(layout_.GetLayoutOrigin(), gfx::Size(0, 0));
165
166 // For each balloon, extend the rectangle. This approach is indifferent to
167 // the orientation of the balloons.
168 const Balloons& balloons = base_.balloons();
169 Balloons::const_iterator iter;
170 for (iter = balloons.begin(); iter != balloons.end(); ++iter) {
171 gfx::Rect balloon_box = gfx::Rect((*iter)->GetPosition(),
172 (*iter)->GetViewSize());
173 bounds = bounds.Union(balloon_box);
174 }
175
176 return bounds;
177 }
178
179 #if USE_OFFSETS
180 void BalloonCollectionImpl::AddMessageLoopObserver() {
181 if (!added_as_message_loop_observer_) {
182 MessageLoopForUI::current()->AddObserver(this);
183 added_as_message_loop_observer_ = true;
184 }
185 }
186
187 void BalloonCollectionImpl::RemoveMessageLoopObserver() {
188 if (added_as_message_loop_observer_) {
189 MessageLoopForUI::current()->RemoveObserver(this);
190 added_as_message_loop_observer_ = false;
191 }
192 }
193
194 void BalloonCollectionImpl::CancelOffsets() {
195 reposition_factory_.RevokeAll();
196
197 // Unhook from listening to all UI events.
198 RemoveMessageLoopObserver();
199
200 const Balloons& balloons = base_.balloons();
201 for (Balloons::const_iterator it = balloons.begin();
202 it != balloons.end();
203 ++it)
204 (*it)->set_offset(gfx::Point(0, 0));
205
206 PositionBalloons(true);
207 }
208
209 void BalloonCollectionImpl::HandleMouseMoveEvent() {
210 if (!IsCursorInBalloonCollection()) {
211 // Mouse has left the region. Schedule a reposition after
212 // a short delay.
213 if (reposition_factory_.empty()) {
214 MessageLoop::current()->PostDelayedTask(
215 FROM_HERE,
216 reposition_factory_.NewRunnableMethod(
217 &BalloonCollectionImpl::CancelOffsets),
218 kRepositionDelay);
219 }
220 } else {
221 // Mouse moved back into the region. Cancel the reposition.
222 reposition_factory_.RevokeAll();
223 }
224 }
225 #endif
226
227 BalloonCollectionImpl::Layout::Layout() {
228 RefreshSystemMetrics();
229 }
230
231 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size,
232 int* total_size) const {
233 DCHECK(max_balloon_size && total_size);
234
235 // All placement schemes are vertical, so we only care about height.
236 *total_size = work_area_.height();
237 *max_balloon_size = max_balloon_height();
238 }
239
240 gfx::Point BalloonCollectionImpl::Layout::GetLayoutOrigin() const {
241 int x = 0;
242 int y = 0;
243 switch (placement_) {
244 case VERTICALLY_FROM_TOP_LEFT:
245 x = work_area_.x() + HorizontalEdgeMargin();
246 y = work_area_.y() + VerticalEdgeMargin();
247 break;
248 case VERTICALLY_FROM_TOP_RIGHT:
249 x = work_area_.right() - HorizontalEdgeMargin();
250 y = work_area_.y() + VerticalEdgeMargin();
251 break;
252 case VERTICALLY_FROM_BOTTOM_LEFT:
253 x = work_area_.x() + HorizontalEdgeMargin();
254 y = work_area_.bottom() - VerticalEdgeMargin();
255 break;
256 case VERTICALLY_FROM_BOTTOM_RIGHT:
257 x = work_area_.right() - HorizontalEdgeMargin();
258 y = work_area_.bottom() - VerticalEdgeMargin();
259 break;
260 default:
261 NOTREACHED();
262 break;
263 }
264 return gfx::Point(x, y);
265 }
266
267 gfx::Point BalloonCollectionImpl::Layout::NextPosition(
268 const gfx::Size& balloon_size,
269 gfx::Point* position_iterator) const {
270 DCHECK(position_iterator);
271
272 int x = 0;
273 int y = 0;
274 switch (placement_) {
275 case VERTICALLY_FROM_TOP_LEFT:
276 x = position_iterator->x();
277 y = position_iterator->y();
278 position_iterator->set_y(position_iterator->y() + balloon_size.height() +
279 InterBalloonMargin());
280 break;
281 case VERTICALLY_FROM_TOP_RIGHT:
282 x = position_iterator->x() - balloon_size.width();
283 y = position_iterator->y();
284 position_iterator->set_y(position_iterator->y() + balloon_size.height() +
285 InterBalloonMargin());
286 break;
287 case VERTICALLY_FROM_BOTTOM_LEFT:
288 position_iterator->set_y(position_iterator->y() - balloon_size.height() -
289 InterBalloonMargin());
290 x = position_iterator->x();
291 y = position_iterator->y();
292 break;
293 case VERTICALLY_FROM_BOTTOM_RIGHT:
294 position_iterator->set_y(position_iterator->y() - balloon_size.height() -
295 InterBalloonMargin());
296 x = position_iterator->x() - balloon_size.width();
297 y = position_iterator->y();
298 break;
299 default:
300 NOTREACHED();
301 break;
302 }
303 return gfx::Point(x, y);
304 }
305
306 gfx::Point BalloonCollectionImpl::Layout::OffScreenLocation() const {
307 int x = 0;
308 int y = 0;
309 switch (placement_) {
310 case VERTICALLY_FROM_TOP_LEFT:
311 x = work_area_.x() + HorizontalEdgeMargin();
312 y = work_area_.y() + kBalloonMaxHeight + VerticalEdgeMargin();
313 break;
314 case VERTICALLY_FROM_TOP_RIGHT:
315 x = work_area_.right() - kBalloonMaxWidth - HorizontalEdgeMargin();
316 y = work_area_.y() + kBalloonMaxHeight + VerticalEdgeMargin();
317 break;
318 case VERTICALLY_FROM_BOTTOM_LEFT:
319 x = work_area_.x() + HorizontalEdgeMargin();
320 y = work_area_.bottom() + kBalloonMaxHeight + VerticalEdgeMargin();
321 break;
322 case VERTICALLY_FROM_BOTTOM_RIGHT:
323 x = work_area_.right() - kBalloonMaxWidth - HorizontalEdgeMargin();
324 y = work_area_.bottom() + kBalloonMaxHeight + VerticalEdgeMargin();
325 break;
326 default:
327 NOTREACHED();
328 break;
329 }
330 return gfx::Point(x, y);
331 }
332
333 // static
334 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits(
335 const gfx::Size& size) {
336 // restrict to the min & max sizes
337 return gfx::Size(
338 std::max(min_balloon_width(),
339 std::min(max_balloon_width(), size.width())),
340 std::max(min_balloon_height(),
341 std::min(max_balloon_height(), size.height())));
342 }
343
344 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() {
345 bool changed = false;
346
347 #if defined(OS_MACOSX)
348 gfx::Rect new_work_area = GetMacWorkArea();
349 #else
350 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider(
351 WindowSizer::CreateDefaultMonitorInfoProvider());
352 gfx::Rect new_work_area = info_provider->GetPrimaryMonitorWorkArea();
353 #endif
354 if (!work_area_.Equals(new_work_area)) {
355 work_area_.SetRect(new_work_area.x(), new_work_area.y(),
356 new_work_area.width(), new_work_area.height());
357 changed = true;
358 }
359
360 return changed;
361 }
OLDNEW
« no previous file with comments | « chrome/browser/notifications/balloon_collection.h ('k') | chrome/browser/notifications/balloon_collection_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698