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 #include "chrome/browser/notifications/balloon_collection_impl.h" 6 7 #include "base/bind.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" 23 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; 27 28 // Allow at least this number of balloons on the screen. 29 const int kMinAllowedBalloonCount = 2; 30 31 // The spacing between the balloon and the panel. 32 const int kVerticalSpacingBetweenBalloonAndPanel = 5; 33 34 #if USE_OFFSETS 35 // Delay from the mouse leaving the balloon collection before 36 // there is a relayout, in milliseconds. 37 const int kRepositionDelayMs = 300; 38 #endif // USE_OFFSETS 39 40 41 BalloonCollectionImpl::BalloonCollectionImpl() 42 #if USE_OFFSETS 43 : reposition_factory_(this), 44 added_as_message_loop_observer_(false) 45 #endif 46 { 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()); 51 52 SetPositionPreference(BalloonCollection::DEFAULT_POSITION); 53 } 54 55 BalloonCollectionImpl::~BalloonCollectionImpl() { 56 #if USE_OFFSETS 57 RemoveMessageLoopObserver(); 58 #endif 59 } 60 61 void BalloonCollectionImpl::AddImpl(const Notification& notification, 62 Profile* profile, 63 bool add_to_front) { 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); 71 new_balloon->Show(); 72 #if USE_OFFSETS 73 int count = base_.count(); 74 if (count > 0 && layout_.RequiresOffsets()) 75 new_balloon->set_offset(base_.balloons()[count - 1]->offset()); 76 #endif 77 base_.Add(new_balloon, add_to_front); 78 PositionBalloons(false); 79 80 // There may be no listener in a unit test. 81 if (space_change_listener_) 82 space_change_listener_->OnBalloonSpaceChanged(); 83 84 // This is used only for testing. 85 if (!on_collection_changed_callback_.is_null()) 86 on_collection_changed_callback_.Run(); 87 } 88 89 void BalloonCollectionImpl::Add(const Notification& notification, 90 Profile* profile) { 91 AddImpl(notification, profile, false); 92 } 93 94 const Notification* BalloonCollectionImpl::FindById( 95 const std::string& id) const { 96 return base_.FindById(id); 97 } 98 99 bool BalloonCollectionImpl::RemoveById(const std::string& id) { 100 return base_.CloseById(id); 101 } 102 103 bool BalloonCollectionImpl::RemoveBySourceOrigin(const GURL& origin) { 104 return base_.CloseAllBySourceOrigin(origin); 105 } 106 107 bool BalloonCollectionImpl::RemoveByProfile(Profile* profile) { 108 return base_.CloseAllByProfile(profile); 109 } 110 111 void BalloonCollectionImpl::RemoveAll() { 112 base_.CloseAll(); 113 } 114 115 bool BalloonCollectionImpl::HasSpace() const { 116 int count = base_.count(); 117 if (count < kMinAllowedBalloonCount) 118 return true; 119 120 int max_balloon_size = 0; 121 int total_size = 0; 122 layout_.GetMaxLinearSize(&max_balloon_size, &total_size); 123 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; 128 } 129 130 void BalloonCollectionImpl::ResizeBalloon(Balloon* balloon, 131 const gfx::Size& size) { 132 balloon->set_content_size(Layout::ConstrainToSizeLimits(size)); 133 PositionBalloons(true); 134 } 135 136 void BalloonCollectionImpl::DisplayChanged() { 137 layout_.RefreshSystemMetrics(); 138 PositionBalloons(true); 139 } 140 141 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { 142 #if USE_OFFSETS 143 // We want to free the balloon when finished. 144 const Balloons& balloons = base_.balloons(); 145 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()) { 151 if (*it == source) { 152 ++it; 153 if (it != balloons.end()) { 154 apply_offset = true; 155 offset.set_y((source)->offset().y() - (*it)->offset().y() + 156 (*it)->content_size().height() - source->content_size().height()); 157 } 158 } else { 159 if (apply_offset) 160 (*it)->add_offset(offset); 161 ++it; 162 } 163 } 164 // Start listening for UI events so we cancel the offset when the mouse 165 // leaves the balloon area. 166 if (apply_offset) 167 AddMessageLoopObserver(); 168 } 169 #endif 170 171 base_.Remove(source); 172 PositionBalloons(true); 173 174 // There may be no listener in a unit test. 175 if (space_change_listener_) 176 space_change_listener_->OnBalloonSpaceChanged(); 177 178 // This is used only for testing. 179 if (!on_collection_changed_callback_.is_null()) 180 on_collection_changed_callback_.Run(); 181 } 182 183 const BalloonCollection::Balloons& BalloonCollectionImpl::GetActiveBalloons() { 184 return base_.balloons(); 185 } 186 187 void BalloonCollectionImpl::Observe( 188 int type, 189 const content::NotificationSource& source, 190 const content::NotificationDetails& details) { 191 gfx::Rect bounds; 192 switch (type) { 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); 198 break; 199 default: 200 NOTREACHED(); 201 break; 202 } 203 } 204 205 void BalloonCollectionImpl::PositionBalloonsInternal(bool reposition) { 206 const Balloons& balloons = base_.balloons(); 207 208 layout_.RefreshSystemMetrics(); 209 gfx::Point origin = layout_.GetLayoutOrigin(); 210 for (Balloons::const_iterator it = balloons.begin(); 211 it != balloons.end(); 212 ++it) { 213 gfx::Point upper_left = layout_.NextPosition((*it)->GetViewSize(), &origin); 214 (*it)->SetPosition(upper_left, reposition); 215 } 216 } 217 218 gfx::Rect BalloonCollectionImpl::GetBalloonsBoundingBox() const { 219 // Start from the layout origin. 220 gfx::Rect bounds = gfx::Rect(layout_.GetLayoutOrigin(), gfx::Size(0, 0)); 221 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); 230 } 231 232 return bounds; 233 } 234 235 #if USE_OFFSETS 236 void BalloonCollectionImpl::AddMessageLoopObserver() { 237 if (!added_as_message_loop_observer_) { 238 base::MessageLoopForUI::current()->AddObserver(this); 239 added_as_message_loop_observer_ = true; 240 } 241 } 242 243 void BalloonCollectionImpl::RemoveMessageLoopObserver() { 244 if (added_as_message_loop_observer_) { 245 base::MessageLoopForUI::current()->RemoveObserver(this); 246 added_as_message_loop_observer_ = false; 247 } 248 } 249 250 void BalloonCollectionImpl::CancelOffsets() { 251 reposition_factory_.InvalidateWeakPtrs(); 252 253 // Unhook from listening to all UI events. 254 RemoveMessageLoopObserver(); 255 256 const Balloons& balloons = base_.balloons(); 257 for (Balloons::const_iterator it = balloons.begin(); 258 it != balloons.end(); 259 ++it) 260 (*it)->set_offset(gfx::Vector2d()); 261 262 PositionBalloons(true); 263 } 264 265 void BalloonCollectionImpl::HandleMouseMoveEvent() { 266 if (!IsCursorInBalloonCollection()) { 267 // Mouse has left the region. Schedule a reposition after 268 // a short delay. 269 if (!reposition_factory_.HasWeakPtrs()) { 270 base::MessageLoop::current()->PostDelayedTask( 271 FROM_HERE, 272 base::Bind(&BalloonCollectionImpl::CancelOffsets, 273 reposition_factory_.GetWeakPtr()), 274 base::TimeDelta::FromMilliseconds(kRepositionDelayMs)); 275 } 276 } else { 277 // Mouse moved back into the region. Cancel the reposition. 278 reposition_factory_.InvalidateWeakPtrs(); 279 } 280 } 281 #endif 282 283 BalloonCollectionImpl::Layout::Layout() 284 : placement_(INVALID), 285 need_to_compute_panel_offset_(false), 286 offset_to_move_above_panels_(0) { 287 RefreshSystemMetrics(); 288 } 289 290 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size, 291 int* total_size) const { 292 DCHECK(max_balloon_size && total_size); 293 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(); 297 } 298 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. 302 int x = 0; 303 int y = 0; 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_; 308 break; 309 } 310 case VERTICALLY_FROM_TOP_RIGHT: { 311 x = work_area_.right() - HorizontalEdgeMargin(); 312 y = work_area_.y() + VerticalEdgeMargin() + offset_to_move_above_panels_; 313 break; 314 } 315 case VERTICALLY_FROM_BOTTOM_LEFT: 316 x = work_area_.x() + HorizontalEdgeMargin(); 317 y = work_area_.bottom() - VerticalEdgeMargin() - 318 offset_to_move_above_panels_; 319 break; 320 case VERTICALLY_FROM_BOTTOM_RIGHT: 321 x = work_area_.right() - HorizontalEdgeMargin(); 322 y = work_area_.bottom() - VerticalEdgeMargin() - 323 offset_to_move_above_panels_; 324 break; 325 default: 326 NOTREACHED(); 327 break; 328 } 329 return gfx::Point(x, y); 330 } 331 332 gfx::Point BalloonCollectionImpl::Layout::NextPosition( 333 const gfx::Size& balloon_size, 334 gfx::Point* position_iterator) const { 335 DCHECK(position_iterator); 336 337 int x = 0; 338 int y = 0; 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()); 345 break; 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()); 351 break; 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(); 357 break; 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(); 363 break; 364 default: 365 NOTREACHED(); 366 break; 367 } 368 return gfx::Point(x, y); 369 } 370 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); 377 break; 378 case VERTICALLY_FROM_TOP_RIGHT: 379 case VERTICALLY_FROM_BOTTOM_RIGHT: 380 location.Offset(-kBalloonMaxWidth - BalloonView::GetHorizontalMargin(), 381 kBalloonMaxHeight); 382 break; 383 default: 384 NOTREACHED(); 385 break; 386 } 387 return location; 388 } 389 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); 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; 403 } 404 405 // static 406 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits( 407 const gfx::Size& size) { 408 // restrict to the min & max sizes 409 return gfx::Size( 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()))); 414 } 415 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 420 // present. 421 if (!need_to_compute_panel_offset_) 422 return false; 423 424 const DockedPanelCollection::Panels& panels = 425 PanelManager::GetInstance()->docked_collection()->panels(); 426 int offset_to_move_above_panels = 0; 427 428 // The offset is the maximum height of panels that could overlap with the 429 // balloons. 430 if (NeedToMoveAboveLeftSidePanels()) { 431 for (DockedPanelCollection::Panels::const_reverse_iterator iter = 432 panels.rbegin(); 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()) 436 break; 437 438 int current_height = (*iter)->GetBounds().height(); 439 if (current_height > offset_to_move_above_panels) 440 offset_to_move_above_panels = current_height; 441 } 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()) 448 break; 449 450 int current_height = (*iter)->GetBounds().height(); 451 if (current_height > offset_to_move_above_panels) 452 offset_to_move_above_panels = current_height; 453 } 454 } 455 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; 460 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) 464 return false; 465 466 offset_to_move_above_panels_ = offset_to_move_above_panels; 467 return true; 468 } 469 470 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() { 471 bool changed = false; 472 473 #if defined(OS_MACOSX) 474 gfx::Rect new_work_area = GetMacWorkArea(); 475 #else 476 // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312 477 gfx::Rect new_work_area = 478 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area(); 479 #endif 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()); 483 changed = true; 484 } 485 486 return changed; 487 } 488