blob: 7fb1ea9adc7f53c0a4623238578d3ffb52937c4e [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]e0fc2f12010-03-14 23:30:5917#include "gfx/point.h"
18#include "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]b71f8182010-03-04 22:12:0250 virtual void DisplayChanged();
[email protected]83f22072010-03-02 23:47:0951 virtual void OnBalloonClosed(Balloon* source);
[email protected]24c5013f2010-11-19 22:21:0852 virtual const Balloons& GetActiveBalloons() { return base_.balloons(); }
[email protected]83f22072010-03-02 23:47:0953
[email protected]099c6c22010-07-15 21:02:0154 // MessageLoopForUI::Observer interface.
55#if defined(OS_WIN)
56 virtual void WillProcessMessage(const MSG& event) {}
57 virtual void DidProcessMessage(const MSG& event);
58#endif
59#if defined(OS_LINUX)
60 virtual void WillProcessEvent(GdkEvent* event) {}
61 virtual void DidProcessEvent(GdkEvent* event);
62#endif
63
[email protected]83f22072010-03-02 23:47:0964 protected:
65 // Calculates layout values for the balloons including
66 // the scaling, the max/min sizes, and the upper left corner of each.
67 class Layout {
68 public:
69 Layout();
70
71 // Refresh the work area and balloon placement.
72 void OnDisplaySettingsChanged();
73
74 // TODO(johnnyg): Scale the size to account for the system font factor.
75 static int min_balloon_width() { return kBalloonMinWidth; }
76 static int max_balloon_width() { return kBalloonMaxWidth; }
77 static int min_balloon_height() { return kBalloonMinHeight; }
78 static int max_balloon_height() { return kBalloonMaxHeight; }
79
[email protected]c4043d92010-06-24 19:45:5680 // Utility function constrains the input rectangle to the min and max sizes.
81 static gfx::Size ConstrainToSizeLimits(const gfx::Size& rect);
82
[email protected]83f22072010-03-02 23:47:0983 // Returns both the total space available and the maximum
84 // allowed per balloon.
85 //
86 // The size may be a height or length depending on the way that
87 // balloons are laid out.
88 void GetMaxLinearSize(int* max_balloon_size, int* total_size) const;
89
90 // Refresh the cached values for work area and drawing metrics.
91 // The application should call this method to re-acquire metrics after
92 // any resolution or settings change.
93 // Returns true if and only if a metric changed.
94 bool RefreshSystemMetrics();
95
96 // Returns the origin for the sequence of balloons depending on layout.
97 // Should not be used to place a balloon -- only to call NextPosition().
98 gfx::Point GetLayoutOrigin() const;
99
100 // Compute the position for the next balloon.
101 // Start with *position_iterator = GetLayoutOrigin() and call repeatedly
102 // to get a sequence of positions. Return value is the upper-left coordinate
103 // for each next balloon.
104 gfx::Point NextPosition(const gfx::Size& balloon_size,
105 gfx::Point* position_iterator) const;
106
[email protected]c4043d92010-06-24 19:45:56107 // Return a offscreen location which is offscreen for this layout,
108 // to be used as the initial position for an animation into view.
109 gfx::Point OffScreenLocation() const;
110
[email protected]83f22072010-03-02 23:47:09111 private:
112 enum Placement {
[email protected]83f22072010-03-02 23:47:09113 VERTICALLY_FROM_TOP_RIGHT,
114 VERTICALLY_FROM_BOTTOM_RIGHT
115 };
116
117 // Layout parameters
118 int VerticalEdgeMargin() const;
119 int HorizontalEdgeMargin() const;
120 int InterBalloonMargin() const;
121
122 // Minimum and maximum size of balloon content.
123 static const int kBalloonMinWidth = 300;
124 static const int kBalloonMaxWidth = 300;
125 static const int kBalloonMinHeight = 24;
[email protected]cf348912010-10-28 19:31:48126 static const int kBalloonMaxHeight = 160;
[email protected]83f22072010-03-02 23:47:09127
128 static Placement placement_;
129 gfx::Rect work_area_;
130 DISALLOW_COPY_AND_ASSIGN(Layout);
131 };
132
133 // Creates a new balloon. Overridable by unit tests. The caller is
134 // responsible for freeing the pointer returned.
135 virtual Balloon* MakeBalloon(const Notification& notification,
136 Profile* profile);
137
138 private:
[email protected]83f22072010-03-02 23:47:09139 // Adjusts the positions of the balloons (e.g., when one is closed).
[email protected]3eafbf72010-08-31 19:13:23140 // Implemented by each platform for specific UI requirements.
[email protected]83f22072010-03-02 23:47:09141 void PositionBalloons(bool is_reposition);
142
[email protected]3eafbf72010-08-31 19:13:23143 // Cross-platform internal implementation for PositionBalloons.
144 void PositionBalloonsInternal(bool is_reposition);
145
[email protected]a69ab4dc2010-04-13 16:36:01146#if defined(OS_MACOSX)
147 // Get the work area on Mac OS, without inverting the coordinates.
148 static gfx::Rect GetMacWorkArea();
149#endif
150
[email protected]24c5013f2010-11-19 22:21:08151 // Base implementation for the collection of active balloons.
152 BalloonCollectionBase base_;
153
154 // The layout parameters for balloons in this collection.
155 Layout layout_;
156
[email protected]099c6c22010-07-15 21:02:01157#if USE_OFFSETS
158 // Start and stop observing all UI events.
159 void AddMessageLoopObserver();
160 void RemoveMessageLoopObserver();
161
162 // Cancel all offset and reposition the balloons normally.
163 void CancelOffsets();
164
165 // Handles a mouse motion while the balloons are temporarily offset.
166 void HandleMouseMoveEvent();
167
168 // Is the current cursor in the balloon area?
169 bool IsCursorInBalloonCollection() const;
[email protected]099c6c22010-07-15 21:02:01170
[email protected]099c6c22010-07-15 21:02:01171 // Factory for generating delayed reposition tasks on mouse motion.
172 ScopedRunnableMethodFactory<BalloonCollectionImpl> reposition_factory_;
173
174 // Is the balloon collection currently listening for UI events?
175 bool added_as_message_loop_observer_;
176#endif
177
[email protected]83f22072010-03-02 23:47:09178 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl);
179};
180
181#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_