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.
5 #include "chrome/browser/notifications/balloon_collection_impl.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/notifications/balloon.h"
12 #include "chrome/browser/notifications/balloon_host.h"
13 #include "chrome/browser/notifications/notification.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/panels/docked_panel_collection.h"
16 #include "chrome/browser/ui/panels/panel.h"
17 #include "chrome/browser/ui/panels/panel_manager.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_service.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/gfx/size.h"
24 // Portion of the screen allotted for notifications. When notification balloons
25 // extend over this, no new notifications are shown until some are closed.
26 const double kPercentBalloonFillFactor = 0.7;
28 // Allow at least this number of balloons on the screen.
29 const int kMinAllowedBalloonCount = 2;
31 // The spacing between the balloon and the panel.
32 const int kVerticalSpacingBetweenBalloonAndPanel = 5;
35 // Delay from the mouse leaving the balloon collection before
36 // there is a relayout, in milliseconds.
37 const int kRepositionDelayMs = 300;
41 BalloonCollectionImpl::BalloonCollectionImpl()
43 : reposition_factory_(this),
44 added_as_message_loop_observer_(false)
47 registrar_.Add(this, chrome::NOTIFICATION_PANEL_COLLECTION_UPDATED,
48 content::NotificationService::AllSources());
49 registrar_.Add(this, chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE,
50 content::NotificationService::AllSources());
52 SetPositionPreference(BalloonCollection::DEFAULT_POSITION);
55 BalloonCollectionImpl::~BalloonCollectionImpl() {
57 RemoveMessageLoopObserver();
61 void BalloonCollectionImpl::AddImpl(const Notification& notification,
64 Balloon* new_balloon = MakeBalloon(notification, profile);
65 // 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
67 // condition. We are only interested in comparing height to maximum.
68 new_balloon->set_min_scrollbar_size(gfx::Size(1 + layout_.max_balloon_width(),
69 layout_.max_balloon_height()));
70 new_balloon->SetPosition(layout_.OffScreenLocation(), false);
73 int count = base_.count();
74 if (count > 0 && layout_.RequiresOffsets())
75 new_balloon->set_offset(base_.balloons()[count - 1]->offset());
77 base_.Add(new_balloon, add_to_front);
78 PositionBalloons(false);
80 // There may be no listener in a unit test.
81 if (space_change_listener_)
82 space_change_listener_->OnBalloonSpaceChanged();
84 // This is used only for testing.
85 if (!on_collection_changed_callback_.is_null())
86 on_collection_changed_callback_.Run();
89 void BalloonCollectionImpl::Add(const Notification& notification,
91 AddImpl(notification, profile, false);
94 const Notification* BalloonCollectionImpl::FindById(
95 const std::string& id) const {
96 return base_.FindById(id);
99 bool BalloonCollectionImpl::RemoveById(const std::string& id) {
100 return base_.CloseById(id);
103 bool BalloonCollectionImpl::RemoveBySourceOrigin(const GURL& origin) {
104 return base_.CloseAllBySourceOrigin(origin);
107 bool BalloonCollectionImpl::RemoveByProfile(Profile* profile) {
108 return base_.CloseAllByProfile(profile);
111 void BalloonCollectionImpl::RemoveAll() {
115 bool BalloonCollectionImpl::HasSpace() const {
116 int count = base_.count();
117 if (count < kMinAllowedBalloonCount)
120 int max_balloon_size = 0;
122 layout_.GetMaxLinearSize(&max_balloon_size, &total_size);
124 int current_max_size = max_balloon_size * count;
125 int max_allowed_size = static_cast<int>(total_size *
126 kPercentBalloonFillFactor);
127 return current_max_size < max_allowed_size - max_balloon_size;
130 void BalloonCollectionImpl::ResizeBalloon(Balloon* balloon,
131 const gfx::Size& size) {
132 balloon->set_content_size(Layout::ConstrainToSizeLimits(size));
133 PositionBalloons(true);
136 void BalloonCollectionImpl::DisplayChanged() {
137 layout_.RefreshSystemMetrics();
138 PositionBalloons(true);
141 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) {
143 // We want to free the balloon when finished.
144 const Balloons& balloons = base_.balloons();
146 Balloons::const_iterator it = balloons.begin();
147 if (layout_.RequiresOffsets()) {
148 gfx::Vector2d offset;
149 bool apply_offset = false;
150 while (it != balloons.end()) {
153 if (it != balloons.end()) {
155 offset.set_y((source)->offset().y() - (*it)->offset().y() +
156 (*it)->content_size().height() - source->content_size().height());
160 (*it)->add_offset(offset);
164 // Start listening for UI events so we cancel the offset when the mouse
165 // leaves the balloon area.
167 AddMessageLoopObserver();
171 base_.Remove(source);
172 PositionBalloons(true);
174 // There may be no listener in a unit test.
175 if (space_change_listener_)
176 space_change_listener_->OnBalloonSpaceChanged();
178 // This is used only for testing.
179 if (!on_collection_changed_callback_.is_null())
180 on_collection_changed_callback_.Run();
183 const BalloonCollection::Balloons& BalloonCollectionImpl::GetActiveBalloons() {
184 return base_.balloons();
187 void BalloonCollectionImpl::Observe(
189 const content::NotificationSource& source,
190 const content::NotificationDetails& details) {
193 case chrome::NOTIFICATION_PANEL_COLLECTION_UPDATED:
194 case chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE:
195 layout_.enable_computing_panel_offset();
196 if (layout_.ComputeOffsetToMoveAbovePanels())
197 PositionBalloons(true);
205 void BalloonCollectionImpl::PositionBalloonsInternal(bool reposition) {
206 const Balloons& balloons = base_.balloons();
208 layout_.RefreshSystemMetrics();
209 gfx::Point origin = layout_.GetLayoutOrigin();
210 for (Balloons::const_iterator it = balloons.begin();
211 it != balloons.end();
213 gfx::Point upper_left = layout_.NextPosition((*it)->GetViewSize(), &origin);
214 (*it)->SetPosition(upper_left, reposition);
218 gfx::Rect BalloonCollectionImpl::GetBalloonsBoundingBox() const {
219 // Start from the layout origin.
220 gfx::Rect bounds = gfx::Rect(layout_.GetLayoutOrigin(), gfx::Size(0, 0));
222 // For each balloon, extend the rectangle. This approach is indifferent to
223 // the orientation of the balloons.
224 const Balloons& balloons = base_.balloons();
225 Balloons::const_iterator iter;
226 for (iter = balloons.begin(); iter != balloons.end(); ++iter) {
227 gfx::Rect balloon_box = gfx::Rect((*iter)->GetPosition(),
228 (*iter)->GetViewSize());
229 bounds.Union(balloon_box);
236 void BalloonCollectionImpl::AddMessageLoopObserver() {
237 if (!added_as_message_loop_observer_) {
238 base::MessageLoopForUI::current()->AddObserver(this);
239 added_as_message_loop_observer_ = true;
243 void BalloonCollectionImpl::RemoveMessageLoopObserver() {
244 if (added_as_message_loop_observer_) {
245 base::MessageLoopForUI::current()->RemoveObserver(this);
246 added_as_message_loop_observer_ = false;
250 void BalloonCollectionImpl::CancelOffsets() {
251 reposition_factory_.InvalidateWeakPtrs();
253 // Unhook from listening to all UI events.
254 RemoveMessageLoopObserver();
256 const Balloons& balloons = base_.balloons();
257 for (Balloons::const_iterator it = balloons.begin();
258 it != balloons.end();
260 (*it)->set_offset(gfx::Vector2d());
262 PositionBalloons(true);
265 void BalloonCollectionImpl::HandleMouseMoveEvent() {
266 if (!IsCursorInBalloonCollection()) {
267 // Mouse has left the region. Schedule a reposition after
269 if (!reposition_factory_.HasWeakPtrs()) {
270 base::MessageLoop::current()->PostDelayedTask(
272 base::Bind(&BalloonCollectionImpl::CancelOffsets,
273 reposition_factory_.GetWeakPtr()),
274 base::TimeDelta::FromMilliseconds(kRepositionDelayMs));
277 // Mouse moved back into the region. Cancel the reposition.
278 reposition_factory_.InvalidateWeakPtrs();
283 BalloonCollectionImpl::Layout::Layout()
284 : placement_(INVALID),
285 need_to_compute_panel_offset_(false),
286 offset_to_move_above_panels_(0) {
287 RefreshSystemMetrics();
290 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size,
291 int* total_size) const {
292 DCHECK(max_balloon_size && total_size);
294 // All placement schemes are vertical, so we only care about height.
295 *total_size = work_area_.height();
296 *max_balloon_size = max_balloon_height();
299 gfx::Point BalloonCollectionImpl::Layout::GetLayoutOrigin() const {
300 // For lower-left and lower-right positioning, we need to add an offset
301 // to ensure balloons to stay on top of panels to avoid overlapping.
304 switch (placement_) {
305 case VERTICALLY_FROM_TOP_LEFT: {
306 x = work_area_.x() + HorizontalEdgeMargin();
307 y = work_area_.y() + VerticalEdgeMargin() + offset_to_move_above_panels_;
310 case VERTICALLY_FROM_TOP_RIGHT: {
311 x = work_area_.right() - HorizontalEdgeMargin();
312 y = work_area_.y() + VerticalEdgeMargin() + offset_to_move_above_panels_;
315 case VERTICALLY_FROM_BOTTOM_LEFT:
316 x = work_area_.x() + HorizontalEdgeMargin();
317 y = work_area_.bottom() - VerticalEdgeMargin() -
318 offset_to_move_above_panels_;
320 case VERTICALLY_FROM_BOTTOM_RIGHT:
321 x = work_area_.right() - HorizontalEdgeMargin();
322 y = work_area_.bottom() - VerticalEdgeMargin() -
323 offset_to_move_above_panels_;
329 return gfx::Point(x, y);
332 gfx::Point BalloonCollectionImpl::Layout::NextPosition(
333 const gfx::Size& balloon_size,
334 gfx::Point* position_iterator) const {
335 DCHECK(position_iterator);
339 switch (placement_) {
340 case VERTICALLY_FROM_TOP_LEFT:
341 x = position_iterator->x();
342 y = position_iterator->y();
343 position_iterator->set_y(position_iterator->y() + balloon_size.height() +
344 InterBalloonMargin());
346 case VERTICALLY_FROM_TOP_RIGHT:
347 x = position_iterator->x() - balloon_size.width();
348 y = position_iterator->y();
349 position_iterator->set_y(position_iterator->y() + balloon_size.height() +
350 InterBalloonMargin());
352 case VERTICALLY_FROM_BOTTOM_LEFT:
353 position_iterator->set_y(position_iterator->y() - balloon_size.height() -
354 InterBalloonMargin());
355 x = position_iterator->x();
356 y = position_iterator->y();
358 case VERTICALLY_FROM_BOTTOM_RIGHT:
359 position_iterator->set_y(position_iterator->y() - balloon_size.height() -
360 InterBalloonMargin());
361 x = position_iterator->x() - balloon_size.width();
362 y = position_iterator->y();
368 return gfx::Point(x, y);
371 gfx::Point BalloonCollectionImpl::Layout::OffScreenLocation() const {
372 gfx::Point location = GetLayoutOrigin();
373 switch (placement_) {
374 case VERTICALLY_FROM_TOP_LEFT:
375 case VERTICALLY_FROM_BOTTOM_LEFT:
376 location.Offset(0, kBalloonMaxHeight);
378 case VERTICALLY_FROM_TOP_RIGHT:
379 case VERTICALLY_FROM_BOTTOM_RIGHT:
380 location.Offset(-kBalloonMaxWidth - BalloonView::GetHorizontalMargin(),
390 bool BalloonCollectionImpl::Layout::RequiresOffsets() const {
391 // Layout schemes that grow up from the bottom require offsets;
392 // schemes that grow down do not require offsets.
393 bool offsets = (placement_ == VERTICALLY_FROM_BOTTOM_LEFT ||
394 placement_ == VERTICALLY_FROM_BOTTOM_RIGHT);
396 #if defined(OS_MACOSX)
397 // These schemes are in screen-coordinates, and top and bottom
398 // are inverted on Mac.
406 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits(
407 const gfx::Size& size) {
408 // restrict to the min & max sizes
410 std::max(min_balloon_width(),
411 std::min(max_balloon_width(), size.width())),
412 std::max(min_balloon_height(),
413 std::min(max_balloon_height(), size.height())));
416 bool BalloonCollectionImpl::Layout::ComputeOffsetToMoveAbovePanels() {
417 // If the offset is not enabled due to that we have not received a
418 // notification about panel, don't proceed because we don't want to call
419 // PanelManager::GetInstance() to create an instance when panel is not
421 if (!need_to_compute_panel_offset_)
424 const DockedPanelCollection::Panels& panels =
425 PanelManager::GetInstance()->docked_collection()->panels();
426 int offset_to_move_above_panels = 0;
428 // The offset is the maximum height of panels that could overlap with the
430 if (NeedToMoveAboveLeftSidePanels()) {
431 for (DockedPanelCollection::Panels::const_reverse_iterator iter =
433 iter != panels.rend(); ++iter) {
434 // No need to check panels beyond the area occupied by the balloons.
435 if ((*iter)->GetBounds().x() >= work_area_.x() + max_balloon_width())
438 int current_height = (*iter)->GetBounds().height();
439 if (current_height > offset_to_move_above_panels)
440 offset_to_move_above_panels = current_height;
442 } else if (NeedToMoveAboveRightSidePanels()) {
443 for (DockedPanelCollection::Panels::const_iterator iter = panels.begin();
444 iter != panels.end(); ++iter) {
445 // No need to check panels beyond the area occupied by the balloons.
446 if ((*iter)->GetBounds().right() <=
447 work_area_.right() - max_balloon_width())
450 int current_height = (*iter)->GetBounds().height();
451 if (current_height > offset_to_move_above_panels)
452 offset_to_move_above_panels = current_height;
456 // Ensure that we have some sort of margin between the 1st balloon and the
457 // panel beneath it even the vertical edge margin is 0 as on Mac.
458 if (offset_to_move_above_panels && !VerticalEdgeMargin())
459 offset_to_move_above_panels += kVerticalSpacingBetweenBalloonAndPanel;
461 // If no change is detected, return false to indicate that we do not need to
462 // reposition balloons.
463 if (offset_to_move_above_panels_ == offset_to_move_above_panels)
466 offset_to_move_above_panels_ = offset_to_move_above_panels;
470 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() {
471 bool changed = false;
473 #if defined(OS_MACOSX)
474 gfx::Rect new_work_area = GetMacWorkArea();
476 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312
477 gfx::Rect new_work_area =
478 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area();
480 if (work_area_ != new_work_area) {
481 work_area_.SetRect(new_work_area.x(), new_work_area.y(),
482 new_work_area.width(), new_work_area.height());