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