OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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.h" |
| 6 |
| 7 #include "base/gfx/rect.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/stl_util-inl.h" |
| 10 #include "chrome/browser/notifications/balloon.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Portion of the screen allotted for notifications. When notification balloons |
| 15 // extend over this, no new notifications are shown until some are closed. |
| 16 const double kPercentBalloonFillFactor = 0.7; |
| 17 |
| 18 // Allow at least this number of balloons on the screen. |
| 19 const int kMinAllowedBalloonCount = 2; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 // static |
| 24 BalloonCollectionImpl::Layout::Placement |
| 25 BalloonCollectionImpl::Layout::placement_ = |
| 26 Layout::VERTICALLY_FROM_BOTTOM_RIGHT; |
| 27 |
| 28 BalloonCollectionImpl::BalloonCollectionImpl() |
| 29 : space_change_listener_(NULL) { |
| 30 } |
| 31 |
| 32 BalloonCollectionImpl::~BalloonCollectionImpl() { |
| 33 STLDeleteElements(&balloons_); |
| 34 } |
| 35 |
| 36 void BalloonCollectionImpl::Add(const Notification& notification, |
| 37 Profile* profile) { |
| 38 Balloon* new_balloon = MakeBalloon(notification, profile); |
| 39 balloons_.push_back(new_balloon); |
| 40 PositionBalloons(false); |
| 41 new_balloon->Show(); |
| 42 |
| 43 // There may be no listener in a unit test. |
| 44 if (space_change_listener_) |
| 45 space_change_listener_->OnBalloonSpaceChanged(); |
| 46 } |
| 47 |
| 48 bool BalloonCollectionImpl::HasSpace() const { |
| 49 if (count() < kMinAllowedBalloonCount) |
| 50 return true; |
| 51 |
| 52 int max_balloon_size = 0; |
| 53 int total_size = 0; |
| 54 layout_.GetMaxLinearSize(&max_balloon_size, &total_size); |
| 55 |
| 56 int current_max_size = max_balloon_size * count(); |
| 57 int max_allowed_size = static_cast<int>(total_size * |
| 58 kPercentBalloonFillFactor); |
| 59 return current_max_size < max_allowed_size - max_balloon_size; |
| 60 } |
| 61 |
| 62 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { |
| 63 // We want to free the balloon when finished. |
| 64 scoped_ptr<Balloon> closed(source); |
| 65 for (Balloons::iterator it = balloons_.begin(); it != balloons_.end(); ++it) { |
| 66 if (*it == source) { |
| 67 balloons_.erase(it); |
| 68 break; |
| 69 } |
| 70 } |
| 71 PositionBalloons(true); |
| 72 |
| 73 // There may be no listener in a unit test. |
| 74 if (space_change_listener_) |
| 75 space_change_listener_->OnBalloonSpaceChanged(); |
| 76 } |
| 77 |
| 78 void BalloonCollectionImpl::PositionBalloons(bool reposition) { |
| 79 gfx::Point origin = layout_.GetLayoutOrigin(); |
| 80 for (Balloons::iterator it = balloons_.begin(); it != balloons_.end(); ++it) { |
| 81 gfx::Point upper_left = layout_.NextPosition((*it)->size(), &origin); |
| 82 (*it)->SetPosition(upper_left, reposition); |
| 83 } |
| 84 } |
| 85 |
| 86 BalloonCollectionImpl::Layout::Layout() { |
| 87 RefreshSystemMetrics(); |
| 88 } |
| 89 |
| 90 const void BalloonCollectionImpl::Layout::GetMaxLinearSize( |
| 91 int* max_balloon_size, |
| 92 int* total_size) const { |
| 93 DCHECK(max_balloon_size && total_size); |
| 94 |
| 95 switch (placement_) { |
| 96 case HORIZONTALLY_FROM_BOTTOM_LEFT: |
| 97 case HORIZONTALLY_FROM_BOTTOM_RIGHT: |
| 98 *total_size = work_area_.width(); |
| 99 *max_balloon_size = max_balloon_width(); |
| 100 break; |
| 101 case VERTICALLY_FROM_TOP_RIGHT: |
| 102 case VERTICALLY_FROM_BOTTOM_RIGHT: |
| 103 *total_size = work_area_.height(); |
| 104 *max_balloon_size = max_balloon_height(); |
| 105 break; |
| 106 default: |
| 107 NOTREACHED(); |
| 108 break; |
| 109 } |
| 110 } |
| 111 |
| 112 gfx::Point BalloonCollectionImpl::Layout::GetLayoutOrigin() const { |
| 113 int x = 0; |
| 114 int y = 0; |
| 115 switch (placement_) { |
| 116 case HORIZONTALLY_FROM_BOTTOM_LEFT: |
| 117 x = work_area_.x(); |
| 118 y = work_area_.bottom(); |
| 119 break; |
| 120 case HORIZONTALLY_FROM_BOTTOM_RIGHT: |
| 121 x = work_area_.right(); |
| 122 y = work_area_.bottom(); |
| 123 break; |
| 124 case VERTICALLY_FROM_TOP_RIGHT: |
| 125 x = work_area_.right(); |
| 126 y = work_area_.y(); |
| 127 break; |
| 128 case VERTICALLY_FROM_BOTTOM_RIGHT: |
| 129 x = work_area_.right(); |
| 130 y = work_area_.bottom(); |
| 131 break; |
| 132 default: |
| 133 NOTREACHED(); |
| 134 break; |
| 135 } |
| 136 return gfx::Point(x, y); |
| 137 } |
| 138 |
| 139 gfx::Point BalloonCollectionImpl::Layout::NextPosition( |
| 140 const gfx::Size& balloon_size, |
| 141 gfx::Point* position_iterator) const { |
| 142 DCHECK(position_iterator); |
| 143 |
| 144 int x = 0; |
| 145 int y = 0; |
| 146 switch (placement_) { |
| 147 case HORIZONTALLY_FROM_BOTTOM_LEFT: |
| 148 x = position_iterator->x(); |
| 149 y = position_iterator->y() - balloon_size.height(); |
| 150 position_iterator->set_x(position_iterator->x() + balloon_size.width()); |
| 151 break; |
| 152 case HORIZONTALLY_FROM_BOTTOM_RIGHT: |
| 153 position_iterator->set_x(position_iterator->x() - balloon_size.width()); |
| 154 x = position_iterator->x(); |
| 155 y = position_iterator->y() - balloon_size.height(); |
| 156 break; |
| 157 case VERTICALLY_FROM_TOP_RIGHT: |
| 158 x = position_iterator->x() - balloon_size.width(); |
| 159 y = position_iterator->y(); |
| 160 position_iterator->set_y(position_iterator->y() + balloon_size.height()); |
| 161 break; |
| 162 case VERTICALLY_FROM_BOTTOM_RIGHT: |
| 163 position_iterator->set_y(position_iterator->y() - balloon_size.height()); |
| 164 x = position_iterator->x() - balloon_size.width(); |
| 165 y = position_iterator->y(); |
| 166 break; |
| 167 default: |
| 168 NOTREACHED(); |
| 169 break; |
| 170 } |
| 171 return gfx::Point(x, y); |
| 172 } |
OLD | NEW |