Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / system / tray / system_tray.h
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 shelf should be forced visible when auto-hidden.
98   bool ShouldShowShelf() 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   // Gets tray_date_ for browser tests.
162   internal::TrayDate* GetTrayDateForTesting() const;
163
164  private:
165   // Creates the default set of items for the sytem tray.
166   void CreateItems(SystemTrayDelegate* delegate);
167
168   // Resets |system_bubble_| and clears any related state.
169   void DestroySystemBubble();
170
171   // Resets |notification_bubble_| and clears any related state.
172   void DestroyNotificationBubble();
173
174   // Calculates the x-offset for the item in the tray. Returns -1 if its tray
175   // item view is not visible.
176   int GetTrayXOffset(SystemTrayItem* item) const;
177
178   // Shows the default view and its arrow position is shifted by |x_offset|.
179   void ShowDefaultViewWithOffset(BubbleCreationType creation_type,
180                                  int x_offset,
181                                  bool persistent);
182
183   // Constructs or re-constructs |system_bubble_| and populates it with |items|.
184   // Specify |change_tray_status| to true if want to change the tray background
185   // status.
186   void ShowItems(const std::vector<SystemTrayItem*>& items,
187                  bool details,
188                  bool activate,
189                  BubbleCreationType creation_type,
190                  int x_offset,
191                  bool persistent);
192
193   // Constructs or re-constructs |notification_bubble_| and populates it with
194   // |notification_items_|, or destroys it if there are no notification items.
195   void UpdateNotificationBubble();
196
197   // Checks the current status of the system tray and updates the web
198   // notification tray according to the current status.
199   void UpdateWebNotifications();
200
201   // Deactivate the system tray in the shelf if it was active before.
202   void CloseSystemBubbleAndDeactivateSystemTray();
203
204   const ScopedVector<SystemTrayItem>& items() const { return items_; }
205
206   // Overridden from internal::ActionableView.
207   virtual bool PerformAction(const ui::Event& event) OVERRIDE;
208
209   // Owned items.
210   ScopedVector<SystemTrayItem> items_;
211
212   // User items - note, this is a subset of the |items_| list. Note that no
213   // item in this list needs to be deleted.
214   std::vector<internal::TrayUser*> user_items_;
215
216   // Pointers to members of |items_|.
217   SystemTrayItem* detailed_item_;
218   std::vector<SystemTrayItem*> notification_items_;
219
220   // Mappings of system tray item and it's view in the tray.
221   std::map<SystemTrayItem*, views::View*> tray_item_map_;
222
223   // Bubble for default and detailed views.
224   scoped_ptr<internal::SystemBubbleWrapper> system_bubble_;
225
226   // Bubble for notifications.
227   scoped_ptr<internal::SystemBubbleWrapper> notification_bubble_;
228
229   // Keep track of the default view height so that when we create detailed
230   // views directly (e.g. from a notification) we know what height to use.
231   int default_bubble_height_;
232
233   // Set to true when system notifications should be hidden (e.g. web
234   // notification bubble is visible).
235   bool hide_notifications_;
236
237   // This is true when the displayed system tray menu is a full tray menu,
238   // otherwise a single line item menu like the volume slider is shown.
239   // Note that the value is only valid when |system_bubble_| is true.
240   bool full_system_tray_menu_;
241
242   internal::TrayAccessibility* tray_accessibility_;  // not owned
243   internal::TrayDate* tray_date_;
244
245   DISALLOW_COPY_AND_ASSIGN(SystemTray);
246 };
247
248 }  // namespace ash
249
250 #endif  // ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_