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_SYSTEM_TRAY_H_
      6 #define ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_
      7 
      8 #include "ash/ash_export.h"
      9 #include "ash/system/tray/system_tray_bubble.h"
     10 #include "ash/system/tray/tray_background_view.h"
     11 #include "ash/system/user/login_status.h"
     12 #include "base/basictypes.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/scoped_vector.h"
     16 #include "ui/views/bubble/tray_bubble_view.h"
     17 #include "ui/views/view.h"
     18 
     19 #include <map>
     20 #include <vector>
     21 
     22 namespace ash {
     23 class SystemBubbleWrapper;
     24 class SystemTrayDelegate;
     25 class SystemTrayItem;
     26 class TrayAccessibility;
     27 class TrayDate;
     28 class TrayUser;
     29 
     30 // There are different methods for creating bubble views.
     31 enum BubbleCreationType {
     32   BUBBLE_CREATE_NEW,    // Closes any existing bubble and creates a new one.
     33   BUBBLE_USE_EXISTING,  // Uses any existing bubble, or creates a new one.
     34 };
     35 
     36 class ASH_EXPORT SystemTray : public TrayBackgroundView,
     37                               public views::TrayBubbleView::Delegate {
     38  public:
     39   explicit SystemTray(StatusAreaWidget* status_area_widget);
     40   virtual ~SystemTray();
     41 
     42   // Calls TrayBackgroundView::Initialize(), creates the tray items, and
     43   // adds them to SystemTrayNotifier.
     44   void InitializeTrayItems(SystemTrayDelegate* delegate);
     45 
     46   // Adds a new item in the tray.
     47   void AddTrayItem(SystemTrayItem* item);
     48 
     49   // Removes an existing tray item.
     50   void RemoveTrayItem(SystemTrayItem* item);
     51 
     52   // Returns all tray items that has been added to system tray.
     53   const std::vector<SystemTrayItem*>& GetTrayItems() const;
     54 
     55   // Shows the default view of all items.
     56   void ShowDefaultView(BubbleCreationType creation_type);
     57 
     58   // Shows default view that ingnores outside clicks and activation loss.
     59   void ShowPersistentDefaultView();
     60 
     61   // Shows details of a particular item. If |close_delay_in_seconds| is
     62   // non-zero, then the view is automatically closed after the specified time.
     63   void ShowDetailedView(SystemTrayItem* item,
     64                         int close_delay_in_seconds,
     65                         bool activate,
     66                         BubbleCreationType creation_type);
     67 
     68   // Continue showing the existing detailed view, if any, for |close_delay|
     69   // seconds.
     70   void SetDetailedViewCloseDelay(int close_delay);
     71 
     72   // Hides the detailed view for |item|.
     73   void HideDetailedView(SystemTrayItem* item);
     74 
     75   // Shows the notification view for |item|.
     76   void ShowNotificationView(SystemTrayItem* item);
     77 
     78   // Hides the notification view for |item|.
     79   void HideNotificationView(SystemTrayItem* item);
     80 
     81   // Updates the items when the login status of the system changes.
     82   void UpdateAfterLoginStatusChange(user::LoginStatus login_status);
     83 
     84   // Updates the items when the shelf alignment changes.
     85   void UpdateAfterShelfAlignmentChange(ShelfAlignment alignment);
     86 
     87   // Temporarily hides/unhides the notification bubble.
     88   void SetHideNotifications(bool hidden);
     89 
     90   // Returns true if the shelf should be forced visible when auto-hidden.
     91   bool ShouldShowShelf() const;
     92 
     93   // Returns true if there is a system bubble (already visible or in the process
     94   // of being created).
     95   bool HasSystemBubble() const;
     96 
     97   // Returns true if there is a notification bubble.
     98   bool HasNotificationBubble() const;
     99 
    100   // Returns true if the system_bubble_ exists and is of type |type|.
    101   bool HasSystemBubbleType(SystemTrayBubble::BubbleType type);
    102 
    103   // Returns a pointer to the system bubble or NULL if none.
    104   SystemTrayBubble* GetSystemBubble();
    105 
    106   // Returns true if any bubble is visible.
    107   bool IsAnyBubbleVisible() const;
    108 
    109   // Returns true if the mouse is inside the notification bubble.
    110   bool IsMouseInNotificationBubble() const;
    111 
    112   // Closes system bubble and returns true if it did exist.
    113   bool CloseSystemBubble() const;
    114 
    115   // Returns view for help button if default view is shown. Returns NULL
    116   // otherwise.
    117   views::View* GetHelpButtonView() const;
    118 
    119   // Accessors for testing.
    120 
    121   // Returns true if the bubble exists.
    122   bool CloseNotificationBubbleForTest() const;
    123 
    124   // Overridden from TrayBackgroundView.
    125   virtual void SetShelfAlignment(ShelfAlignment alignment) OVERRIDE;
    126   virtual void AnchorUpdated() OVERRIDE;
    127   virtual base::string16 GetAccessibleNameForTray() OVERRIDE;
    128   virtual void BubbleResized(const views::TrayBubbleView* bubble_view) OVERRIDE;
    129   virtual void HideBubbleWithView(
    130       const views::TrayBubbleView* bubble_view) OVERRIDE;
    131   virtual bool ClickedOutsideBubble() OVERRIDE;
    132 
    133   // Overridden from message_center::TrayBubbleView::Delegate.
    134   virtual void BubbleViewDestroyed() OVERRIDE;
    135   virtual void OnMouseEnteredView() OVERRIDE;
    136   virtual void OnMouseExitedView() OVERRIDE;
    137   virtual base::string16 GetAccessibleNameForBubble() OVERRIDE;
    138   virtual gfx::Rect GetAnchorRect(
    139       views::Widget* anchor_widget,
    140       AnchorType anchor_type,
    141       AnchorAlignment anchor_alignment) const OVERRIDE;
    142   virtual void HideBubble(const views::TrayBubbleView* bubble_view) OVERRIDE;
    143 
    144   TrayAccessibility* GetTrayAccessibilityForTest() {
    145     return tray_accessibility_;
    146   }
    147 
    148   // Get the tray item view (or NULL) for a given |tray_item| in a unit test.
    149   views::View* GetTrayItemViewForTest(SystemTrayItem* tray_item);
    150 
    151   // Gets tray_date_ for browser tests.
    152   TrayDate* GetTrayDateForTesting() const;
    153 
    154  private:
    155   // Creates the default set of items for the sytem tray.
    156   void CreateItems(SystemTrayDelegate* delegate);
    157 
    158   // Resets |system_bubble_| and clears any related state.
    159   void DestroySystemBubble();
    160 
    161   // Resets |notification_bubble_| and clears any related state.
    162   void DestroyNotificationBubble();
    163 
    164   // Calculates the x-offset for the item in the tray. Returns -1 if its tray
    165   // item view is not visible.
    166   int GetTrayXOffset(SystemTrayItem* item) const;
    167 
    168   // Shows the default view and its arrow position is shifted by |x_offset|.
    169   void ShowDefaultViewWithOffset(BubbleCreationType creation_type,
    170                                  int x_offset,
    171                                  bool persistent);
    172 
    173   // Constructs or re-constructs |system_bubble_| and populates it with |items|.
    174   // Specify |change_tray_status| to true if want to change the tray background
    175   // status.
    176   void ShowItems(const std::vector<SystemTrayItem*>& items,
    177                  bool details,
    178                  bool activate,
    179                  BubbleCreationType creation_type,
    180                  int x_offset,
    181                  bool persistent);
    182 
    183   // Constructs or re-constructs |notification_bubble_| and populates it with
    184   // |notification_items_|, or destroys it if there are no notification items.
    185   void UpdateNotificationBubble();
    186 
    187   // Checks the current status of the system tray and updates the web
    188   // notification tray according to the current status.
    189   void UpdateWebNotifications();
    190 
    191   // Deactivate the system tray in the shelf if it was active before.
    192   void CloseSystemBubbleAndDeactivateSystemTray();
    193 
    194   const ScopedVector<SystemTrayItem>& items() const { return items_; }
    195 
    196   // Overridden from ActionableView.
    197   virtual bool PerformAction(const ui::Event& event) OVERRIDE;
    198 
    199   // Owned items.
    200   ScopedVector<SystemTrayItem> items_;
    201 
    202   // Pointers to members of |items_|.
    203   SystemTrayItem* detailed_item_;
    204   std::vector<SystemTrayItem*> notification_items_;
    205 
    206   // Mappings of system tray item and it's view in the tray.
    207   std::map<SystemTrayItem*, views::View*> tray_item_map_;
    208 
    209   // Bubble for default and detailed views.
    210   scoped_ptr<SystemBubbleWrapper> system_bubble_;
    211 
    212   // Bubble for notifications.
    213   scoped_ptr<SystemBubbleWrapper> notification_bubble_;
    214 
    215   // Keep track of the default view height so that when we create detailed
    216   // views directly (e.g. from a notification) we know what height to use.
    217   int default_bubble_height_;
    218 
    219   // Set to true when system notifications should be hidden (e.g. web
    220   // notification bubble is visible).
    221   bool hide_notifications_;
    222 
    223   // This is true when the displayed system tray menu is a full tray menu,
    224   // otherwise a single line item menu like the volume slider is shown.
    225   // Note that the value is only valid when |system_bubble_| is true.
    226   bool full_system_tray_menu_;
    227 
    228   TrayAccessibility* tray_accessibility_;  // not owned
    229   TrayDate* tray_date_;
    230 
    231   DISALLOW_COPY_AND_ASSIGN(SystemTray);
    232 };
    233 
    234 }  // namespace ash
    235 
    236 #endif  // ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_
    237