[email protected] | d41925f | 2011-03-22 00:49:28 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 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_IMPL_H_ |
| 8 | #define CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 9 | #pragma once |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 10 | |
| 11 | #include <deque> |
| 12 | |
| 13 | #include "base/basictypes.h" |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 14 | #include "base/message_loop.h" |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 15 | #include "chrome/browser/notifications/balloon_collection.h" |
[email protected] | 24c5013f | 2010-11-19 22:21:08 | [diff] [blame] | 16 | #include "chrome/browser/notifications/balloon_collection_base.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 17 | #include "ui/gfx/point.h" |
| 18 | #include "ui/gfx/rect.h" |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 19 | |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 20 | // Mac balloons grow from the top down and have close buttons on top, so |
| 21 | // offsetting is not necessary for easy multiple-closing. Other platforms grow |
| 22 | // from the bottom up and have close buttons on top, so it is necessary. |
| 23 | #if defined(OS_MACOSX) |
| 24 | #define USE_OFFSETS 0 |
| 25 | #else |
| 26 | #define USE_OFFSETS 1 |
| 27 | #endif |
| 28 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 29 | // A balloon collection represents a set of notification balloons being |
| 30 | // shown on the screen. It positions new notifications according to |
| 31 | // a layout, and monitors for balloons being closed, which it reports |
| 32 | // up to its parent, the notification UI manager. |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 33 | class BalloonCollectionImpl : public BalloonCollection |
| 34 | #if USE_OFFSETS |
| 35 | , public MessageLoopForUI::Observer |
| 36 | #endif |
| 37 | { |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 38 | public: |
| 39 | BalloonCollectionImpl(); |
| 40 | virtual ~BalloonCollectionImpl(); |
| 41 | |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 42 | // BalloonCollection interface. |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 43 | virtual void Add(const Notification& notification, |
| 44 | Profile* profile); |
[email protected] | 24c5013f | 2010-11-19 22:21:08 | [diff] [blame] | 45 | virtual bool RemoveById(const std::string& id); |
| 46 | virtual bool RemoveBySourceOrigin(const GURL& source_origin); |
[email protected] | ad085262 | 2010-12-01 19:12:08 | [diff] [blame] | 47 | virtual void RemoveAll(); |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 48 | virtual bool HasSpace() const; |
| 49 | virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size); |
[email protected] | b0b2a3dd | 2011-01-06 00:30:05 | [diff] [blame] | 50 | virtual void SetPositionPreference(PositionPreference position); |
[email protected] | b71f818 | 2010-03-04 22:12:02 | [diff] [blame] | 51 | virtual void DisplayChanged(); |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 52 | virtual void OnBalloonClosed(Balloon* source); |
[email protected] | 7caf730c | 2011-02-01 18:35:08 | [diff] [blame] | 53 | virtual const Balloons& GetActiveBalloons(); |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 54 | |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 55 | // MessageLoopForUI::Observer interface. |
| 56 | #if defined(OS_WIN) |
| 57 | virtual void WillProcessMessage(const MSG& event) {} |
| 58 | virtual void DidProcessMessage(const MSG& event); |
| 59 | #endif |
[email protected] | 1fd5302c | 2011-05-28 04:06:43 | [diff] [blame] | 60 | #if defined(TOOLKIT_USES_GTK) |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 61 | virtual void WillProcessEvent(GdkEvent* event) {} |
| 62 | virtual void DidProcessEvent(GdkEvent* event); |
| 63 | #endif |
| 64 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 65 | protected: |
| 66 | // Calculates layout values for the balloons including |
| 67 | // the scaling, the max/min sizes, and the upper left corner of each. |
| 68 | class Layout { |
| 69 | public: |
| 70 | Layout(); |
| 71 | |
[email protected] | b0b2a3dd | 2011-01-06 00:30:05 | [diff] [blame] | 72 | // These enumerations all are based on a screen orientation where |
| 73 | // the origin is the top-left. |
| 74 | enum Placement { |
[email protected] | d41925f | 2011-03-22 00:49:28 | [diff] [blame] | 75 | INVALID, |
[email protected] | b0b2a3dd | 2011-01-06 00:30:05 | [diff] [blame] | 76 | VERTICALLY_FROM_TOP_LEFT, |
| 77 | VERTICALLY_FROM_TOP_RIGHT, |
| 78 | VERTICALLY_FROM_BOTTOM_LEFT, |
| 79 | VERTICALLY_FROM_BOTTOM_RIGHT |
| 80 | }; |
| 81 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 82 | // Refresh the work area and balloon placement. |
| 83 | void OnDisplaySettingsChanged(); |
| 84 | |
| 85 | // TODO(johnnyg): Scale the size to account for the system font factor. |
| 86 | static int min_balloon_width() { return kBalloonMinWidth; } |
| 87 | static int max_balloon_width() { return kBalloonMaxWidth; } |
| 88 | static int min_balloon_height() { return kBalloonMinHeight; } |
| 89 | static int max_balloon_height() { return kBalloonMaxHeight; } |
| 90 | |
[email protected] | c4043d9 | 2010-06-24 19:45:56 | [diff] [blame] | 91 | // Utility function constrains the input rectangle to the min and max sizes. |
| 92 | static gfx::Size ConstrainToSizeLimits(const gfx::Size& rect); |
| 93 | |
[email protected] | b0b2a3dd | 2011-01-06 00:30:05 | [diff] [blame] | 94 | void set_placement(Placement placement) { placement_ = placement; } |
| 95 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 96 | // Returns both the total space available and the maximum |
| 97 | // allowed per balloon. |
| 98 | // |
| 99 | // The size may be a height or length depending on the way that |
| 100 | // balloons are laid out. |
| 101 | void GetMaxLinearSize(int* max_balloon_size, int* total_size) const; |
| 102 | |
| 103 | // Refresh the cached values for work area and drawing metrics. |
| 104 | // The application should call this method to re-acquire metrics after |
| 105 | // any resolution or settings change. |
| 106 | // Returns true if and only if a metric changed. |
| 107 | bool RefreshSystemMetrics(); |
| 108 | |
| 109 | // Returns the origin for the sequence of balloons depending on layout. |
| 110 | // Should not be used to place a balloon -- only to call NextPosition(). |
| 111 | gfx::Point GetLayoutOrigin() const; |
| 112 | |
| 113 | // Compute the position for the next balloon. |
| 114 | // Start with *position_iterator = GetLayoutOrigin() and call repeatedly |
| 115 | // to get a sequence of positions. Return value is the upper-left coordinate |
| 116 | // for each next balloon. |
| 117 | gfx::Point NextPosition(const gfx::Size& balloon_size, |
| 118 | gfx::Point* position_iterator) const; |
| 119 | |
[email protected] | c4043d9 | 2010-06-24 19:45:56 | [diff] [blame] | 120 | // Return a offscreen location which is offscreen for this layout, |
| 121 | // to be used as the initial position for an animation into view. |
| 122 | gfx::Point OffScreenLocation() const; |
| 123 | |
[email protected] | a2f00ee | 2011-01-27 18:56:01 | [diff] [blame] | 124 | // Returns true if the layout requires offsetting for keeping the close |
| 125 | // buttons under the cursor during rapid-close interaction. |
| 126 | bool RequiresOffsets() const; |
| 127 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 128 | private: |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 129 | // Layout parameters |
| 130 | int VerticalEdgeMargin() const; |
| 131 | int HorizontalEdgeMargin() const; |
| 132 | int InterBalloonMargin() const; |
| 133 | |
| 134 | // Minimum and maximum size of balloon content. |
| 135 | static const int kBalloonMinWidth = 300; |
| 136 | static const int kBalloonMaxWidth = 300; |
| 137 | static const int kBalloonMinHeight = 24; |
[email protected] | cf34891 | 2010-10-28 19:31:48 | [diff] [blame] | 138 | static const int kBalloonMaxHeight = 160; |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 139 | |
[email protected] | b0b2a3dd | 2011-01-06 00:30:05 | [diff] [blame] | 140 | Placement placement_; |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 141 | gfx::Rect work_area_; |
| 142 | DISALLOW_COPY_AND_ASSIGN(Layout); |
| 143 | }; |
| 144 | |
| 145 | // Creates a new balloon. Overridable by unit tests. The caller is |
| 146 | // responsible for freeing the pointer returned. |
| 147 | virtual Balloon* MakeBalloon(const Notification& notification, |
| 148 | Profile* profile); |
| 149 | |
[email protected] | 06cb6ba | 2011-01-10 19:31:02 | [diff] [blame] | 150 | // Gets a bounding box for all the current balloons in screen coordinates. |
| 151 | gfx::Rect GetBalloonsBoundingBox() const; |
| 152 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 153 | private: |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 154 | // Adjusts the positions of the balloons (e.g., when one is closed). |
[email protected] | 3eafbf7 | 2010-08-31 19:13:23 | [diff] [blame] | 155 | // Implemented by each platform for specific UI requirements. |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 156 | void PositionBalloons(bool is_reposition); |
| 157 | |
[email protected] | 3eafbf7 | 2010-08-31 19:13:23 | [diff] [blame] | 158 | // Cross-platform internal implementation for PositionBalloons. |
| 159 | void PositionBalloonsInternal(bool is_reposition); |
| 160 | |
[email protected] | a69ab4dc | 2010-04-13 16:36:01 | [diff] [blame] | 161 | #if defined(OS_MACOSX) |
| 162 | // Get the work area on Mac OS, without inverting the coordinates. |
| 163 | static gfx::Rect GetMacWorkArea(); |
| 164 | #endif |
| 165 | |
[email protected] | 24c5013f | 2010-11-19 22:21:08 | [diff] [blame] | 166 | // Base implementation for the collection of active balloons. |
| 167 | BalloonCollectionBase base_; |
| 168 | |
| 169 | // The layout parameters for balloons in this collection. |
| 170 | Layout layout_; |
| 171 | |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 172 | #if USE_OFFSETS |
| 173 | // Start and stop observing all UI events. |
| 174 | void AddMessageLoopObserver(); |
| 175 | void RemoveMessageLoopObserver(); |
| 176 | |
| 177 | // Cancel all offset and reposition the balloons normally. |
| 178 | void CancelOffsets(); |
| 179 | |
| 180 | // Handles a mouse motion while the balloons are temporarily offset. |
| 181 | void HandleMouseMoveEvent(); |
| 182 | |
| 183 | // Is the current cursor in the balloon area? |
| 184 | bool IsCursorInBalloonCollection() const; |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 185 | |
[email protected] | 099c6c2 | 2010-07-15 21:02:01 | [diff] [blame] | 186 | // Factory for generating delayed reposition tasks on mouse motion. |
| 187 | ScopedRunnableMethodFactory<BalloonCollectionImpl> reposition_factory_; |
| 188 | |
| 189 | // Is the balloon collection currently listening for UI events? |
| 190 | bool added_as_message_loop_observer_; |
| 191 | #endif |
| 192 | |
[email protected] | 83f2207 | 2010-03-02 23:47:09 | [diff] [blame] | 193 | DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl); |
| 194 | }; |
| 195 | |
| 196 | #endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_ |