blob: d1307355076986d38fa5fd95e69c214ebc9af7de [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]e0fc2f12010-03-14 23:30:5916#include "gfx/point.h"
17#include "gfx/rect.h"
[email protected]83f22072010-03-02 23:47:0918
[email protected]099c6c22010-07-15 21:02:0119// Mac balloons grow from the top down and have close buttons on top, so
20// offsetting is not necessary for easy multiple-closing. Other platforms grow
21// from the bottom up and have close buttons on top, so it is necessary.
22#if defined(OS_MACOSX)
23#define USE_OFFSETS 0
24#else
25#define USE_OFFSETS 1
26#endif
27
[email protected]83f22072010-03-02 23:47:0928// A balloon collection represents a set of notification balloons being
29// shown on the screen. It positions new notifications according to
30// a layout, and monitors for balloons being closed, which it reports
31// up to its parent, the notification UI manager.
[email protected]099c6c22010-07-15 21:02:0132class BalloonCollectionImpl : public BalloonCollection
33#if USE_OFFSETS
34 , public MessageLoopForUI::Observer
35#endif
36{
[email protected]83f22072010-03-02 23:47:0937 public:
38 BalloonCollectionImpl();
39 virtual ~BalloonCollectionImpl();
40
[email protected]099c6c22010-07-15 21:02:0141 // BalloonCollection interface.
[email protected]83f22072010-03-02 23:47:0942 virtual void Add(const Notification& notification,
43 Profile* profile);
44 virtual bool Remove(const Notification& notification);
45 virtual bool HasSpace() const;
46 virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size);
[email protected]b71f8182010-03-04 22:12:0247 virtual void DisplayChanged();
[email protected]83f22072010-03-02 23:47:0948 virtual void OnBalloonClosed(Balloon* source);
[email protected]d19ee3442010-04-08 22:14:3949 virtual const Balloons& GetActiveBalloons() {
50 return balloons_;
51 }
[email protected]83f22072010-03-02 23:47:0952
[email protected]099c6c22010-07-15 21:02:0153 // MessageLoopForUI::Observer interface.
54#if defined(OS_WIN)
55 virtual void WillProcessMessage(const MSG& event) {}
56 virtual void DidProcessMessage(const MSG& event);
57#endif
58#if defined(OS_LINUX)
59 virtual void WillProcessEvent(GdkEvent* event) {}
60 virtual void DidProcessEvent(GdkEvent* event);
61#endif
62
[email protected]83f22072010-03-02 23:47:0963 protected:
64 // Calculates layout values for the balloons including
65 // the scaling, the max/min sizes, and the upper left corner of each.
66 class Layout {
67 public:
68 Layout();
69
70 // Refresh the work area and balloon placement.
71 void OnDisplaySettingsChanged();
72
73 // TODO(johnnyg): Scale the size to account for the system font factor.
74 static int min_balloon_width() { return kBalloonMinWidth; }
75 static int max_balloon_width() { return kBalloonMaxWidth; }
76 static int min_balloon_height() { return kBalloonMinHeight; }
77 static int max_balloon_height() { return kBalloonMaxHeight; }
78
[email protected]c4043d92010-06-24 19:45:5679 // Utility function constrains the input rectangle to the min and max sizes.
80 static gfx::Size ConstrainToSizeLimits(const gfx::Size& rect);
81
[email protected]83f22072010-03-02 23:47:0982 // Returns both the total space available and the maximum
83 // allowed per balloon.
84 //
85 // The size may be a height or length depending on the way that
86 // balloons are laid out.
87 void GetMaxLinearSize(int* max_balloon_size, int* total_size) const;
88
89 // Refresh the cached values for work area and drawing metrics.
90 // The application should call this method to re-acquire metrics after
91 // any resolution or settings change.
92 // Returns true if and only if a metric changed.
93 bool RefreshSystemMetrics();
94
95 // Returns the origin for the sequence of balloons depending on layout.
96 // Should not be used to place a balloon -- only to call NextPosition().
97 gfx::Point GetLayoutOrigin() const;
98
99 // Compute the position for the next balloon.
100 // Start with *position_iterator = GetLayoutOrigin() and call repeatedly
101 // to get a sequence of positions. Return value is the upper-left coordinate
102 // for each next balloon.
103 gfx::Point NextPosition(const gfx::Size& balloon_size,
104 gfx::Point* position_iterator) const;
105
[email protected]c4043d92010-06-24 19:45:56106 // Return a offscreen location which is offscreen for this layout,
107 // to be used as the initial position for an animation into view.
108 gfx::Point OffScreenLocation() const;
109
[email protected]83f22072010-03-02 23:47:09110 private:
111 enum Placement {
[email protected]83f22072010-03-02 23:47:09112 VERTICALLY_FROM_TOP_RIGHT,
113 VERTICALLY_FROM_BOTTOM_RIGHT
114 };
115
116 // Layout parameters
117 int VerticalEdgeMargin() const;
118 int HorizontalEdgeMargin() const;
119 int InterBalloonMargin() const;
120
121 // Minimum and maximum size of balloon content.
122 static const int kBalloonMinWidth = 300;
123 static const int kBalloonMaxWidth = 300;
124 static const int kBalloonMinHeight = 24;
125 static const int kBalloonMaxHeight = 120;
126
127 static Placement placement_;
128 gfx::Rect work_area_;
129 DISALLOW_COPY_AND_ASSIGN(Layout);
130 };
131
132 // Creates a new balloon. Overridable by unit tests. The caller is
133 // responsible for freeing the pointer returned.
134 virtual Balloon* MakeBalloon(const Notification& notification,
135 Profile* profile);
136
137 private:
138 // The number of balloons being displayed.
139 int count() const { return balloons_.size(); }
140
141 // Adjusts the positions of the balloons (e.g., when one is closed).
142 void PositionBalloons(bool is_reposition);
143
[email protected]a69ab4dc2010-04-13 16:36:01144#if defined(OS_MACOSX)
145 // Get the work area on Mac OS, without inverting the coordinates.
146 static gfx::Rect GetMacWorkArea();
147#endif
148
[email protected]099c6c22010-07-15 21:02:01149#if USE_OFFSETS
150 // Start and stop observing all UI events.
151 void AddMessageLoopObserver();
152 void RemoveMessageLoopObserver();
153
154 // Cancel all offset and reposition the balloons normally.
155 void CancelOffsets();
156
157 // Handles a mouse motion while the balloons are temporarily offset.
158 void HandleMouseMoveEvent();
159
160 // Is the current cursor in the balloon area?
161 bool IsCursorInBalloonCollection() const;
162#endif
163
[email protected]83f22072010-03-02 23:47:09164 // Queue of active balloons.
165 typedef std::deque<Balloon*> Balloons;
166 Balloons balloons_;
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 // Factory for generating delayed reposition tasks on mouse motion.
173 ScopedRunnableMethodFactory<BalloonCollectionImpl> reposition_factory_;
174
175 // Is the balloon collection currently listening for UI events?
176 bool added_as_message_loop_observer_;
177#endif
178
[email protected]83f22072010-03-02 23:47:09179 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl);
180};
181
182#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_