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.
5 #ifndef ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_
6 #define ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_
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"
24 class SystemTrayDelegate;
28 class SystemBubbleWrapper;
29 class TrayAccessibility;
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.
40 class ASH_EXPORT SystemTray : public internal::TrayBackgroundView,
41 public views::TrayBubbleView::Delegate {
43 explicit SystemTray(internal::StatusAreaWidget* status_area_widget);
44 virtual ~SystemTray();
46 // Calls TrayBackgroundView::Initialize(), creates the tray items, and
47 // adds them to SystemTrayNotifier.
48 void InitializeTrayItems(SystemTrayDelegate* delegate);
50 // Adds a new item in the tray.
51 void AddTrayItem(SystemTrayItem* item);
53 // Removes an existing tray item.
54 void RemoveTrayItem(SystemTrayItem* item);
56 // Returns all tray items that has been added to system tray.
57 const std::vector<SystemTrayItem*>& GetTrayItems() const;
59 // Returns all tray user items that were added to the system tray.
60 const std::vector<internal::TrayUser*>& GetTrayUserItems() const;
62 // Shows the default view of all items.
63 void ShowDefaultView(BubbleCreationType creation_type);
65 // Shows default view that ingnores outside clicks and activation loss.
66 void ShowPersistentDefaultView();
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,
73 BubbleCreationType creation_type);
75 // Continue showing the existing detailed view, if any, for |close_delay|
77 void SetDetailedViewCloseDelay(int close_delay);
79 // Hides the detailed view for |item|.
80 void HideDetailedView(SystemTrayItem* item);
82 // Shows the notification view for |item|.
83 void ShowNotificationView(SystemTrayItem* item);
85 // Hides the notification view for |item|.
86 void HideNotificationView(SystemTrayItem* item);
88 // Updates the items when the login status of the system changes.
89 void UpdateAfterLoginStatusChange(user::LoginStatus login_status);
91 // Updates the items when the shelf alignment changes.
92 void UpdateAfterShelfAlignmentChange(ShelfAlignment alignment);
94 // Temporarily hides/unhides the notification bubble.
95 void SetHideNotifications(bool hidden);
97 // Returns true if the shelf should be forced visible when auto-hidden.
98 bool ShouldShowShelf() const;
100 // Returns true if there is a system bubble (already visible or in the process
101 // of being created).
102 bool HasSystemBubble() const;
104 // Returns true if there is a notification bubble.
105 bool HasNotificationBubble() const;
107 // Returns true if the system_bubble_ exists and is of type |type|.
108 bool HasSystemBubbleType(internal::SystemTrayBubble::BubbleType type);
110 // Returns a pointer to the system bubble or NULL if none.
111 internal::SystemTrayBubble* GetSystemBubble();
113 // Returns true if any bubble is visible.
114 bool IsAnyBubbleVisible() const;
116 // Returns true if the mouse is inside the notification bubble.
117 bool IsMouseInNotificationBubble() const;
119 // Closes system bubble and returns true if it did exist.
120 bool CloseSystemBubble() const;
122 // Returns view for help button if default view is shown. Returns NULL
124 views::View* GetHelpButtonView() const;
126 // Accessors for testing.
128 // Returns true if the bubble exists.
129 bool CloseNotificationBubbleForTest() const;
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;
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;
150 internal::TrayAccessibility* GetTrayAccessibilityForTest() {
151 return tray_accessibility_;
154 // Get the tray item view (or NULL) for a given |tray_item| in a unit test.
155 views::View* GetTrayItemViewForTest(SystemTrayItem* tray_item);
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);
161 // Gets tray_date_ for browser tests.
162 internal::TrayDate* GetTrayDateForTesting() const;
165 // Creates the default set of items for the sytem tray.
166 void CreateItems(SystemTrayDelegate* delegate);
168 // Resets |system_bubble_| and clears any related state.
169 void DestroySystemBubble();
171 // Resets |notification_bubble_| and clears any related state.
172 void DestroyNotificationBubble();
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;
178 // Shows the default view and its arrow position is shifted by |x_offset|.
179 void ShowDefaultViewWithOffset(BubbleCreationType creation_type,
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
186 void ShowItems(const std::vector<SystemTrayItem*>& items,
189 BubbleCreationType creation_type,
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();
197 // Checks the current status of the system tray and updates the web
198 // notification tray according to the current status.
199 void UpdateWebNotifications();
201 // Deactivate the system tray in the shelf if it was active before.
202 void CloseSystemBubbleAndDeactivateSystemTray();
204 const ScopedVector<SystemTrayItem>& items() const { return items_; }
206 // Overridden from internal::ActionableView.
207 virtual bool PerformAction(const ui::Event& event) OVERRIDE;
210 ScopedVector<SystemTrayItem> items_;
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_;
216 // Pointers to members of |items_|.
217 SystemTrayItem* detailed_item_;
218 std::vector<SystemTrayItem*> notification_items_;
220 // Mappings of system tray item and it's view in the tray.
221 std::map<SystemTrayItem*, views::View*> tray_item_map_;
223 // Bubble for default and detailed views.
224 scoped_ptr<internal::SystemBubbleWrapper> system_bubble_;
226 // Bubble for notifications.
227 scoped_ptr<internal::SystemBubbleWrapper> notification_bubble_;
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_;
233 // Set to true when system notifications should be hidden (e.g. web
234 // notification bubble is visible).
235 bool hide_notifications_;
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_;
242 internal::TrayAccessibility* tray_accessibility_; // not owned
243 internal::TrayDate* tray_date_;
245 DISALLOW_COPY_AND_ASSIGN(SystemTray);
250 #endif // ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_