blob: c7fe67caddbe82648706b608ef26dcb81ccd9049 [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_
9
10#include <deque>
11
12#include "base/basictypes.h"
[email protected]83f22072010-03-02 23:47:0913#include "chrome/browser/notifications/balloon_collection.h"
[email protected]e0fc2f12010-03-14 23:30:5914#include "gfx/point.h"
15#include "gfx/rect.h"
[email protected]83f22072010-03-02 23:47:0916
17// A balloon collection represents a set of notification balloons being
18// shown on the screen. It positions new notifications according to
19// a layout, and monitors for balloons being closed, which it reports
20// up to its parent, the notification UI manager.
21class BalloonCollectionImpl : public BalloonCollection {
22 public:
23 BalloonCollectionImpl();
24 virtual ~BalloonCollectionImpl();
25
26 // BalloonCollectionInterface overrides
27 virtual void Add(const Notification& notification,
28 Profile* profile);
29 virtual bool Remove(const Notification& notification);
30 virtual bool HasSpace() const;
31 virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size);
[email protected]b71f8182010-03-04 22:12:0232 virtual void DisplayChanged();
[email protected]83f22072010-03-02 23:47:0933 virtual void OnBalloonClosed(Balloon* source);
34
35 protected:
36 // Calculates layout values for the balloons including
37 // the scaling, the max/min sizes, and the upper left corner of each.
38 class Layout {
39 public:
40 Layout();
41
42 // Refresh the work area and balloon placement.
43 void OnDisplaySettingsChanged();
44
45 // TODO(johnnyg): Scale the size to account for the system font factor.
46 static int min_balloon_width() { return kBalloonMinWidth; }
47 static int max_balloon_width() { return kBalloonMaxWidth; }
48 static int min_balloon_height() { return kBalloonMinHeight; }
49 static int max_balloon_height() { return kBalloonMaxHeight; }
50
51 // Returns both the total space available and the maximum
52 // allowed per balloon.
53 //
54 // The size may be a height or length depending on the way that
55 // balloons are laid out.
56 void GetMaxLinearSize(int* max_balloon_size, int* total_size) const;
57
58 // Refresh the cached values for work area and drawing metrics.
59 // The application should call this method to re-acquire metrics after
60 // any resolution or settings change.
61 // Returns true if and only if a metric changed.
62 bool RefreshSystemMetrics();
63
64 // Returns the origin for the sequence of balloons depending on layout.
65 // Should not be used to place a balloon -- only to call NextPosition().
66 gfx::Point GetLayoutOrigin() const;
67
68 // Compute the position for the next balloon.
69 // Start with *position_iterator = GetLayoutOrigin() and call repeatedly
70 // to get a sequence of positions. Return value is the upper-left coordinate
71 // for each next balloon.
72 gfx::Point NextPosition(const gfx::Size& balloon_size,
73 gfx::Point* position_iterator) const;
74
75 private:
76 enum Placement {
77 HORIZONTALLY_FROM_BOTTOM_LEFT,
78 HORIZONTALLY_FROM_BOTTOM_RIGHT,
79 VERTICALLY_FROM_TOP_RIGHT,
80 VERTICALLY_FROM_BOTTOM_RIGHT
81 };
82
83 // Layout parameters
84 int VerticalEdgeMargin() const;
85 int HorizontalEdgeMargin() const;
86 int InterBalloonMargin() const;
87
88 // Minimum and maximum size of balloon content.
89 static const int kBalloonMinWidth = 300;
90 static const int kBalloonMaxWidth = 300;
91 static const int kBalloonMinHeight = 24;
92 static const int kBalloonMaxHeight = 120;
93
94 static Placement placement_;
95 gfx::Rect work_area_;
96 DISALLOW_COPY_AND_ASSIGN(Layout);
97 };
98
99 // Creates a new balloon. Overridable by unit tests. The caller is
100 // responsible for freeing the pointer returned.
101 virtual Balloon* MakeBalloon(const Notification& notification,
102 Profile* profile);
103
104 private:
105 // The number of balloons being displayed.
106 int count() const { return balloons_.size(); }
107
108 // Adjusts the positions of the balloons (e.g., when one is closed).
109 void PositionBalloons(bool is_reposition);
110
111 // Queue of active balloons.
112 typedef std::deque<Balloon*> Balloons;
113 Balloons balloons_;
114
115 // The layout parameters for balloons in this collection.
116 Layout layout_;
117
118 DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl);
119};
120
121#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_IMPL_H_