OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 // Portion of the screen allotted for notifications. When notification balloons | 24 // Portion of the screen allotted for notifications. When notification balloons |
25 // extend over this, no new notifications are shown until some are closed. | 25 // extend over this, no new notifications are shown until some are closed. |
26 const double kPercentBalloonFillFactor = 0.7; | 26 const double kPercentBalloonFillFactor = 0.7; |
27 | 27 |
28 // Allow at least this number of balloons on the screen. | 28 // Allow at least this number of balloons on the screen. |
29 const int kMinAllowedBalloonCount = 2; | 29 const int kMinAllowedBalloonCount = 2; |
30 | 30 |
31 // The spacing between the balloon and the panel. | 31 // The spacing between the balloon and the panel. |
32 const int kVerticalSpacingBetweenBalloonAndPanel = 5; | 32 const int kVerticalSpacingBetweenBalloonAndPanel = 5; |
33 | 33 |
34 #if USE_OFFSETS | |
35 // Delay from the mouse leaving the balloon collection before | 34 // Delay from the mouse leaving the balloon collection before |
36 // there is a relayout, in milliseconds. | 35 // there is a relayout, in milliseconds. |
37 const int kRepositionDelayMs = 300; | 36 const int kRepositionDelayMs = 300; |
38 #endif // USE_OFFSETS | |
39 | 37 |
40 | 38 |
41 BalloonCollectionImpl::BalloonCollectionImpl() | 39 BalloonCollectionImpl::BalloonCollectionImpl() |
42 #if USE_OFFSETS | |
43 : reposition_factory_(this), | 40 : reposition_factory_(this), |
44 added_as_message_loop_observer_(false) | 41 added_as_message_loop_observer_(false) { |
45 #endif | |
46 { | |
47 registrar_.Add(this, chrome::NOTIFICATION_PANEL_COLLECTION_UPDATED, | 42 registrar_.Add(this, chrome::NOTIFICATION_PANEL_COLLECTION_UPDATED, |
48 content::NotificationService::AllSources()); | 43 content::NotificationService::AllSources()); |
49 registrar_.Add(this, chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE, | 44 registrar_.Add(this, chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE, |
50 content::NotificationService::AllSources()); | 45 content::NotificationService::AllSources()); |
51 | 46 |
52 SetPositionPreference(BalloonCollection::DEFAULT_POSITION); | 47 SetPositionPreference(BalloonCollection::DEFAULT_POSITION); |
53 } | 48 } |
54 | 49 |
55 BalloonCollectionImpl::~BalloonCollectionImpl() { | 50 BalloonCollectionImpl::~BalloonCollectionImpl() { |
56 #if USE_OFFSETS | |
57 RemoveMessageLoopObserver(); | 51 RemoveMessageLoopObserver(); |
58 #endif | |
59 } | 52 } |
60 | 53 |
61 void BalloonCollectionImpl::AddImpl(const Notification& notification, | 54 void BalloonCollectionImpl::AddImpl(const Notification& notification, |
62 Profile* profile, | 55 Profile* profile, |
63 bool add_to_front) { | 56 bool add_to_front) { |
64 Balloon* new_balloon = MakeBalloon(notification, profile); | 57 Balloon* new_balloon = MakeBalloon(notification, profile); |
65 // The +1 on width is necessary because width is fixed on notifications, | 58 // The +1 on width is necessary because width is fixed on notifications, |
66 // so since we always have the max size, we would always hit the scrollbar | 59 // so since we always have the max size, we would always hit the scrollbar |
67 // condition. We are only interested in comparing height to maximum. | 60 // condition. We are only interested in comparing height to maximum. |
68 new_balloon->set_min_scrollbar_size(gfx::Size(1 + layout_.max_balloon_width(), | 61 new_balloon->set_min_scrollbar_size(gfx::Size(1 + layout_.max_balloon_width(), |
69 layout_.max_balloon_height())); | 62 layout_.max_balloon_height())); |
70 new_balloon->SetPosition(layout_.OffScreenLocation(), false); | 63 new_balloon->SetPosition(layout_.OffScreenLocation(), false); |
71 new_balloon->Show(); | 64 new_balloon->Show(); |
72 #if USE_OFFSETS | |
73 int count = base_.count(); | 65 int count = base_.count(); |
74 if (count > 0 && layout_.RequiresOffsets()) | 66 if (count > 0 && layout_.RequiresOffsets()) |
75 new_balloon->set_offset(base_.balloons()[count - 1]->offset()); | 67 new_balloon->set_offset(base_.balloons()[count - 1]->offset()); |
76 #endif | |
77 base_.Add(new_balloon, add_to_front); | 68 base_.Add(new_balloon, add_to_front); |
78 PositionBalloons(false); | 69 PositionBalloons(false); |
79 | 70 |
80 // There may be no listener in a unit test. | 71 // There may be no listener in a unit test. |
81 if (space_change_listener_) | 72 if (space_change_listener_) |
82 space_change_listener_->OnBalloonSpaceChanged(); | 73 space_change_listener_->OnBalloonSpaceChanged(); |
83 | 74 |
84 // This is used only for testing. | 75 // This is used only for testing. |
85 if (!on_collection_changed_callback_.is_null()) | 76 if (!on_collection_changed_callback_.is_null()) |
86 on_collection_changed_callback_.Run(); | 77 on_collection_changed_callback_.Run(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 balloon->set_content_size(Layout::ConstrainToSizeLimits(size)); | 123 balloon->set_content_size(Layout::ConstrainToSizeLimits(size)); |
133 PositionBalloons(true); | 124 PositionBalloons(true); |
134 } | 125 } |
135 | 126 |
136 void BalloonCollectionImpl::DisplayChanged() { | 127 void BalloonCollectionImpl::DisplayChanged() { |
137 layout_.RefreshSystemMetrics(); | 128 layout_.RefreshSystemMetrics(); |
138 PositionBalloons(true); | 129 PositionBalloons(true); |
139 } | 130 } |
140 | 131 |
141 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { | 132 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { |
142 #if USE_OFFSETS | |
143 // We want to free the balloon when finished. | 133 // We want to free the balloon when finished. |
144 const Balloons& balloons = base_.balloons(); | 134 const Balloons& balloons = base_.balloons(); |
145 | 135 |
146 Balloons::const_iterator it = balloons.begin(); | 136 Balloons::const_iterator it = balloons.begin(); |
147 if (layout_.RequiresOffsets()) { | 137 if (layout_.RequiresOffsets()) { |
148 gfx::Vector2d offset; | 138 gfx::Vector2d offset; |
149 bool apply_offset = false; | 139 bool apply_offset = false; |
150 while (it != balloons.end()) { | 140 while (it != balloons.end()) { |
151 if (*it == source) { | 141 if (*it == source) { |
152 ++it; | 142 ++it; |
153 if (it != balloons.end()) { | 143 if (it != balloons.end()) { |
154 apply_offset = true; | 144 apply_offset = true; |
155 offset.set_y((source)->offset().y() - (*it)->offset().y() + | 145 offset.set_y((source)->offset().y() - (*it)->offset().y() + |
156 (*it)->content_size().height() - source->content_size().height()); | 146 (*it)->content_size().height() - source->content_size().height()); |
157 } | 147 } |
158 } else { | 148 } else { |
159 if (apply_offset) | 149 if (apply_offset) |
160 (*it)->add_offset(offset); | 150 (*it)->add_offset(offset); |
161 ++it; | 151 ++it; |
162 } | 152 } |
163 } | 153 } |
164 // Start listening for UI events so we cancel the offset when the mouse | 154 // Start listening for UI events so we cancel the offset when the mouse |
165 // leaves the balloon area. | 155 // leaves the balloon area. |
166 if (apply_offset) | 156 if (apply_offset) |
167 AddMessageLoopObserver(); | 157 AddMessageLoopObserver(); |
168 } | 158 } |
169 #endif | |
170 | 159 |
171 base_.Remove(source); | 160 base_.Remove(source); |
172 PositionBalloons(true); | 161 PositionBalloons(true); |
173 | 162 |
174 // There may be no listener in a unit test. | 163 // There may be no listener in a unit test. |
175 if (space_change_listener_) | 164 if (space_change_listener_) |
176 space_change_listener_->OnBalloonSpaceChanged(); | 165 space_change_listener_->OnBalloonSpaceChanged(); |
177 | 166 |
178 // This is used only for testing. | 167 // This is used only for testing. |
179 if (!on_collection_changed_callback_.is_null()) | 168 if (!on_collection_changed_callback_.is_null()) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 Balloons::const_iterator iter; | 214 Balloons::const_iterator iter; |
226 for (iter = balloons.begin(); iter != balloons.end(); ++iter) { | 215 for (iter = balloons.begin(); iter != balloons.end(); ++iter) { |
227 gfx::Rect balloon_box = gfx::Rect((*iter)->GetPosition(), | 216 gfx::Rect balloon_box = gfx::Rect((*iter)->GetPosition(), |
228 (*iter)->GetViewSize()); | 217 (*iter)->GetViewSize()); |
229 bounds.Union(balloon_box); | 218 bounds.Union(balloon_box); |
230 } | 219 } |
231 | 220 |
232 return bounds; | 221 return bounds; |
233 } | 222 } |
234 | 223 |
235 #if USE_OFFSETS | |
236 void BalloonCollectionImpl::AddMessageLoopObserver() { | 224 void BalloonCollectionImpl::AddMessageLoopObserver() { |
237 if (!added_as_message_loop_observer_) { | 225 if (!added_as_message_loop_observer_) { |
238 base::MessageLoopForUI::current()->AddObserver(this); | 226 base::MessageLoopForUI::current()->AddObserver(this); |
239 added_as_message_loop_observer_ = true; | 227 added_as_message_loop_observer_ = true; |
240 } | 228 } |
241 } | 229 } |
242 | 230 |
243 void BalloonCollectionImpl::RemoveMessageLoopObserver() { | 231 void BalloonCollectionImpl::RemoveMessageLoopObserver() { |
244 if (added_as_message_loop_observer_) { | 232 if (added_as_message_loop_observer_) { |
245 base::MessageLoopForUI::current()->RemoveObserver(this); | 233 base::MessageLoopForUI::current()->RemoveObserver(this); |
(...skipping 25 matching lines...) Expand all Loading... |
271 FROM_HERE, | 259 FROM_HERE, |
272 base::Bind(&BalloonCollectionImpl::CancelOffsets, | 260 base::Bind(&BalloonCollectionImpl::CancelOffsets, |
273 reposition_factory_.GetWeakPtr()), | 261 reposition_factory_.GetWeakPtr()), |
274 base::TimeDelta::FromMilliseconds(kRepositionDelayMs)); | 262 base::TimeDelta::FromMilliseconds(kRepositionDelayMs)); |
275 } | 263 } |
276 } else { | 264 } else { |
277 // Mouse moved back into the region. Cancel the reposition. | 265 // Mouse moved back into the region. Cancel the reposition. |
278 reposition_factory_.InvalidateWeakPtrs(); | 266 reposition_factory_.InvalidateWeakPtrs(); |
279 } | 267 } |
280 } | 268 } |
281 #endif | |
282 | 269 |
283 BalloonCollectionImpl::Layout::Layout() | 270 BalloonCollectionImpl::Layout::Layout() |
284 : placement_(INVALID), | 271 : placement_(INVALID), |
285 need_to_compute_panel_offset_(false), | 272 need_to_compute_panel_offset_(false), |
286 offset_to_move_above_panels_(0) { | 273 offset_to_move_above_panels_(0) { |
287 RefreshSystemMetrics(); | 274 RefreshSystemMetrics(); |
288 } | 275 } |
289 | 276 |
290 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size, | 277 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size, |
291 int* total_size) const { | 278 int* total_size) const { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 break; | 372 break; |
386 } | 373 } |
387 return location; | 374 return location; |
388 } | 375 } |
389 | 376 |
390 bool BalloonCollectionImpl::Layout::RequiresOffsets() const { | 377 bool BalloonCollectionImpl::Layout::RequiresOffsets() const { |
391 // Layout schemes that grow up from the bottom require offsets; | 378 // Layout schemes that grow up from the bottom require offsets; |
392 // schemes that grow down do not require offsets. | 379 // schemes that grow down do not require offsets. |
393 bool offsets = (placement_ == VERTICALLY_FROM_BOTTOM_LEFT || | 380 bool offsets = (placement_ == VERTICALLY_FROM_BOTTOM_LEFT || |
394 placement_ == VERTICALLY_FROM_BOTTOM_RIGHT); | 381 placement_ == VERTICALLY_FROM_BOTTOM_RIGHT); |
395 | |
396 #if defined(OS_MACOSX) | |
397 // These schemes are in screen-coordinates, and top and bottom | |
398 // are inverted on Mac. | |
399 offsets = !offsets; | |
400 #endif | |
401 | |
402 return offsets; | 382 return offsets; |
403 } | 383 } |
404 | 384 |
405 // static | 385 // static |
406 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits( | 386 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits( |
407 const gfx::Size& size) { | 387 const gfx::Size& size) { |
408 // restrict to the min & max sizes | 388 // restrict to the min & max sizes |
409 return gfx::Size( | 389 return gfx::Size( |
410 std::max(min_balloon_width(), | 390 std::max(min_balloon_width(), |
411 std::min(max_balloon_width(), size.width())), | 391 std::min(max_balloon_width(), size.width())), |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 if (offset_to_move_above_panels_ == offset_to_move_above_panels) | 443 if (offset_to_move_above_panels_ == offset_to_move_above_panels) |
464 return false; | 444 return false; |
465 | 445 |
466 offset_to_move_above_panels_ = offset_to_move_above_panels; | 446 offset_to_move_above_panels_ = offset_to_move_above_panels; |
467 return true; | 447 return true; |
468 } | 448 } |
469 | 449 |
470 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() { | 450 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() { |
471 bool changed = false; | 451 bool changed = false; |
472 | 452 |
473 #if defined(OS_MACOSX) | |
474 gfx::Rect new_work_area = GetMacWorkArea(); | |
475 #else | |
476 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312 | 453 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312 |
477 gfx::Rect new_work_area = | 454 gfx::Rect new_work_area = |
478 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area(); | 455 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area(); |
479 #endif | |
480 if (work_area_ != new_work_area) { | 456 if (work_area_ != new_work_area) { |
481 work_area_.SetRect(new_work_area.x(), new_work_area.y(), | 457 work_area_.SetRect(new_work_area.x(), new_work_area.y(), |
482 new_work_area.width(), new_work_area.height()); | 458 new_work_area.width(), new_work_area.height()); |
483 changed = true; | 459 changed = true; |
484 } | 460 } |
485 | 461 |
486 return changed; | 462 return changed; |
487 } | 463 } |
OLD | NEW |