Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ash / system / status_area_widget.cc
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 #include "ash/system/status_area_widget.h"
6
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"
23
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"
27 #endif
28
29 namespace ash {
30
31 namespace internal {
32
33 const char StatusAreaWidget::kNativeViewName[] = "StatusAreaWidget";
34
35 StatusAreaWidget::StatusAreaWidget(aura::Window* status_container)
36     : status_area_widget_delegate_(new internal::StatusAreaWidgetDelegate),
37       overview_button_tray_(NULL),
38       system_tray_(NULL),
39       web_notification_tray_(NULL),
40 #if defined(OS_CHROMEOS)
41       logout_button_tray_(NULL),
42       virtual_keyboard_tray_(NULL),
43 #endif
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;
50   Init(params);
51   set_focus_on_creation(false);
52   SetContentsView(status_area_widget_delegate_);
53   GetNativeView()->SetName(kNativeViewName);
54 }
55
56 StatusAreaWidget::~StatusAreaWidget() {
57 }
58
59 void StatusAreaWidget::CreateTrayViews() {
60   AddOverviewButtonTray();
61   AddSystemTray();
62   AddWebNotificationTray();
63 #if defined(OS_CHROMEOS)
64   AddLogoutButtonTray();
65   AddVirtualKeyboardTray();
66 #endif
67
68   SystemTrayDelegate* delegate =
69       ash::Shell::GetInstance()->system_tray_delegate();
70   DCHECK(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();
77 #endif
78   overview_button_tray_->Initialize();
79   UpdateAfterLoginStatusChange(delegate->GetUserLoginStatus());
80 }
81
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;
88   delete system_tray_;
89   system_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;
95 #endif
96   delete overview_button_tray_;
97   overview_button_tray_ = NULL;
98 }
99
100 bool StatusAreaWidget::ShouldShowShelf() const {
101   if ((system_tray_ && system_tray_->ShouldShowShelf()) ||
102       (web_notification_tray_ &&
103        web_notification_tray_->ShouldBlockShelfAutoHide()))
104     return true;
105
106   if (!RootWindowController::ForShelf(GetNativeView())->shelf()->IsVisible())
107     return false;
108
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());
114 }
115
116 bool StatusAreaWidget::IsMessageBubbleShown() const {
117   return ((system_tray_ && system_tray_->IsAnyBubbleVisible()) ||
118           (web_notification_tray_ &&
119            web_notification_tray_->IsMessageCenterBubbleVisible()));
120 }
121
122 void StatusAreaWidget::OnNativeWidgetActivationChanged(bool active) {
123   Widget::OnNativeWidgetActivationChanged(active);
124   if (active)
125     status_area_widget_delegate_->SetPaneFocusAndFocusDefault();
126 }
127
128 void StatusAreaWidget::AddSystemTray() {
129   system_tray_ = new SystemTray(this);
130   status_area_widget_delegate_->AddTray(system_tray_);
131 }
132
133 void StatusAreaWidget::AddWebNotificationTray() {
134   web_notification_tray_ = new WebNotificationTray(this);
135   status_area_widget_delegate_->AddTray(web_notification_tray_);
136 }
137
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_);
142 }
143
144 void StatusAreaWidget::AddVirtualKeyboardTray() {
145   virtual_keyboard_tray_ = new VirtualKeyboardTray(this);
146   status_area_widget_delegate_->AddTray(virtual_keyboard_tray_);
147 }
148 #endif
149
150 void StatusAreaWidget::AddOverviewButtonTray() {
151   overview_button_tray_ = new OverviewButtonTray(this);
152   status_area_widget_delegate_->AddTray(overview_button_tray_);
153 }
154
155 void StatusAreaWidget::SetShelfAlignment(ShelfAlignment alignment) {
156   status_area_widget_delegate_->set_alignment(alignment);
157   if (system_tray_)
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);
166 #endif
167   if (overview_button_tray_)
168     overview_button_tray_->SetShelfAlignment(alignment);
169   status_area_widget_delegate_->UpdateLayout();
170 }
171
172 void StatusAreaWidget::SetHideSystemNotifications(bool hide) {
173   if (system_tray_)
174     system_tray_->SetHideNotifications(hide);
175 }
176
177 void StatusAreaWidget::UpdateAfterLoginStatusChange(
178     user::LoginStatus login_status) {
179   if (login_status_ == login_status)
180     return;
181   login_status_ = login_status;
182   if (system_tray_)
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);
189 #endif
190 }
191
192 }  // namespace internal
193 }  // namespace ash