blob: fdabcb5b22ec16a022dea90e3190078cfd8ae969 [file] [log] [blame]
[email protected]83f22072010-03-02 23:47:091// 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// 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]32b76ef2010-07-26 23:08:249#pragma once
[email protected]83f22072010-03-02 23:47:0910
11#include <deque>
12
13#include "base/basictypes.h"
[email protected]099c6c22010-07-15 21:02:0114#include "base/message_loop.h"
[email protected]83f22072010-03-02 23:47:0915#include "chrome/browser/notifications/balloon_collection.h"
[email protected]24c5013f2010-11-19 22:21:0816#include "chrome/browser/notifications/balloon_collection_base.h"
[email protected]08397d52011-02-05 01:53:3817#include "ui/gfx/point.h"
18#include "ui/gfx/rect.h"
[email protected]83f22072010-03-02 23:47:0919
[email protected]099c6c22010-07-15 21:02:0120// 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]83f22072010-03-02 23:47:0929// 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]099c6c22010-07-15 21:02:0133class BalloonCollectionImpl : public BalloonCollection
34#if USE_OFFSETS
35 , public MessageLoopForUI::Observer
36#endif
37{
[email protected]83f22072010-03-02 23:47:0938 public:
39 BalloonCollectionImpl();
40 virtual ~BalloonCollectionImpl();
41
[email protected]099c6c22010-07-15 21:02:0142 // BalloonCollection interface.
[email protected]83f22072010-03-02 23:47:0943 virtual void Add(const Notification& notification,
44 Profile* profile);
[email protected]24c5013f2010-11-19 22:21:0845 virtual bool RemoveById(const std::string& id);
46 virtual bool RemoveBySourceOrigin(const GURL& source_origin);
[email protected]ad0852622010-12-01 19:12:0847 virtual void RemoveAll();
[email protected]83f22072010-03-02 23:47:0948 virtual bool HasSpace() const;
49 virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size);
[email protected]b0b2a3dd2011-01-06 00:30:0550 virtual void SetPositionPreference(PositionPreference position);
[email protected]b71f8182010-03-04 22:12:0251 virtual void DisplayChanged();
[email protected]83f22072010-03-02 23:47:0952 virtual void OnBalloonClosed(Balloon* source);
[email protected]7caf730c2011-02-01 18:35:0853 virtual const Balloons& GetActiveBalloons();
[email protected]83f22072010-03-02 23:47:0954
[email protected]099c6c22010-07-15 21:02:0155 // MessageLoopForUI::Observer interface.
56#if defined(OS_WIN)
57 virtual void WillProcessMessage(const MSG& event) {}
58 virtual void DidProcessMessage(const MSG& event);
59#endif
60#if defined(OS_LINUX)
61 virtual void WillProcessEvent(GdkEvent* event) {}
62 virtual void DidProcessEvent(GdkEvent* event);
63#endif
64
[email protected]83f22072010-03-02 23:47:0965 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]b0b2a3dd2011-01-06 00:30:0572 // These enumerations all are based on a screen orientation where
73 // the origin is the top-left.
74 enum Placement {
75 VERTICALLY_FROM_TOP_LEFT,
76 VERTICALLY_FROM_TOP_RIGHT,
77 VERTICALLY_FROM_BOTTOM_LEFT,
78 VERTICALLY_FROM_BOTTOM_RIGHT
79 };
80
[email protected]83f22072010-03-02 23:47:0981 // Refresh the work area and balloon placement.
82 void OnDisplaySettingsChanged();
83
84 // TODO(johnnyg): Scale the size to account for the system font factor.
85 static int min_balloon_width() { return kBalloonMinWidth; }
86 static int max_balloon_width() { return kBalloonMaxWidth; }
87 static int min_balloon_height() { return kBalloonMinHeight; }
88 static int max_balloon_height() { return kBalloonMaxHeight; }
89
[email protected]c4043d92010-06-24 19:45:5690 // Utility function constrains the input rectangle to the min and max sizes.
91 static gfx::Size ConstrainToSizeLimits(const gfx::Size& rect);
92
[email protected]b0b2a3dd2011-01-06 00:30:0593 void set_placement(Placement placement) { placement_ = placement; }
94
[email protected]83f22072010-03-02 23:47:0995 // 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 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
[email protected]c4043d92010-06-24 19:45:56119 // Return a offscreen location which is offscreen for this layout,
120 // to be used as the initial position for an animation into view.
121 gfx::Point OffScreenLocation() const;
122
[email protected]a2f00ee2011-01-27 18:56:01123 // Returns true if the layout requires offsetting for keeping the close
124 // buttons under the cursor during rapid-close interaction.
125 bool RequiresOffsets() const;
126
[email protected]83f22072010-03-02 23:47:09127 private:
[email protected]83f22072010-03-02 23:47:09128 // Layout parameters
129 int VerticalEdgeMargin() const;
130 int HorizontalEdgeMargin() const;
131 int InterBalloonMargin() const;
132
133 // Minimum and maximum size of balloon content.
134 static const int kBalloonMinWidth = 300;
135 static const int kBalloonMaxWidth = 300;
136 static const int kBalloonMinHeight = 24;
[email protected]cf348912010-10-28 19:31:48137 static const int kBalloonMaxHeight = 160;
[email protected]83f22072010-03-02 23:47:09138
[email protected]b0b2a3dd2011-01-06 00:30:05139 Placement placement_;
[email protected]83f22072010-03-02 23:47:09140 gfx::Rect work_area_;
141 DISALLOW_COPY_AND_ASSIGN(Layout);
142 };
143
144 // Creates a new balloon. Overridable by unit tests. The caller is
145 // responsible for freeing the pointer returned.
146 virtual Balloon* MakeBalloon(const Notification& notification,
147 Profile* profile);
148
[email protected]06cb6ba2011-01-10 19:31:02149 // Gets a bounding box for all the current balloons in screen coordinates.
150 gfx::Rect GetBalloonsBoundingBox() const;
151
[email protected]83f22072010-03-02 23:47:09152 private:
[email protected]83f22072010-03-02 23:47:09153 // Adjusts the positions of the balloons (e.g., when one is closed).
[email protected]3eafbf72010-08-31 19:13:23154 // Implemented by each platform for specific UI requirements.
[email protected]83f22072010-03-02 23:47:09155 void PositionBalloons(bool is_reposition);
156
[email protected]3eafbf72010-08-31 19:13:23157 // Cross-platform internal implementation for PositionBalloons.
158 void PositionBalloonsInternal(bool is_reposition);
159
[email protected]a69ab4dc2010-04-13 16:36:01160#if defined(OS_MACOSX)
161 // Get the work area on Mac OS, without inverting the coordinates.
162 static gfx::Rect GetMacWorkArea();
163#endif
164
[email protected]24c5013f2010-11-19 22:21:08165 // Base implementation for the collection of active balloons.
166 BalloonCollectionBase base_;
167
168 // The layout parameters for balloons in this collection.
169 Layout layout_;
170
[email protected]099c6c22010-07-15 21:02:01171#if USE_OFFSETS
172 // Start and stop observing all UI events.
173 void AddMessageLoopObserver();
174 void RemoveMessageLoopObserver();
175
176 // Cancel all offset and reposition the balloons normally.
177 void CancelOffsets();
178
179 // Handles a mouse motion while the balloons are temporarily offset.
180 void HandleMouseMoveEvent();
181
182 // Is the current cursor in the balloon area?
183 bool IsCursorInBalloonCollection() const;
[email protected]099c6c22010-07-15 21:02:01184
[email protected]099c6c22010-07-15 21:02:01185 // Factory for generating delayed reposition tasks on mouse motion.
186 ScopedRunnableMethodFactory<BalloonCollectionImpl> reposition_factory_;
187
188 // Is the balloon collection currently listening for UI events?
189 bool added_as_message_loop_observer_;
190#endif
191
[email protected]83f22072010-03-02 23:47:09192 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl);
193};
194
195#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_