| OLD | NEW |
| 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 && layout_.RequiresOffsets()) | |
| 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 if (layout_.RequiresOffsets()) { | |
| 116 gfx::Point offset; | |
| 117 bool apply_offset = false; | |
| 118 while (it != balloons.end()) { | |
| 119 if (*it == source) { | |
| 120 ++it; | |
| 121 if (it != balloons.end()) { | |
| 122 apply_offset = true; | |
| 123 offset.set_y((source)->offset().y() - (*it)->offset().y() + | |
| 124 (*it)->content_size().height() - source->content_size().height()); | |
| 125 } | |
| 126 } else { | |
| 127 if (apply_offset) | |
| 128 (*it)->add_offset(offset); | |
| 129 ++it; | |
| 130 } | |
| 131 } | |
| 132 // Start listening for UI events so we cancel the offset when the mouse | |
| 133 // leaves the balloon area. | |
| 134 if (apply_offset) | |
| 135 AddMessageLoopObserver(); | |
| 136 } | |
| 137 #endif | |
| 138 | |
| 139 base_.Remove(source); | |
| 140 PositionBalloons(true); | |
| 141 | |
| 142 // There may be no listener in a unit test. | |
| 143 if (space_change_listener_) | |
| 144 space_change_listener_->OnBalloonSpaceChanged(); | |
| 145 | |
| 146 // This is used only for testing. | |
| 147 if (on_collection_changed_callback_.get()) | |
| 148 on_collection_changed_callback_->Run(); | |
| 149 } | |
| 150 | |
| 151 void BalloonCollectionImpl::PositionBalloonsInternal(bool reposition) { | |
| 152 const Balloons& balloons = base_.balloons(); | |
| 153 | |
| 154 layout_.RefreshSystemMetrics(); | |
| 155 gfx::Point origin = layout_.GetLayoutOrigin(); | |
| 156 for (Balloons::const_iterator it = balloons.begin(); | |
| 157 it != balloons.end(); | |
| 158 ++it) { | |
| 159 gfx::Point upper_left = layout_.NextPosition((*it)->GetViewSize(), &origin); | |
| 160 (*it)->SetPosition(upper_left, reposition); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 gfx::Rect BalloonCollectionImpl::GetBalloonsBoundingBox() const { | |
| 165 // Start from the layout origin. | |
| 166 gfx::Rect bounds = gfx::Rect(layout_.GetLayoutOrigin(), gfx::Size(0, 0)); | |
| 167 | |
| 168 // For each balloon, extend the rectangle. This approach is indifferent to | |
| 169 // the orientation of the balloons. | |
| 170 const Balloons& balloons = base_.balloons(); | |
| 171 Balloons::const_iterator iter; | |
| 172 for (iter = balloons.begin(); iter != balloons.end(); ++iter) { | |
| 173 gfx::Rect balloon_box = gfx::Rect((*iter)->GetPosition(), | |
| 174 (*iter)->GetViewSize()); | |
| 175 bounds = bounds.Union(balloon_box); | |
| 176 } | |
| 177 | |
| 178 return bounds; | |
| 179 } | |
| 180 | |
| 181 #if USE_OFFSETS | |
| 182 void BalloonCollectionImpl::AddMessageLoopObserver() { | |
| 183 if (!added_as_message_loop_observer_) { | |
| 184 MessageLoopForUI::current()->AddObserver(this); | |
| 185 added_as_message_loop_observer_ = true; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 void BalloonCollectionImpl::RemoveMessageLoopObserver() { | |
| 190 if (added_as_message_loop_observer_) { | |
| 191 MessageLoopForUI::current()->RemoveObserver(this); | |
| 192 added_as_message_loop_observer_ = false; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void BalloonCollectionImpl::CancelOffsets() { | |
| 197 reposition_factory_.RevokeAll(); | |
| 198 | |
| 199 // Unhook from listening to all UI events. | |
| 200 RemoveMessageLoopObserver(); | |
| 201 | |
| 202 const Balloons& balloons = base_.balloons(); | |
| 203 for (Balloons::const_iterator it = balloons.begin(); | |
| 204 it != balloons.end(); | |
| 205 ++it) | |
| 206 (*it)->set_offset(gfx::Point(0, 0)); | |
| 207 | |
| 208 PositionBalloons(true); | |
| 209 } | |
| 210 | |
| 211 void BalloonCollectionImpl::HandleMouseMoveEvent() { | |
| 212 if (!IsCursorInBalloonCollection()) { | |
| 213 // Mouse has left the region. Schedule a reposition after | |
| 214 // a short delay. | |
| 215 if (reposition_factory_.empty()) { | |
| 216 MessageLoop::current()->PostDelayedTask( | |
| 217 FROM_HERE, | |
| 218 reposition_factory_.NewRunnableMethod( | |
| 219 &BalloonCollectionImpl::CancelOffsets), | |
| 220 kRepositionDelay); | |
| 221 } | |
| 222 } else { | |
| 223 // Mouse moved back into the region. Cancel the reposition. | |
| 224 reposition_factory_.RevokeAll(); | |
| 225 } | |
| 226 } | |
| 227 #endif | |
| 228 | |
| 229 BalloonCollectionImpl::Layout::Layout() { | |
| 230 RefreshSystemMetrics(); | |
| 231 } | |
| 232 | |
| 233 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size, | |
| 234 int* total_size) const { | |
| 235 DCHECK(max_balloon_size && total_size); | |
| 236 | |
| 237 // All placement schemes are vertical, so we only care about height. | |
| 238 *total_size = work_area_.height(); | |
| 239 *max_balloon_size = max_balloon_height(); | |
| 240 } | |
| 241 | |
| 242 gfx::Point BalloonCollectionImpl::Layout::GetLayoutOrigin() const { | |
| 243 int x = 0; | |
| 244 int y = 0; | |
| 245 switch (placement_) { | |
| 246 case VERTICALLY_FROM_TOP_LEFT: | |
| 247 x = work_area_.x() + HorizontalEdgeMargin(); | |
| 248 y = work_area_.y() + VerticalEdgeMargin(); | |
| 249 break; | |
| 250 case VERTICALLY_FROM_TOP_RIGHT: | |
| 251 x = work_area_.right() - HorizontalEdgeMargin(); | |
| 252 y = work_area_.y() + VerticalEdgeMargin(); | |
| 253 break; | |
| 254 case VERTICALLY_FROM_BOTTOM_LEFT: | |
| 255 x = work_area_.x() + HorizontalEdgeMargin(); | |
| 256 y = work_area_.bottom() - VerticalEdgeMargin(); | |
| 257 break; | |
| 258 case VERTICALLY_FROM_BOTTOM_RIGHT: | |
| 259 x = work_area_.right() - HorizontalEdgeMargin(); | |
| 260 y = work_area_.bottom() - VerticalEdgeMargin(); | |
| 261 break; | |
| 262 default: | |
| 263 NOTREACHED(); | |
| 264 break; | |
| 265 } | |
| 266 return gfx::Point(x, y); | |
| 267 } | |
| 268 | |
| 269 gfx::Point BalloonCollectionImpl::Layout::NextPosition( | |
| 270 const gfx::Size& balloon_size, | |
| 271 gfx::Point* position_iterator) const { | |
| 272 DCHECK(position_iterator); | |
| 273 | |
| 274 int x = 0; | |
| 275 int y = 0; | |
| 276 switch (placement_) { | |
| 277 case VERTICALLY_FROM_TOP_LEFT: | |
| 278 x = position_iterator->x(); | |
| 279 y = position_iterator->y(); | |
| 280 position_iterator->set_y(position_iterator->y() + balloon_size.height() + | |
| 281 InterBalloonMargin()); | |
| 282 break; | |
| 283 case VERTICALLY_FROM_TOP_RIGHT: | |
| 284 x = position_iterator->x() - balloon_size.width(); | |
| 285 y = position_iterator->y(); | |
| 286 position_iterator->set_y(position_iterator->y() + balloon_size.height() + | |
| 287 InterBalloonMargin()); | |
| 288 break; | |
| 289 case VERTICALLY_FROM_BOTTOM_LEFT: | |
| 290 position_iterator->set_y(position_iterator->y() - balloon_size.height() - | |
| 291 InterBalloonMargin()); | |
| 292 x = position_iterator->x(); | |
| 293 y = position_iterator->y(); | |
| 294 break; | |
| 295 case VERTICALLY_FROM_BOTTOM_RIGHT: | |
| 296 position_iterator->set_y(position_iterator->y() - balloon_size.height() - | |
| 297 InterBalloonMargin()); | |
| 298 x = position_iterator->x() - balloon_size.width(); | |
| 299 y = position_iterator->y(); | |
| 300 break; | |
| 301 default: | |
| 302 NOTREACHED(); | |
| 303 break; | |
| 304 } | |
| 305 return gfx::Point(x, y); | |
| 306 } | |
| 307 | |
| 308 gfx::Point BalloonCollectionImpl::Layout::OffScreenLocation() const { | |
| 309 int x = 0; | |
| 310 int y = 0; | |
| 311 switch (placement_) { | |
| 312 case VERTICALLY_FROM_TOP_LEFT: | |
| 313 x = work_area_.x() + HorizontalEdgeMargin(); | |
| 314 y = work_area_.y() + kBalloonMaxHeight + VerticalEdgeMargin(); | |
| 315 break; | |
| 316 case VERTICALLY_FROM_TOP_RIGHT: | |
| 317 x = work_area_.right() - kBalloonMaxWidth - HorizontalEdgeMargin(); | |
| 318 y = work_area_.y() + kBalloonMaxHeight + VerticalEdgeMargin(); | |
| 319 break; | |
| 320 case VERTICALLY_FROM_BOTTOM_LEFT: | |
| 321 x = work_area_.x() + HorizontalEdgeMargin(); | |
| 322 y = work_area_.bottom() + kBalloonMaxHeight + VerticalEdgeMargin(); | |
| 323 break; | |
| 324 case VERTICALLY_FROM_BOTTOM_RIGHT: | |
| 325 x = work_area_.right() - kBalloonMaxWidth - HorizontalEdgeMargin(); | |
| 326 y = work_area_.bottom() + kBalloonMaxHeight + VerticalEdgeMargin(); | |
| 327 break; | |
| 328 default: | |
| 329 NOTREACHED(); | |
| 330 break; | |
| 331 } | |
| 332 return gfx::Point(x, y); | |
| 333 } | |
| 334 | |
| 335 bool BalloonCollectionImpl::Layout::RequiresOffsets() const { | |
| 336 // Layout schemes that grow up from the bottom require offsets; | |
| 337 // schemes that grow down do not require offsets. | |
| 338 bool offsets = (placement_ == VERTICALLY_FROM_BOTTOM_LEFT || | |
| 339 placement_ == VERTICALLY_FROM_BOTTOM_RIGHT); | |
| 340 | |
| 341 #if defined(OS_MACOSX) | |
| 342 // These schemes are in screen-coordinates, and top and bottom | |
| 343 // are inverted on Mac. | |
| 344 offsets = !offsets; | |
| 345 #endif | |
| 346 | |
| 347 return offsets; | |
| 348 } | |
| 349 | |
| 350 // static | |
| 351 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits( | |
| 352 const gfx::Size& size) { | |
| 353 // restrict to the min & max sizes | |
| 354 return gfx::Size( | |
| 355 std::max(min_balloon_width(), | |
| 356 std::min(max_balloon_width(), size.width())), | |
| 357 std::max(min_balloon_height(), | |
| 358 std::min(max_balloon_height(), size.height()))); | |
| 359 } | |
| 360 | |
| 361 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() { | |
| 362 bool changed = false; | |
| 363 | |
| 364 #if defined(OS_MACOSX) | |
| 365 gfx::Rect new_work_area = GetMacWorkArea(); | |
| 366 #else | |
| 367 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider( | |
| 368 WindowSizer::CreateDefaultMonitorInfoProvider()); | |
| 369 gfx::Rect new_work_area = info_provider->GetPrimaryMonitorWorkArea(); | |
| 370 #endif | |
| 371 if (!work_area_.Equals(new_work_area)) { | |
| 372 work_area_.SetRect(new_work_area.x(), new_work_area.y(), | |
| 373 new_work_area.width(), new_work_area.height()); | |
| 374 changed = true; | |
| 375 } | |
| 376 | |
| 377 return changed; | |
| 378 } | |
| OLD | NEW |