Home | History | Annotate | Download | only in notifications
      1 // Copyright (c) 2011 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_H_
      8 #define CHROME_BROWSER_NOTIFICATIONS_BALLOON_H_
      9 #pragma once
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "ui/gfx/point.h"
     14 #include "ui/gfx/rect.h"
     15 #include "ui/gfx/size.h"
     16 
     17 class Balloon;
     18 class BalloonCollection;
     19 class BalloonHost;
     20 class Notification;
     21 class Profile;
     22 class SiteInstance;
     23 
     24 // Interface for a view that displays a balloon.
     25 class BalloonView {
     26  public:
     27   virtual ~BalloonView() { }
     28 
     29   // Show the view on the screen.
     30   virtual void Show(Balloon* balloon) = 0;
     31 
     32   // Notify that the content of notification has chagned.
     33   virtual void Update() = 0;
     34 
     35   // Reposition the view to match the position of its balloon.
     36   virtual void RepositionToBalloon() = 0;
     37 
     38   // Close the view.
     39   virtual void Close(bool by_user) = 0;
     40 
     41   // The total size of the view.
     42   virtual gfx::Size GetSize() const = 0;
     43 
     44   // The host for the view's contents.
     45   virtual BalloonHost* GetHost() const = 0;
     46 };
     47 
     48 // Represents a Notification on the screen.
     49 class Balloon {
     50  public:
     51   Balloon(const Notification& notification,
     52           Profile* profile,
     53           BalloonCollection* collection);
     54   virtual ~Balloon();
     55 
     56   const Notification& notification() const { return *notification_.get(); }
     57   Profile* profile() const { return profile_; }
     58 
     59   gfx::Point GetPosition() const {
     60     return position_.Add(offset_);
     61   }
     62   void SetPosition(const gfx::Point& upper_left, bool reposition);
     63 
     64   const gfx::Point& offset() { return offset_;}
     65   void set_offset(const gfx::Point& offset) { offset_ = offset; }
     66   void add_offset(const gfx::Point& offset) { offset_ = offset_.Add(offset); }
     67 
     68   const gfx::Size& content_size() const { return content_size_; }
     69   void set_content_size(const gfx::Size& size) { content_size_ = size; }
     70 
     71   const BalloonCollection* collection() const { return collection_; }
     72 
     73   const gfx::Size& min_scrollbar_size() const { return min_scrollbar_size_; }
     74   void set_min_scrollbar_size(const gfx::Size& size) {
     75     min_scrollbar_size_ = size;
     76   }
     77 
     78   // Request a new content size for this balloon.  This will get passed
     79   // to the balloon collection for checking against available space and
     80   // min/max restrictions.
     81   void SetContentPreferredSize(const gfx::Size& size);
     82 
     83   // Provides a view for this balloon.  Ownership transfers
     84   // to this object.
     85   void set_view(BalloonView* balloon_view);
     86 
     87   // Returns the balloon view associated with the balloon.
     88   BalloonView* view() const {
     89     return balloon_view_.get();
     90   }
     91 
     92   // Returns the viewing size for the balloon (content + frame).
     93   gfx::Size GetViewSize() const { return balloon_view_->GetSize(); }
     94 
     95   // Shows the balloon.
     96   virtual void Show();
     97 
     98   // Notify that the content of notification has changed.
     99   virtual void Update(const Notification& notification);
    100 
    101   // Called when the balloon is clicked by the user.
    102   virtual void OnClick();
    103 
    104   // Called when the balloon is closed, either by user (through the UI)
    105   // or by a script.
    106   virtual void OnClose(bool by_user);
    107 
    108   // Called by script to cause the balloon to close.
    109   virtual void CloseByScript();
    110 
    111  private:
    112   // Non-owned pointer to the profile.
    113   Profile* profile_;
    114 
    115   // The notification being shown in this balloon.
    116   scoped_ptr<Notification> notification_;
    117 
    118   // The collection that this balloon belongs to.  Non-owned pointer.
    119   BalloonCollection* collection_;
    120 
    121   // The actual UI element for the balloon.
    122   scoped_ptr<BalloonView> balloon_view_;
    123 
    124   // Position and size of the balloon on the screen.
    125   gfx::Point position_;
    126   gfx::Size content_size_;
    127 
    128   // Temporary offset for balloons that need to be positioned in a non-standard
    129   // position for keeping the close buttons under the mouse cursor.
    130   gfx::Point offset_;
    131 
    132   // Smallest size for this balloon where scrollbars will be shown.
    133   gfx::Size min_scrollbar_size_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(Balloon);
    136 };
    137 
    138 #endif  // CHROME_BROWSER_NOTIFICATIONS_BALLOON_H_
    139