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 // Handles the visible notification (or balloons). |
| 6 |
| 7 #ifndef CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_H_ |
| 8 #define CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_H_ |
| 9 |
| 10 #include <deque> |
| 11 |
| 12 #include "base/gfx/point.h" |
| 13 #include "base/gfx/rect.h" |
| 14 #include "base/gfx/size.h" |
| 15 #include "chrome/browser/notifications/balloon.h" |
| 16 #include "chrome/browser/notifications/notification.h" |
| 17 |
| 18 class Balloon; |
| 19 class Profile; |
| 20 |
| 21 class BalloonCollection { |
| 22 public: |
| 23 virtual ~BalloonCollection() {} |
| 24 |
| 25 // Adds a new balloon for the specified notification. |
| 26 virtual void Add(const Notification& notification, |
| 27 Profile* profile) = 0; |
| 28 |
| 29 // Is there room to add another notification? |
| 30 virtual bool HasSpace() const = 0; |
| 31 }; |
| 32 |
| 33 // A balloon collection represents a set of notification balloons being |
| 34 // shown on the screen. It positions new notifications according to |
| 35 // a layout, and monitors for balloons being closed, which it reports |
| 36 // up to its parent, the notification UI manager. |
| 37 class BalloonCollectionImpl : public BalloonCollection, |
| 38 public Balloon::BalloonCloseListener { |
| 39 public: |
| 40 class BalloonSpaceChangeListener { |
| 41 public: |
| 42 virtual ~BalloonSpaceChangeListener() {} |
| 43 |
| 44 // Called when there is more or less space for balloons due to |
| 45 // monitor size changes or balloons disappearing. |
| 46 virtual void OnBalloonSpaceChanged() = 0; |
| 47 }; |
| 48 |
| 49 BalloonCollectionImpl(); |
| 50 virtual ~BalloonCollectionImpl(); |
| 51 |
| 52 BalloonSpaceChangeListener* space_change_listener() { |
| 53 return space_change_listener_; |
| 54 } |
| 55 void set_space_change_listener(BalloonSpaceChangeListener* listener) { |
| 56 space_change_listener_ = listener; |
| 57 } |
| 58 |
| 59 // BalloonCollectionInterface overrides |
| 60 virtual void Add(const Notification& notification, |
| 61 Profile* profile); |
| 62 virtual bool HasSpace() const; |
| 63 |
| 64 // Balloon::BalloonCloseListener interface |
| 65 virtual void OnBalloonClosed(Balloon* source); |
| 66 |
| 67 protected: |
| 68 // Creates a new balloon. Overridable by unit tests. The caller is |
| 69 // responsible for freeing the pointer returned. |
| 70 virtual Balloon* MakeBalloon(const Notification& notification, |
| 71 Profile* profile); |
| 72 |
| 73 private: |
| 74 // The number of balloons being displayed. |
| 75 int count() const { return balloons_.size(); } |
| 76 |
| 77 // Adjusts the positions of the balloons (e.g., when one is closed). |
| 78 void PositionBalloons(bool is_reposition); |
| 79 |
| 80 // Calculates layout values for the balloons including |
| 81 // the scaling, the max/min sizes, and the upper left corner of each. |
| 82 class Layout { |
| 83 public: |
| 84 Layout(); |
| 85 |
| 86 // Refresh the work area and balloon placement. |
| 87 void OnDisplaySettingsChanged(); |
| 88 |
| 89 // TODO(johnnyg): Scale the size to account for the system font factor. |
| 90 int min_balloon_width() const { return kBalloonMinWidth; } |
| 91 int max_balloon_width() const { return kBalloonMaxWidth; } |
| 92 int min_balloon_height() const { return kBalloonMinHeight; } |
| 93 int max_balloon_height() const { return kBalloonMaxHeight; } |
| 94 |
| 95 // Returns both the total space available and the maximum |
| 96 // allowed per balloon. |
| 97 // |
| 98 // The size may be a height or length depending on the way that |
| 99 // balloons are laid out. |
| 100 const void GetMaxLinearSize(int* max_balloon_size, int* total_size) const; |
| 101 |
| 102 // Refresh the cached values for work area and drawing metrics. |
| 103 // The application should call this method to re-acquire metrics after |
| 104 // any resolution or settings change. |
| 105 // Returns true if and only if a metric changed. |
| 106 bool RefreshSystemMetrics(); |
| 107 |
| 108 // Returns the origin for the sequence of balloons depending on layout. |
| 109 // Should not be used to place a balloon -- only to call NextPosition(). |
| 110 gfx::Point GetLayoutOrigin() const; |
| 111 |
| 112 // Compute the position for the next balloon. |
| 113 // Start with *position_iterator = GetLayoutOrigin() and call repeatedly |
| 114 // to get a sequence of positions. Return value is the upper-left coordinate |
| 115 // for each next balloon. |
| 116 gfx::Point NextPosition(const gfx::Size& balloon_size, |
| 117 gfx::Point* position_iterator) const; |
| 118 |
| 119 private: |
| 120 enum Placement { |
| 121 HORIZONTALLY_FROM_BOTTOM_LEFT, |
| 122 HORIZONTALLY_FROM_BOTTOM_RIGHT, |
| 123 VERTICALLY_FROM_TOP_RIGHT, |
| 124 VERTICALLY_FROM_BOTTOM_RIGHT |
| 125 }; |
| 126 |
| 127 // Minimum and maximum size of balloon |
| 128 static const int kBalloonMinWidth = 300; |
| 129 static const int kBalloonMaxWidth = 300; |
| 130 static const int kBalloonMinHeight = 90; |
| 131 static const int kBalloonMaxHeight = 120; |
| 132 |
| 133 static Placement placement_; |
| 134 gfx::Rect work_area_; |
| 135 DISALLOW_COPY_AND_ASSIGN(Layout); |
| 136 }; |
| 137 |
| 138 // Non-owned pointer to an object listening for space changes. |
| 139 BalloonSpaceChangeListener* space_change_listener_; |
| 140 |
| 141 // Queue of active balloons. |
| 142 typedef std::deque<Balloon*> Balloons; |
| 143 Balloons balloons_; |
| 144 |
| 145 // The layout parameters for balloons in this collection. |
| 146 Layout layout_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl); |
| 149 }; |
| 150 |
| 151 #endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_H_ |
OLD | NEW |