Home | History | Annotate | Download | only in bubble
      1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_BUBBLE_TRAY_BUBBLE_VIEW_H_
      6 #define UI_VIEWS_BUBBLE_TRAY_BUBBLE_VIEW_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "ui/views/bubble/bubble_delegate.h"
     10 #include "ui/views/mouse_watcher.h"
     11 #include "ui/views/views_export.h"
     12 
     13 // Specialized bubble view for bubbles associated with a tray icon (e.g. the
     14 // Ash status area). Mostly this handles custom anchor location and arrow and
     15 // border rendering. This also has its own delegate for handling mouse events
     16 // and other implementation specific details.
     17 
     18 namespace ui {
     19 class LocatedEvent;
     20 }
     21 
     22 namespace views {
     23 class View;
     24 class Widget;
     25 }
     26 
     27 namespace views {
     28 
     29 namespace internal {
     30 class TrayBubbleBorder;
     31 class TrayBubbleContentMask;
     32 }
     33 
     34 class VIEWS_EXPORT TrayBubbleView : public views::BubbleDelegateView,
     35                                     public views::MouseWatcherListener {
     36  public:
     37   // AnchorType differentiates between bubbles that are anchored on a tray
     38   // element (ANCHOR_TYPE_TRAY) and display an arrow, or that are floating on
     39   // the screen away from the tray (ANCHOR_TYPE_BUBBLE).
     40   enum AnchorType {
     41     ANCHOR_TYPE_TRAY,
     42     ANCHOR_TYPE_BUBBLE,
     43   };
     44 
     45   // AnchorAlignment determines to which side of the anchor the bubble will
     46   // align itself.
     47   enum AnchorAlignment {
     48     ANCHOR_ALIGNMENT_BOTTOM,
     49     ANCHOR_ALIGNMENT_LEFT,
     50     ANCHOR_ALIGNMENT_RIGHT,
     51     ANCHOR_ALIGNMENT_TOP
     52   };
     53 
     54   class VIEWS_EXPORT Delegate {
     55    public:
     56     typedef TrayBubbleView::AnchorType AnchorType;
     57     typedef TrayBubbleView::AnchorAlignment AnchorAlignment;
     58 
     59     Delegate() {}
     60     virtual ~Delegate() {}
     61 
     62     // Called when the view is destroyed. Any pointers to the view should be
     63     // cleared when this gets called.
     64     virtual void BubbleViewDestroyed() = 0;
     65 
     66     // Called when the mouse enters/exits the view.
     67     // Note: This event will only be called if the mouse gets actively moved by
     68     // the user to enter the view.
     69     virtual void OnMouseEnteredView() = 0;
     70     virtual void OnMouseExitedView() = 0;
     71 
     72     // Called from GetAccessibleState(); should return the appropriate
     73     // accessible name for the bubble.
     74     virtual string16 GetAccessibleNameForBubble() = 0;
     75 
     76     // Passes responsibility for BubbleDelegateView::GetAnchorRect to the
     77     // delegate.
     78     virtual gfx::Rect GetAnchorRect(views::Widget* anchor_widget,
     79                                     AnchorType anchor_type,
     80                                     AnchorAlignment anchor_alignment) = 0;
     81 
     82     // Called when a bubble wants to hide/destroy itself (e.g. last visible
     83     // child view was closed).
     84     virtual void HideBubble(const TrayBubbleView* bubble_view) = 0;
     85 
     86    private:
     87     DISALLOW_COPY_AND_ASSIGN(Delegate);
     88   };
     89 
     90   struct VIEWS_EXPORT InitParams {
     91     static const int kArrowDefaultOffset;
     92 
     93     InitParams(AnchorType anchor_type,
     94                AnchorAlignment anchor_alignment,
     95                int min_width,
     96                int max_width);
     97     AnchorType anchor_type;
     98     AnchorAlignment anchor_alignment;
     99     int min_width;
    100     int max_width;
    101     int max_height;
    102     bool can_activate;
    103     bool close_on_deactivate;
    104     SkColor arrow_color;
    105     bool first_item_has_no_margin;
    106     views::BubbleBorder::Arrow arrow;
    107     int arrow_offset;
    108     views::BubbleBorder::ArrowPaintType arrow_paint_type;
    109     views::BubbleBorder::Shadow shadow;
    110     views::BubbleBorder::BubbleAlignment arrow_alignment;
    111   };
    112 
    113   // Constructs and returns a TrayBubbleView. init_params may be modified.
    114   static TrayBubbleView* Create(gfx::NativeView parent_window,
    115                                 views::View* anchor,
    116                                 Delegate* delegate,
    117                                 InitParams* init_params);
    118 
    119   virtual ~TrayBubbleView();
    120 
    121   // Sets up animations, and show the bubble. Must occur after CreateBubble()
    122   // is called.
    123   void InitializeAndShowBubble();
    124 
    125   // Called whenever the bubble size or location may have changed.
    126   void UpdateBubble();
    127 
    128   // Sets the maximum bubble height and resizes the bubble.
    129   void SetMaxHeight(int height);
    130 
    131   // Sets the bubble width.
    132   void SetWidth(int width);
    133 
    134   // Sets whether or not to paint the bubble border arrow.
    135   void SetArrowPaintType(views::BubbleBorder::ArrowPaintType arrow_paint_type);
    136 
    137   // Returns the border insets. Called by TrayEventFilter.
    138   gfx::Insets GetBorderInsets() const;
    139 
    140   // Called when the delegate is destroyed.
    141   void reset_delegate() { delegate_ = NULL; }
    142 
    143   Delegate* delegate() { return delegate_; }
    144 
    145   void set_gesture_dragging(bool dragging) { is_gesture_dragging_ = dragging; }
    146   bool is_gesture_dragging() const { return is_gesture_dragging_; }
    147 
    148   // Overridden from views::WidgetDelegate.
    149   virtual bool CanActivate() const OVERRIDE;
    150   virtual views::NonClientFrameView* CreateNonClientFrameView(
    151       views::Widget* widget) OVERRIDE;
    152   virtual bool WidgetHasHitTestMask() const OVERRIDE;
    153   virtual void GetWidgetHitTestMask(gfx::Path* mask) const OVERRIDE;
    154 
    155   // Overridden from views::BubbleDelegateView.
    156   virtual gfx::Rect GetAnchorRect() OVERRIDE;
    157 
    158   // Overridden from views::View.
    159   virtual gfx::Size GetPreferredSize() OVERRIDE;
    160   virtual gfx::Size GetMaximumSize() OVERRIDE;
    161   virtual int GetHeightForWidth(int width) OVERRIDE;
    162   virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
    163   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
    164   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
    165 
    166   // Overridden from MouseWatcherListener
    167   virtual void MouseMovedOutOfHost() OVERRIDE;
    168 
    169  protected:
    170   TrayBubbleView(gfx::NativeView parent_window,
    171                  views::View* anchor,
    172                  Delegate* delegate,
    173                  const InitParams& init_params);
    174 
    175   // Overridden from views::BubbleDelegateView.
    176   virtual void Init() OVERRIDE;
    177 
    178   // Overridden from views::View.
    179   virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
    180   virtual void ViewHierarchyChanged(
    181       const ViewHierarchyChangedDetails& details) OVERRIDE;
    182 
    183  private:
    184   InitParams params_;
    185   Delegate* delegate_;
    186   int preferred_width_;
    187   internal::TrayBubbleBorder* bubble_border_;
    188   scoped_ptr<internal::TrayBubbleContentMask> bubble_content_mask_;
    189   bool is_gesture_dragging_;
    190 
    191   // True once the mouse cursor was actively moved by the user over the bubble.
    192   // Only then the OnMouseExitedView() event will get passed on to listeners.
    193   bool mouse_actively_entered_;
    194 
    195   // Used to find any mouse movements.
    196   scoped_ptr<MouseWatcher> mouse_watcher_;
    197 
    198   DISALLOW_COPY_AND_ASSIGN(TrayBubbleView);
    199 };
    200 
    201 }  // namespace views
    202 
    203 #endif  // UI_VIEWS_BUBBLE_TRAY_BUBBLE_VIEW_H_
    204