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 #include "ash/system/status_area_widget.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shelf/shelf_widget.h"
10 #include "ash/shell.h"
11 #include "ash/shell_delegate.h"
12 #include "ash/shell_window_ids.h"
13 #include "ash/system/bluetooth/bluetooth_observer.h"
14 #include "ash/system/overview/overview_button_tray.h"
15 #include "ash/system/status_area_widget_delegate.h"
16 #include "ash/system/tray/system_tray.h"
17 #include "ash/system/tray/system_tray_delegate.h"
18 #include "ash/system/web_notification/web_notification_tray.h"
19 #include "ash/wm/window_properties.h"
20 #include "base/i18n/time_formatting.h"
21 #include "ui/aura/window.h"
22 #include "ui/gfx/screen.h"
24 #if defined(OS_CHROMEOS)
25 #include "ash/system/chromeos/session/logout_button_tray.h"
26 #include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h"
33 const char StatusAreaWidget::kNativeViewName[] = "StatusAreaWidget";
35 StatusAreaWidget::StatusAreaWidget(aura::Window* status_container)
36 : status_area_widget_delegate_(new internal::StatusAreaWidgetDelegate),
37 overview_button_tray_(NULL),
39 web_notification_tray_(NULL),
40 #if defined(OS_CHROMEOS)
41 logout_button_tray_(NULL),
42 virtual_keyboard_tray_(NULL),
44 login_status_(user::LOGGED_IN_NONE) {
45 views::Widget::InitParams params(
46 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
47 params.delegate = status_area_widget_delegate_;
48 params.parent = status_container;
49 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
51 set_focus_on_creation(false);
52 SetContentsView(status_area_widget_delegate_);
53 GetNativeView()->SetName(kNativeViewName);
56 StatusAreaWidget::~StatusAreaWidget() {
59 void StatusAreaWidget::CreateTrayViews() {
60 AddOverviewButtonTray();
62 AddWebNotificationTray();
63 #if defined(OS_CHROMEOS)
64 AddLogoutButtonTray();
65 AddVirtualKeyboardTray();
68 SystemTrayDelegate* delegate =
69 ash::Shell::GetInstance()->system_tray_delegate();
71 // Initialize after all trays have been created.
72 system_tray_->InitializeTrayItems(delegate);
73 web_notification_tray_->Initialize();
74 #if defined(OS_CHROMEOS)
75 logout_button_tray_->Initialize();
76 virtual_keyboard_tray_->Initialize();
78 overview_button_tray_->Initialize();
79 UpdateAfterLoginStatusChange(delegate->GetUserLoginStatus());
82 void StatusAreaWidget::Shutdown() {
83 // Destroy the trays early, causing them to be removed from the view
84 // hierarchy. Do not used scoped pointers since we don't want to destroy them
85 // in the destructor if Shutdown() is not called (e.g. in tests).
86 delete web_notification_tray_;
87 web_notification_tray_ = NULL;
90 #if defined(OS_CHROMEOS)
91 delete virtual_keyboard_tray_;
92 virtual_keyboard_tray_ = NULL;
93 delete logout_button_tray_;
94 logout_button_tray_ = NULL;
96 delete overview_button_tray_;
97 overview_button_tray_ = NULL;
100 bool StatusAreaWidget::ShouldShowShelf() const {
101 if ((system_tray_ && system_tray_->ShouldShowShelf()) ||
102 (web_notification_tray_ &&
103 web_notification_tray_->ShouldBlockShelfAutoHide()))
106 if (!RootWindowController::ForShelf(GetNativeView())->shelf()->IsVisible())
109 // If the shelf is currently visible, don't hide the shelf if the mouse
110 // is in any of the notification bubbles.
111 return (system_tray_ && system_tray_->IsMouseInNotificationBubble()) ||
112 (web_notification_tray_ &&
113 web_notification_tray_->IsMouseInNotificationBubble());
116 bool StatusAreaWidget::IsMessageBubbleShown() const {
117 return ((system_tray_ && system_tray_->IsAnyBubbleVisible()) ||
118 (web_notification_tray_ &&
119 web_notification_tray_->IsMessageCenterBubbleVisible()));
122 void StatusAreaWidget::OnNativeWidgetActivationChanged(bool active) {
123 Widget::OnNativeWidgetActivationChanged(active);
125 status_area_widget_delegate_->SetPaneFocusAndFocusDefault();
128 void StatusAreaWidget::AddSystemTray() {
129 system_tray_ = new SystemTray(this);
130 status_area_widget_delegate_->AddTray(system_tray_);
133 void StatusAreaWidget::AddWebNotificationTray() {
134 web_notification_tray_ = new WebNotificationTray(this);
135 status_area_widget_delegate_->AddTray(web_notification_tray_);
138 #if defined(OS_CHROMEOS)
139 void StatusAreaWidget::AddLogoutButtonTray() {
140 logout_button_tray_ = new LogoutButtonTray(this);
141 status_area_widget_delegate_->AddTray(logout_button_tray_);
144 void StatusAreaWidget::AddVirtualKeyboardTray() {
145 virtual_keyboard_tray_ = new VirtualKeyboardTray(this);
146 status_area_widget_delegate_->AddTray(virtual_keyboard_tray_);
150 void StatusAreaWidget::AddOverviewButtonTray() {
151 overview_button_tray_ = new OverviewButtonTray(this);
152 status_area_widget_delegate_->AddTray(overview_button_tray_);
155 void StatusAreaWidget::SetShelfAlignment(ShelfAlignment alignment) {
156 status_area_widget_delegate_->set_alignment(alignment);
158 system_tray_->SetShelfAlignment(alignment);
159 if (web_notification_tray_)
160 web_notification_tray_->SetShelfAlignment(alignment);
161 #if defined(OS_CHROMEOS)
162 if (logout_button_tray_)
163 logout_button_tray_->SetShelfAlignment(alignment);
164 if (virtual_keyboard_tray_)
165 virtual_keyboard_tray_->SetShelfAlignment(alignment);
167 if (overview_button_tray_)
168 overview_button_tray_->SetShelfAlignment(alignment);
169 status_area_widget_delegate_->UpdateLayout();
172 void StatusAreaWidget::SetHideSystemNotifications(bool hide) {
174 system_tray_->SetHideNotifications(hide);
177 void StatusAreaWidget::UpdateAfterLoginStatusChange(
178 user::LoginStatus login_status) {
179 if (login_status_ == login_status)
181 login_status_ = login_status;
183 system_tray_->UpdateAfterLoginStatusChange(login_status);
184 if (web_notification_tray_)
185 web_notification_tray_->UpdateAfterLoginStatusChange(login_status);
186 #if defined(OS_CHROMEOS)
187 if (logout_button_tray_)
188 logout_button_tray_->UpdateAfterLoginStatusChange(login_status);
192 } // namespace internal