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