| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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/logging.h" |
| 8 #include "base/stl_util-inl.h" |
| 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 } |
| 40 |
| 41 BalloonCollectionImpl::~BalloonCollectionImpl() { |
| 42 } |
| 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 const BalloonCollection::Balloons& BalloonCollectionImpl::GetActiveBalloons() { |
| 150 return base_.balloons(); |
| 151 } |
| 152 |
| 153 void BalloonCollectionImpl::PositionBalloonsInternal(bool reposition) { |
| 154 const Balloons& balloons = base_.balloons(); |
| 155 |
| 156 layout_.RefreshSystemMetrics(); |
| 157 gfx::Point origin = layout_.GetLayoutOrigin(); |
| 158 for (Balloons::const_iterator it = balloons.begin(); |
| 159 it != balloons.end(); |
| 160 ++it) { |
| 161 gfx::Point upper_left = layout_.NextPosition((*it)->GetViewSize(), &origin); |
| 162 (*it)->SetPosition(upper_left, reposition); |
| 163 } |
| 164 } |
| 165 |
| 166 gfx::Rect BalloonCollectionImpl::GetBalloonsBoundingBox() const { |
| 167 // Start from the layout origin. |
| 168 gfx::Rect bounds = gfx::Rect(layout_.GetLayoutOrigin(), gfx::Size(0, 0)); |
| 169 |
| 170 // For each balloon, extend the rectangle. This approach is indifferent to |
| 171 // the orientation of the balloons. |
| 172 const Balloons& balloons = base_.balloons(); |
| 173 Balloons::const_iterator iter; |
| 174 for (iter = balloons.begin(); iter != balloons.end(); ++iter) { |
| 175 gfx::Rect balloon_box = gfx::Rect((*iter)->GetPosition(), |
| 176 (*iter)->GetViewSize()); |
| 177 bounds = bounds.Union(balloon_box); |
| 178 } |
| 179 |
| 180 return bounds; |
| 181 } |
| 182 |
| 183 #if USE_OFFSETS |
| 184 void BalloonCollectionImpl::AddMessageLoopObserver() { |
| 185 if (!added_as_message_loop_observer_) { |
| 186 MessageLoopForUI::current()->AddObserver(this); |
| 187 added_as_message_loop_observer_ = true; |
| 188 } |
| 189 } |
| 190 |
| 191 void BalloonCollectionImpl::RemoveMessageLoopObserver() { |
| 192 if (added_as_message_loop_observer_) { |
| 193 MessageLoopForUI::current()->RemoveObserver(this); |
| 194 added_as_message_loop_observer_ = false; |
| 195 } |
| 196 } |
| 197 |
| 198 void BalloonCollectionImpl::CancelOffsets() { |
| 199 reposition_factory_.RevokeAll(); |
| 200 |
| 201 // Unhook from listening to all UI events. |
| 202 RemoveMessageLoopObserver(); |
| 203 |
| 204 const Balloons& balloons = base_.balloons(); |
| 205 for (Balloons::const_iterator it = balloons.begin(); |
| 206 it != balloons.end(); |
| 207 ++it) |
| 208 (*it)->set_offset(gfx::Point(0, 0)); |
| 209 |
| 210 PositionBalloons(true); |
| 211 } |
| 212 |
| 213 void BalloonCollectionImpl::HandleMouseMoveEvent() { |
| 214 if (!IsCursorInBalloonCollection()) { |
| 215 // Mouse has left the region. Schedule a reposition after |
| 216 // a short delay. |
| 217 if (reposition_factory_.empty()) { |
| 218 MessageLoop::current()->PostDelayedTask( |
| 219 FROM_HERE, |
| 220 reposition_factory_.NewRunnableMethod( |
| 221 &BalloonCollectionImpl::CancelOffsets), |
| 222 kRepositionDelay); |
| 223 } |
| 224 } else { |
| 225 // Mouse moved back into the region. Cancel the reposition. |
| 226 reposition_factory_.RevokeAll(); |
| 227 } |
| 228 } |
| 229 #endif |
| 230 |
| 231 BalloonCollectionImpl::Layout::Layout() { |
| 232 RefreshSystemMetrics(); |
| 233 } |
| 234 |
| 235 void BalloonCollectionImpl::Layout::GetMaxLinearSize(int* max_balloon_size, |
| 236 int* total_size) const { |
| 237 DCHECK(max_balloon_size && total_size); |
| 238 |
| 239 // All placement schemes are vertical, so we only care about height. |
| 240 *total_size = work_area_.height(); |
| 241 *max_balloon_size = max_balloon_height(); |
| 242 } |
| 243 |
| 244 gfx::Point BalloonCollectionImpl::Layout::GetLayoutOrigin() const { |
| 245 int x = 0; |
| 246 int y = 0; |
| 247 switch (placement_) { |
| 248 case VERTICALLY_FROM_TOP_LEFT: |
| 249 x = work_area_.x() + HorizontalEdgeMargin(); |
| 250 y = work_area_.y() + VerticalEdgeMargin(); |
| 251 break; |
| 252 case VERTICALLY_FROM_TOP_RIGHT: |
| 253 x = work_area_.right() - HorizontalEdgeMargin(); |
| 254 y = work_area_.y() + VerticalEdgeMargin(); |
| 255 break; |
| 256 case VERTICALLY_FROM_BOTTOM_LEFT: |
| 257 x = work_area_.x() + HorizontalEdgeMargin(); |
| 258 y = work_area_.bottom() - VerticalEdgeMargin(); |
| 259 break; |
| 260 case VERTICALLY_FROM_BOTTOM_RIGHT: |
| 261 x = work_area_.right() - HorizontalEdgeMargin(); |
| 262 y = work_area_.bottom() - VerticalEdgeMargin(); |
| 263 break; |
| 264 default: |
| 265 NOTREACHED(); |
| 266 break; |
| 267 } |
| 268 return gfx::Point(x, y); |
| 269 } |
| 270 |
| 271 gfx::Point BalloonCollectionImpl::Layout::NextPosition( |
| 272 const gfx::Size& balloon_size, |
| 273 gfx::Point* position_iterator) const { |
| 274 DCHECK(position_iterator); |
| 275 |
| 276 int x = 0; |
| 277 int y = 0; |
| 278 switch (placement_) { |
| 279 case VERTICALLY_FROM_TOP_LEFT: |
| 280 x = position_iterator->x(); |
| 281 y = position_iterator->y(); |
| 282 position_iterator->set_y(position_iterator->y() + balloon_size.height() + |
| 283 InterBalloonMargin()); |
| 284 break; |
| 285 case VERTICALLY_FROM_TOP_RIGHT: |
| 286 x = position_iterator->x() - balloon_size.width(); |
| 287 y = position_iterator->y(); |
| 288 position_iterator->set_y(position_iterator->y() + balloon_size.height() + |
| 289 InterBalloonMargin()); |
| 290 break; |
| 291 case VERTICALLY_FROM_BOTTOM_LEFT: |
| 292 position_iterator->set_y(position_iterator->y() - balloon_size.height() - |
| 293 InterBalloonMargin()); |
| 294 x = position_iterator->x(); |
| 295 y = position_iterator->y(); |
| 296 break; |
| 297 case VERTICALLY_FROM_BOTTOM_RIGHT: |
| 298 position_iterator->set_y(position_iterator->y() - balloon_size.height() - |
| 299 InterBalloonMargin()); |
| 300 x = position_iterator->x() - balloon_size.width(); |
| 301 y = position_iterator->y(); |
| 302 break; |
| 303 default: |
| 304 NOTREACHED(); |
| 305 break; |
| 306 } |
| 307 return gfx::Point(x, y); |
| 308 } |
| 309 |
| 310 gfx::Point BalloonCollectionImpl::Layout::OffScreenLocation() const { |
| 311 int x = 0; |
| 312 int y = 0; |
| 313 switch (placement_) { |
| 314 case VERTICALLY_FROM_TOP_LEFT: |
| 315 x = work_area_.x() + HorizontalEdgeMargin(); |
| 316 y = work_area_.y() + kBalloonMaxHeight + VerticalEdgeMargin(); |
| 317 break; |
| 318 case VERTICALLY_FROM_TOP_RIGHT: |
| 319 x = work_area_.right() - kBalloonMaxWidth - HorizontalEdgeMargin(); |
| 320 y = work_area_.y() + kBalloonMaxHeight + VerticalEdgeMargin(); |
| 321 break; |
| 322 case VERTICALLY_FROM_BOTTOM_LEFT: |
| 323 x = work_area_.x() + HorizontalEdgeMargin(); |
| 324 y = work_area_.bottom() + kBalloonMaxHeight + VerticalEdgeMargin(); |
| 325 break; |
| 326 case VERTICALLY_FROM_BOTTOM_RIGHT: |
| 327 x = work_area_.right() - kBalloonMaxWidth - HorizontalEdgeMargin(); |
| 328 y = work_area_.bottom() + kBalloonMaxHeight + VerticalEdgeMargin(); |
| 329 break; |
| 330 default: |
| 331 NOTREACHED(); |
| 332 break; |
| 333 } |
| 334 return gfx::Point(x, y); |
| 335 } |
| 336 |
| 337 // static |
| 338 gfx::Size BalloonCollectionImpl::Layout::ConstrainToSizeLimits( |
| 339 const gfx::Size& size) { |
| 340 // restrict to the min & max sizes |
| 341 return gfx::Size( |
| 342 std::max(min_balloon_width(), |
| 343 std::min(max_balloon_width(), size.width())), |
| 344 std::max(min_balloon_height(), |
| 345 std::min(max_balloon_height(), size.height()))); |
| 346 } |
| 347 |
| 348 bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() { |
| 349 bool changed = false; |
| 350 |
| 351 #if defined(OS_MACOSX) |
| 352 gfx::Rect new_work_area = GetMacWorkArea(); |
| 353 #else |
| 354 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider( |
| 355 WindowSizer::CreateDefaultMonitorInfoProvider()); |
| 356 gfx::Rect new_work_area = info_provider->GetPrimaryMonitorWorkArea(); |
| 357 #endif |
| 358 if (!work_area_.Equals(new_work_area)) { |
| 359 work_area_.SetRect(new_work_area.x(), new_work_area.y(), |
| 360 new_work_area.width(), new_work_area.height()); |
| 361 changed = true; |
| 362 } |
| 363 |
| 364 return changed; |
| 365 } |
| OLD | NEW |