summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications
diff options
context:
space:
mode:
authorjohnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-29 17:51:36 +0000
committerjohnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-29 17:51:36 +0000
commitc8173315b15ccf58d8062900fd25eebfd56eb327 (patch)
treea73c01e3f081369ec15cc91fefb80b2baf8eb94f /chrome/browser/notifications
parent276aa6a60396329c7fb4a988bd0be7f7d0895651 (diff)
downloadchromium_src-c8173315b15ccf58d8062900fd25eebfd56eb327.zip
chromium_src-c8173315b15ccf58d8062900fd25eebfd56eb327.tar.gz
chromium_src-c8173315b15ccf58d8062900fd25eebfd56eb327.tar.bz2
Adds UI components for desktop notifications, including balloon view classes to display toasts on the screen, and manager for controlling the layout of the balloons.
BUG=none TEST=none yet (part of larger patch) Review URL: http://codereview.chromium.org/338051 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30471 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/notifications')
-rw-r--r--chrome/browser/notifications/balloon.cc43
-rw-r--r--chrome/browser/notifications/balloon.h91
-rw-r--r--chrome/browser/notifications/balloon_collection.cc172
-rw-r--r--chrome/browser/notifications/balloon_collection.h151
-rw-r--r--chrome/browser/notifications/balloon_collection_linux.cc21
-rw-r--r--chrome/browser/notifications/balloon_collection_mac.mm22
-rw-r--r--chrome/browser/notifications/balloon_collection_win.cc54
-rw-r--r--chrome/browser/notifications/balloons.h200
8 files changed, 554 insertions, 200 deletions
diff --git a/chrome/browser/notifications/balloon.cc b/chrome/browser/notifications/balloon.cc
new file mode 100644
index 0000000..71abe66
--- /dev/null
+++ b/chrome/browser/notifications/balloon.cc
@@ -0,0 +1,43 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/notifications/balloon.h"
+
+#include "base/gfx/rect.h"
+#include "base/logging.h"
+#include "chrome/browser/notifications/balloon_collection.h"
+#include "chrome/browser/renderer_host/site_instance.h"
+
+Balloon::Balloon(const Notification& notification, Profile* profile,
+ BalloonCloseListener* listener)
+ : profile_(profile),
+ notification_(notification),
+ close_listener_(listener) {
+}
+
+Balloon::~Balloon() {
+}
+
+void Balloon::SetPosition(const gfx::Point& upper_left, bool reposition) {
+ position_ = upper_left;
+ if (reposition && balloon_view_.get())
+ balloon_view_->RepositionToBalloon();
+}
+
+void Balloon::set_view(BalloonView* balloon_view) {
+ balloon_view_.reset(balloon_view);
+}
+
+void Balloon::Show() {
+ notification_.Display();
+ if (balloon_view_.get()) {
+ balloon_view_->Show(this);
+ }
+}
+
+void Balloon::Close(bool by_user) {
+ notification_.Close(by_user);
+ if (close_listener_)
+ close_listener_->OnBalloonClosed(this);
+}
diff --git a/chrome/browser/notifications/balloon.h b/chrome/browser/notifications/balloon.h
new file mode 100644
index 0000000..5559f09
--- /dev/null
+++ b/chrome/browser/notifications/balloon.h
@@ -0,0 +1,91 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Handles the visible notification (or balloons).
+
+#ifndef CHROME_BROWSER_NOTIFICATIONS_BALLOON_H_
+#define CHROME_BROWSER_NOTIFICATIONS_BALLOON_H_
+
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/gfx/point.h"
+#include "base/gfx/rect.h"
+#include "base/gfx/size.h"
+#include "base/scoped_ptr.h"
+#include "chrome/browser/notifications/notification.h"
+
+class Balloon;
+class Profile;
+class SiteInstance;
+
+// Interface for a view that displays a balloon.
+class BalloonView {
+ public:
+ virtual ~BalloonView() { }
+
+ // Show the view on the screen.
+ virtual void Show(Balloon* balloon) = 0;
+
+ // Reposition the view to match the position of its balloon.
+ virtual void RepositionToBalloon() = 0;
+
+ // Close the view.
+ virtual void Close() = 0;
+};
+
+// Represents a Notification on the screen.
+class Balloon {
+ public:
+ class BalloonCloseListener {
+ public:
+ virtual ~BalloonCloseListener() {}
+
+ // Called when a balloon is closed.
+ virtual void OnBalloonClosed(Balloon* source) = 0;
+ };
+
+ // |listener| may be null in unit tests w/o actual UI.
+ Balloon(const Notification& notification,
+ Profile* profile,
+ BalloonCloseListener* listener);
+ virtual ~Balloon();
+
+ const Notification& notification() const { return notification_; }
+ Profile* profile() const { return profile_; }
+
+ const gfx::Point& position() const { return position_; }
+ void SetPosition(const gfx::Point& upper_left, bool reposition);
+
+ const gfx::Size& size() const { return size_; }
+ void set_size(const gfx::Size& size) { size_ = size; }
+
+ // Provides a view for this balloon. Ownership transfers
+ // to this object.
+ void set_view(BalloonView* balloon_view);
+
+ virtual void Show();
+ virtual void Close(bool by_user);
+
+ private:
+ // Non-owned pointer to the profile.
+ Profile* profile_;
+
+ // The notification being shown in this balloon.
+ Notification notification_;
+
+ // A listener to be called when the balloon closes.
+ BalloonCloseListener* close_listener_;
+
+ // The actual UI element for the balloon.
+ scoped_ptr<BalloonView> balloon_view_;
+
+ // Position and size of the balloon on the screen.
+ gfx::Point position_;
+ gfx::Size size_;
+
+ DISALLOW_COPY_AND_ASSIGN(Balloon);
+};
+
+#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_H_
diff --git a/chrome/browser/notifications/balloon_collection.cc b/chrome/browser/notifications/balloon_collection.cc
new file mode 100644
index 0000000..d979ddd
--- /dev/null
+++ b/chrome/browser/notifications/balloon_collection.cc
@@ -0,0 +1,172 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/notifications/balloon_collection.h"
+
+#include "base/gfx/rect.h"
+#include "base/logging.h"
+#include "base/stl_util-inl.h"
+#include "chrome/browser/notifications/balloon.h"
+
+namespace {
+
+// Portion of the screen allotted for notifications. When notification balloons
+// extend over this, no new notifications are shown until some are closed.
+const double kPercentBalloonFillFactor = 0.7;
+
+// Allow at least this number of balloons on the screen.
+const int kMinAllowedBalloonCount = 2;
+
+} // namespace
+
+// static
+BalloonCollectionImpl::Layout::Placement
+ BalloonCollectionImpl::Layout::placement_ =
+ Layout::VERTICALLY_FROM_BOTTOM_RIGHT;
+
+BalloonCollectionImpl::BalloonCollectionImpl()
+ : space_change_listener_(NULL) {
+}
+
+BalloonCollectionImpl::~BalloonCollectionImpl() {
+ STLDeleteElements(&balloons_);
+}
+
+void BalloonCollectionImpl::Add(const Notification& notification,
+ Profile* profile) {
+ Balloon* new_balloon = MakeBalloon(notification, profile);
+ balloons_.push_back(new_balloon);
+ PositionBalloons(false);
+ new_balloon->Show();
+
+ // There may be no listener in a unit test.
+ if (space_change_listener_)
+ space_change_listener_->OnBalloonSpaceChanged();
+}
+
+bool BalloonCollectionImpl::HasSpace() const {
+ if (count() < kMinAllowedBalloonCount)
+ return true;
+
+ int max_balloon_size = 0;
+ int total_size = 0;
+ layout_.GetMaxLinearSize(&max_balloon_size, &total_size);
+
+ int current_max_size = max_balloon_size * count();
+ int max_allowed_size = static_cast<int>(total_size *
+ kPercentBalloonFillFactor);
+ return current_max_size < max_allowed_size - max_balloon_size;
+}
+
+void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) {
+ // We want to free the balloon when finished.
+ scoped_ptr<Balloon> closed(source);
+ for (Balloons::iterator it = balloons_.begin(); it != balloons_.end(); ++it) {
+ if (*it == source) {
+ balloons_.erase(it);
+ break;
+ }
+ }
+ PositionBalloons(true);
+
+ // There may be no listener in a unit test.
+ if (space_change_listener_)
+ space_change_listener_->OnBalloonSpaceChanged();
+}
+
+void BalloonCollectionImpl::PositionBalloons(bool reposition) {
+ gfx::Point origin = layout_.GetLayoutOrigin();
+ for (Balloons::iterator it = balloons_.begin(); it != balloons_.end(); ++it) {
+ gfx::Point upper_left = layout_.NextPosition((*it)->size(), &origin);
+ (*it)->SetPosition(upper_left, reposition);
+ }
+}
+
+BalloonCollectionImpl::Layout::Layout() {
+ RefreshSystemMetrics();
+}
+
+const void BalloonCollectionImpl::Layout::GetMaxLinearSize(
+ int* max_balloon_size,
+ int* total_size) const {
+ DCHECK(max_balloon_size && total_size);
+
+ switch (placement_) {
+ case HORIZONTALLY_FROM_BOTTOM_LEFT:
+ case HORIZONTALLY_FROM_BOTTOM_RIGHT:
+ *total_size = work_area_.width();
+ *max_balloon_size = max_balloon_width();
+ break;
+ case VERTICALLY_FROM_TOP_RIGHT:
+ case VERTICALLY_FROM_BOTTOM_RIGHT:
+ *total_size = work_area_.height();
+ *max_balloon_size = max_balloon_height();
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+}
+
+gfx::Point BalloonCollectionImpl::Layout::GetLayoutOrigin() const {
+ int x = 0;
+ int y = 0;
+ switch (placement_) {
+ case HORIZONTALLY_FROM_BOTTOM_LEFT:
+ x = work_area_.x();
+ y = work_area_.bottom();
+ break;
+ case HORIZONTALLY_FROM_BOTTOM_RIGHT:
+ x = work_area_.right();
+ y = work_area_.bottom();
+ break;
+ case VERTICALLY_FROM_TOP_RIGHT:
+ x = work_area_.right();
+ y = work_area_.y();
+ break;
+ case VERTICALLY_FROM_BOTTOM_RIGHT:
+ x = work_area_.right();
+ y = work_area_.bottom();
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ return gfx::Point(x, y);
+}
+
+gfx::Point BalloonCollectionImpl::Layout::NextPosition(
+ const gfx::Size& balloon_size,
+ gfx::Point* position_iterator) const {
+ DCHECK(position_iterator);
+
+ int x = 0;
+ int y = 0;
+ switch (placement_) {
+ case HORIZONTALLY_FROM_BOTTOM_LEFT:
+ x = position_iterator->x();
+ y = position_iterator->y() - balloon_size.height();
+ position_iterator->set_x(position_iterator->x() + balloon_size.width());
+ break;
+ case HORIZONTALLY_FROM_BOTTOM_RIGHT:
+ position_iterator->set_x(position_iterator->x() - balloon_size.width());
+ x = position_iterator->x();
+ y = position_iterator->y() - balloon_size.height();
+ break;
+ case VERTICALLY_FROM_TOP_RIGHT:
+ x = position_iterator->x() - balloon_size.width();
+ y = position_iterator->y();
+ position_iterator->set_y(position_iterator->y() + balloon_size.height());
+ break;
+ case VERTICALLY_FROM_BOTTOM_RIGHT:
+ position_iterator->set_y(position_iterator->y() - balloon_size.height());
+ x = position_iterator->x() - balloon_size.width();
+ y = position_iterator->y();
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ return gfx::Point(x, y);
+}
diff --git a/chrome/browser/notifications/balloon_collection.h b/chrome/browser/notifications/balloon_collection.h
new file mode 100644
index 0000000..a756936
--- /dev/null
+++ b/chrome/browser/notifications/balloon_collection.h
@@ -0,0 +1,151 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Handles the visible notification (or balloons).
+
+#ifndef CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_H_
+#define CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_H_
+
+#include <deque>
+
+#include "base/gfx/point.h"
+#include "base/gfx/rect.h"
+#include "base/gfx/size.h"
+#include "chrome/browser/notifications/balloon.h"
+#include "chrome/browser/notifications/notification.h"
+
+class Balloon;
+class Profile;
+
+class BalloonCollection {
+ public:
+ virtual ~BalloonCollection() {}
+
+ // Adds a new balloon for the specified notification.
+ virtual void Add(const Notification& notification,
+ Profile* profile) = 0;
+
+ // Is there room to add another notification?
+ virtual bool HasSpace() const = 0;
+};
+
+// A balloon collection represents a set of notification balloons being
+// shown on the screen. It positions new notifications according to
+// a layout, and monitors for balloons being closed, which it reports
+// up to its parent, the notification UI manager.
+class BalloonCollectionImpl : public BalloonCollection,
+ public Balloon::BalloonCloseListener {
+ public:
+ class BalloonSpaceChangeListener {
+ public:
+ virtual ~BalloonSpaceChangeListener() {}
+
+ // Called when there is more or less space for balloons due to
+ // monitor size changes or balloons disappearing.
+ virtual void OnBalloonSpaceChanged() = 0;
+ };
+
+ BalloonCollectionImpl();
+ virtual ~BalloonCollectionImpl();
+
+ BalloonSpaceChangeListener* space_change_listener() {
+ return space_change_listener_;
+ }
+ void set_space_change_listener(BalloonSpaceChangeListener* listener) {
+ space_change_listener_ = listener;
+ }
+
+ // BalloonCollectionInterface overrides
+ virtual void Add(const Notification& notification,
+ Profile* profile);
+ virtual bool HasSpace() const;
+
+ // Balloon::BalloonCloseListener interface
+ virtual void OnBalloonClosed(Balloon* source);
+
+ protected:
+ // Creates a new balloon. Overridable by unit tests. The caller is
+ // responsible for freeing the pointer returned.
+ virtual Balloon* MakeBalloon(const Notification& notification,
+ Profile* profile);
+
+ private:
+ // The number of balloons being displayed.
+ int count() const { return balloons_.size(); }
+
+ // Adjusts the positions of the balloons (e.g., when one is closed).
+ void PositionBalloons(bool is_reposition);
+
+ // Calculates layout values for the balloons including
+ // the scaling, the max/min sizes, and the upper left corner of each.
+ class Layout {
+ public:
+ Layout();
+
+ // Refresh the work area and balloon placement.
+ void OnDisplaySettingsChanged();
+
+ // TODO(johnnyg): Scale the size to account for the system font factor.
+ int min_balloon_width() const { return kBalloonMinWidth; }
+ int max_balloon_width() const { return kBalloonMaxWidth; }
+ int min_balloon_height() const { return kBalloonMinHeight; }
+ int max_balloon_height() const { return kBalloonMaxHeight; }
+
+ // Returns both the total space available and the maximum
+ // allowed per balloon.
+ //
+ // The size may be a height or length depending on the way that
+ // balloons are laid out.
+ const void GetMaxLinearSize(int* max_balloon_size, int* total_size) const;
+
+ // Refresh the cached values for work area and drawing metrics.
+ // The application should call this method to re-acquire metrics after
+ // any resolution or settings change.
+ // Returns true if and only if a metric changed.
+ bool RefreshSystemMetrics();
+
+ // Returns the origin for the sequence of balloons depending on layout.
+ // Should not be used to place a balloon -- only to call NextPosition().
+ gfx::Point GetLayoutOrigin() const;
+
+ // Compute the position for the next balloon.
+ // Start with *position_iterator = GetLayoutOrigin() and call repeatedly
+ // to get a sequence of positions. Return value is the upper-left coordinate
+ // for each next balloon.
+ gfx::Point NextPosition(const gfx::Size& balloon_size,
+ gfx::Point* position_iterator) const;
+
+ private:
+ enum Placement {
+ HORIZONTALLY_FROM_BOTTOM_LEFT,
+ HORIZONTALLY_FROM_BOTTOM_RIGHT,
+ VERTICALLY_FROM_TOP_RIGHT,
+ VERTICALLY_FROM_BOTTOM_RIGHT
+ };
+
+ // Minimum and maximum size of balloon
+ static const int kBalloonMinWidth = 300;
+ static const int kBalloonMaxWidth = 300;
+ static const int kBalloonMinHeight = 90;
+ static const int kBalloonMaxHeight = 120;
+
+ static Placement placement_;
+ gfx::Rect work_area_;
+ DISALLOW_COPY_AND_ASSIGN(Layout);
+ };
+
+ // Non-owned pointer to an object listening for space changes.
+ BalloonSpaceChangeListener* space_change_listener_;
+
+ // Queue of active balloons.
+ typedef std::deque<Balloon*> Balloons;
+ Balloons balloons_;
+
+ // The layout parameters for balloons in this collection.
+ Layout layout_;
+
+ DISALLOW_COPY_AND_ASSIGN(BalloonCollectionImpl);
+};
+
+#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_COLLECTION_H_
diff --git a/chrome/browser/notifications/balloon_collection_linux.cc b/chrome/browser/notifications/balloon_collection_linux.cc
new file mode 100644
index 0000000..6d29f97
--- /dev/null
+++ b/chrome/browser/notifications/balloon_collection_linux.cc
@@ -0,0 +1,21 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/notifications/balloon_collection.h"
+
+#include "base/gfx/size.h"
+#include "base/logging.h"
+
+Balloon* BalloonCollectionImpl::MakeBalloon(const Notification& notification,
+ Profile* profile) {
+ // TODO(johnnyg): http://crbug.com/23954. Part of future Linux support.
+ NOTIMPLEMENTED();
+ return NULL;
+}
+
+bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() {
+ // TODO(johnnyg): http://crbug.com/23954. Part of future Linux support.
+ NOTIMPLEMENTED();
+ return false;
+}
diff --git a/chrome/browser/notifications/balloon_collection_mac.mm b/chrome/browser/notifications/balloon_collection_mac.mm
new file mode 100644
index 0000000..45728fb
--- /dev/null
+++ b/chrome/browser/notifications/balloon_collection_mac.mm
@@ -0,0 +1,22 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/notifications/balloon_collection.h"
+
+#include "base/gfx/size.h"
+#include "base/logging.h"
+
+Balloon* BalloonCollectionImpl::MakeBalloon(const Notification& notification,
+ Profile* profile) {
+ // TODO(johnnyg): http://crbug.com/23066. Part of future Mac support.
+ NOTIMPLEMENTED();
+ return NULL;
+}
+
+bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() {
+ // TODO(johnnyg): http://crbug.com/23066. Part of future Mac support.
+ NOTIMPLEMENTED();
+ return false;
+}
+
diff --git a/chrome/browser/notifications/balloon_collection_win.cc b/chrome/browser/notifications/balloon_collection_win.cc
new file mode 100644
index 0000000..45f7921
--- /dev/null
+++ b/chrome/browser/notifications/balloon_collection_win.cc
@@ -0,0 +1,54 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/notifications/balloon_collection.h"
+
+#include "base/gfx/rect.h"
+#include "base/logging.h"
+#include "base/stl_util-inl.h"
+#include "chrome/browser/notifications/balloon.h"
+#include "chrome/browser/views/notifications/balloon_view.h"
+
+namespace {
+
+void GetMainScreenWorkArea(gfx::Rect* bounds) {
+ DCHECK(bounds);
+ RECT work_area = {0};
+ if (::SystemParametersInfo(SPI_GETWORKAREA, 0, &work_area, 0)) {
+ bounds->SetRect(work_area.left, work_area.top,
+ work_area.right, work_area.bottom);
+ } else {
+ // If call to ::SystemParametersInfo fails for some reason, we simply get
+ // the full screen size as an alternative.
+ bounds->SetRect(0, 0,
+ ::GetSystemMetrics(SM_CXSCREEN) - 1,
+ ::GetSystemMetrics(SM_CYSCREEN) - 1);
+ }
+}
+
+} // namespace
+
+Balloon* BalloonCollectionImpl::MakeBalloon(const Notification& notification,
+ Profile* profile) {
+ Balloon* balloon = new Balloon(notification, profile, this);
+ balloon->set_view(new BalloonViewImpl());
+ gfx::Size size(layout_.min_balloon_width(), layout_.min_balloon_height());
+ balloon->set_size(size);
+ return balloon;
+}
+
+bool BalloonCollectionImpl::Layout::RefreshSystemMetrics() {
+ bool changed = false;
+
+ gfx::Rect new_work_area(work_area_.x(), work_area_.y(),
+ work_area_.width(), work_area_.height());
+ GetMainScreenWorkArea(&new_work_area);
+ if (!work_area_.Equals(new_work_area)) {
+ work_area_.SetRect(new_work_area.x(), new_work_area.y(),
+ new_work_area.width(), new_work_area.height());
+ changed = true;
+ }
+
+ return changed;
+}
diff --git a/chrome/browser/notifications/balloons.h b/chrome/browser/notifications/balloons.h
deleted file mode 100644
index 6191d53..0000000
--- a/chrome/browser/notifications/balloons.h
+++ /dev/null
@@ -1,200 +0,0 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Handles the visible notification (or balloons).
-
-#ifndef CHROME_BROWSER_NOTIFICATIONS_BALLOONS_H_
-#define CHROME_BROWSER_NOTIFICATIONS_BALLOONS_H_
-
-#include <deque>
-#include <vector>
-
-#include "base/basictypes.h"
-#include "base/gfx/point.h"
-#include "base/gfx/rect.h"
-#include "base/gfx/size.h"
-#include "base/scoped_ptr.h"
-#include "chrome/browser/notifications/notification.h"
-
-class Balloon;
-class BalloonView;
-class Profile;
-class SiteInstance;
-
-class BalloonCloseListener {
- public:
- virtual ~BalloonCloseListener() { }
-
- // Called when a balloon is closed.
- virtual void OnBalloonClosed(Balloon* source) = 0;
-};
-
-class BalloonSpaceChangeListener {
- public:
- virtual ~BalloonSpaceChangeListener() { }
-
- // Called when there is more or less space for balloons due to
- // monitor size changes or balloons disappearing.
- virtual void OnBalloonSpaceChanged() = 0;
-};
-
-class BalloonCollectionInterface {
- public:
- virtual ~BalloonCollectionInterface() {}
-
- // Adds a new balloon for the specified notification.
- virtual void Add(const Notification& notification,
- Profile* profile,
- SiteInstance* site_instance) = 0;
-
- // Is there room to add another notification?
- virtual bool HasSpace() const = 0;
-};
-
-typedef std::deque<Balloon*> Balloons;
-
-// Represents a Notification on the screen.
-class Balloon {
- public:
- Balloon(const Notification& notification,
- Profile* profile,
- SiteInstance* site_instance,
- BalloonCloseListener* listener);
- ~Balloon();
-
- const Notification& notification() const {
- return notification_;
- }
-
- Profile* profile() const {
- return profile_;
- }
-
- SiteInstance* site_instance() const {
- return site_instance_;
- }
-
- void SetPosition(const gfx::Point& upper_left, bool reposition);
- void SetSize(const gfx::Size& size);
- void Show();
- void Close();
-
- const gfx::Point& position() const;
- const gfx::Size& size() const;
-
- private:
- Profile* profile_;
- SiteInstance* site_instance_;
- Notification notification_;
- BalloonCloseListener* close_listener_;
- scoped_ptr<BalloonView> balloon_view_;
- gfx::Point position_;
- gfx::Size size_;
- DISALLOW_COPY_AND_ASSIGN(Balloon);
-};
-
-class BalloonCollection : public BalloonCollectionInterface,
- public BalloonCloseListener {
- public:
- explicit BalloonCollection();
-
- BalloonSpaceChangeListener* space_change_listener() { return listener_; }
- void set_space_change_listener(BalloonSpaceChangeListener* listener) {
- listener_ = listener;
- }
-
- // BalloonCollectionInterface overrides
- virtual void Add(const Notification& notification,
- Profile* profile,
- SiteInstance* site_instance);
- virtual void ShowAll();
- virtual void HideAll();
- virtual bool HasSpace() const;
-
- // BalloonCloseListener interface
- virtual void OnBalloonClosed(Balloon* source);
-
- protected:
- // Overridable by unit tests.
- virtual Balloon* MakeBalloon(const Notification& notification,
- Profile* profile,
- SiteInstance* site_instance) {
- return new Balloon(notification, profile, site_instance, this);
- }
-
- private:
- // The number of balloons being displayed.
- int count() const { return balloons_.size(); }
-
- // Calculates layout values for the balloons including
- // the scaling, the max/min sizes, and the upper left corner of each.
- class Layout {
- public:
- Layout();
-
- // Refresh the work area and balloon placement.
- void OnDisplaySettingsChanged();
-
- int min_balloon_width() const;
- int max_balloon_width() const;
- int min_balloon_height() const;
- int max_balloon_height() const;
-
- // Returns both the total length available and the maximum
- // allowed per balloon.
- //
- // The length may be a height or length depending on the way that
- // balloons are laid out.
- const void GetMaxLengths(int* max_balloon_length, int* total_length) const;
-
- // Scale the size to count in the system font factor.
- int ScaleSize(int size) const;
-
- // Refresh the cached values for work area and drawing metrics.
- // This is done automatically first time and the application should
- // call this method to re-acquire metrics after any
- // resolution or settings change.
- //
- // Return true if and only if a metric changed.
- bool RefreshSystemMetrics();
-
- // Returns the starting value for NextUpperLeftPosition.
- gfx::Point GetStartPosition() const;
-
- // Compute the position for the next balloon.
- // Modifies origin.
- // Returns the position of the upper left coordinate for the given
- // balloon.
- gfx::Point NextPosition(const gfx::Size& balloon_size,
- gfx::Point* origin) const;
-
- private:
- enum Placement {
- HORIZONTALLY_FROM_BOTTOM_LEFT,
- HORIZONTALLY_FROM_BOTTOM_RIGHT,
- VERTICALLY_FROM_TOP_RIGHT,
- VERTICALLY_FROM_BOTTOM_RIGHT
- };
-
- // Minimum and maximum size of balloon
- static const int kBalloonMinWidth = 300;
- static const int kBalloonMaxWidth = 300;
- static const int kBalloonMinHeight = 90;
- static const int kBalloonMaxHeight = 120;
-
- static Placement placement_;
- gfx::Rect work_area_;
- double font_scale_factor_;
- DISALLOW_COPY_AND_ASSIGN(Layout);
- };
-
- // Non-owned pointer to an object listening for space changes.
- BalloonSpaceChangeListener* listener_;
-
- Balloons balloons_;
- Layout layout_;
- DISALLOW_COPY_AND_ASSIGN(BalloonCollection);
-};
-
-#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOONS_H_