Home | History | Annotate | Download | only in tray
      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 ASH_SYSTEM_TRAY_TRAY_BACKGROUND_VIEW_H_
      6 #define ASH_SYSTEM_TRAY_TRAY_BACKGROUND_VIEW_H_
      7 
      8 #include "ash/ash_export.h"
      9 #include "ash/shelf/background_animator.h"
     10 #include "ash/shelf/shelf_types.h"
     11 #include "ash/system/tray/actionable_view.h"
     12 #include "ui/views/bubble/tray_bubble_view.h"
     13 
     14 namespace ash {
     15 namespace internal {
     16 
     17 class ShelfLayoutManager;
     18 class StatusAreaWidget;
     19 class TrayEventFilter;
     20 class TrayBackground;
     21 
     22 // Base class for children of StatusAreaWidget: SystemTray, WebNotificationTray.
     23 // This class handles setting and animating the background when the Launcher
     24 // his shown/hidden. It also inherits from ActionableView so that the tray
     25 // items can override PerformAction when clicked on.
     26 class ASH_EXPORT TrayBackgroundView : public ActionableView,
     27                                       public BackgroundAnimatorDelegate {
     28  public:
     29   static const char kViewClassName[];
     30 
     31   // Base class for tray containers. Sets the border and layout. The container
     32   // auto-resizes the widget when necessary.
     33   class TrayContainer : public views::View {
     34    public:
     35     explicit TrayContainer(ShelfAlignment alignment);
     36     virtual ~TrayContainer() {}
     37 
     38     void SetAlignment(ShelfAlignment alignment);
     39 
     40     void set_size(const gfx::Size& size) { size_ = size; }
     41 
     42     // Overridden from views::View.
     43     virtual gfx::Size GetPreferredSize() OVERRIDE;
     44 
     45    protected:
     46     // Overridden from views::View.
     47     virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE;
     48     virtual void ChildVisibilityChanged(View* child) OVERRIDE;
     49     virtual void ViewHierarchyChanged(
     50         const ViewHierarchyChangedDetails& details) OVERRIDE;
     51 
     52    private:
     53     void UpdateLayout();
     54 
     55     ShelfAlignment alignment_;
     56     gfx::Size size_;
     57 
     58     DISALLOW_COPY_AND_ASSIGN(TrayContainer);
     59   };
     60 
     61   explicit TrayBackgroundView(internal::StatusAreaWidget* status_area_widget);
     62   virtual ~TrayBackgroundView();
     63 
     64   // Called after the tray has been added to the widget containing it.
     65   virtual void Initialize();
     66 
     67   // Overridden from views::View.
     68   virtual const char* GetClassName() const OVERRIDE;
     69   virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
     70   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
     71   virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE;
     72   virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE;
     73   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
     74   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
     75 
     76   // Overridden from internal::ActionableView.
     77   virtual bool PerformAction(const ui::Event& event) OVERRIDE;
     78 
     79   // Overridden from internal::BackgroundAnimatorDelegate.
     80   virtual void UpdateBackground(int alpha) OVERRIDE;
     81 
     82   // Called whenever the shelf alignment changes.
     83   virtual void SetShelfAlignment(ShelfAlignment alignment);
     84 
     85   // Called when the anchor (tray or bubble) may have moved or changed.
     86   virtual void AnchorUpdated() {}
     87 
     88   // Called from GetAccessibleState, must return a valid accessible name.
     89   virtual base::string16 GetAccessibleNameForTray() = 0;
     90 
     91   // Hides the bubble associated with |bubble_view|. Called when the widget
     92   // is closed.
     93   virtual void HideBubbleWithView(const views::TrayBubbleView* bubble_view) = 0;
     94 
     95   // Called by the bubble wrapper when a click event occurs outside the bubble.
     96   // May close the bubble. Returns true if the event is handled.
     97   virtual bool ClickedOutsideBubble() = 0;
     98 
     99   // Sets |contents| as a child.
    100   void SetContents(views::View* contents);
    101 
    102   // Creates and sets contents background to |background_|.
    103   void SetContentsBackground();
    104 
    105   // Sets whether the tray paints a background. Default is true, but is set to
    106   // false if a window overlaps the shelf.
    107   void SetPaintsBackground(
    108       bool value,
    109       internal::BackgroundAnimator::ChangeType change_type);
    110 
    111   // Initializes animations for the bubble.
    112   void InitializeBubbleAnimations(views::Widget* bubble_widget);
    113 
    114   // Returns the window hosting the bubble.
    115   aura::Window* GetBubbleWindowContainer() const;
    116 
    117   // Returns the anchor rect for the bubble.
    118   gfx::Rect GetBubbleAnchorRect(
    119       views::Widget* anchor_widget,
    120       views::TrayBubbleView::AnchorType anchor_type,
    121       views::TrayBubbleView::AnchorAlignment anchor_alignment) const;
    122 
    123   // Returns the bubble anchor alignment based on |shelf_alignment_|.
    124   views::TrayBubbleView::AnchorAlignment GetAnchorAlignment() const;
    125 
    126   // Updates the view visual based on the visibility of the bubble.
    127   void SetBubbleVisible(bool visible);
    128 
    129   StatusAreaWidget* status_area_widget() {
    130     return status_area_widget_;
    131   }
    132   const StatusAreaWidget* status_area_widget() const {
    133     return status_area_widget_;
    134   }
    135   TrayContainer* tray_container() const { return tray_container_; }
    136   ShelfAlignment shelf_alignment() const { return shelf_alignment_; }
    137   TrayEventFilter* tray_event_filter() { return tray_event_filter_.get(); }
    138 
    139   ShelfLayoutManager* GetShelfLayoutManager();
    140 
    141   // Updates the arrow visibilty based on the launcher visibilty.
    142   void UpdateBubbleViewArrow(views::TrayBubbleView* bubble_view);
    143 
    144   // Provides the background with a function to query for pressed state.
    145   virtual bool IsPressed();
    146 
    147  private:
    148   class TrayWidgetObserver;
    149 
    150   // Called from Initialize after all status area trays have been created.
    151   // Sets the border based on the position of the view.
    152   void SetBorder();
    153 
    154   // Unowned pointer to parent widget.
    155   StatusAreaWidget* status_area_widget_;
    156 
    157   // Convenience pointer to the contents view.
    158   TrayContainer* tray_container_;
    159 
    160   // Shelf alignment.
    161   ShelfAlignment shelf_alignment_;
    162 
    163   // Owned by the view passed to SetContents().
    164   internal::TrayBackground* background_;
    165 
    166   internal::BackgroundAnimator hide_background_animator_;
    167   internal::BackgroundAnimator hover_background_animator_;
    168   bool hovered_;
    169   bool pressed_;
    170   scoped_ptr<TrayWidgetObserver> widget_observer_;
    171   scoped_ptr<TrayEventFilter> tray_event_filter_;
    172 
    173   DISALLOW_COPY_AND_ASSIGN(TrayBackgroundView);
    174 };
    175 
    176 }  // namespace internal
    177 }  // namespace ash
    178 
    179 #endif  // ASH_SYSTEM_TRAY_TRAY_BACKGROUND_VIEW_H_
    180