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/status_area_widget_delegate.h" 15 #include "ash/system/tray/system_tray.h" 16 #include "ash/system/tray/system_tray_delegate.h" 17 #include "ash/system/web_notification/web_notification_tray.h" 18 #include "ash/wm/window_properties.h" 19 #include "base/i18n/time_formatting.h" 20 #include "ui/aura/window.h" 21 #include "ui/gfx/screen.h" 22 23 namespace ash { 24 25 namespace internal { 26 27 const char StatusAreaWidget::kNativeViewName[] = "StatusAreaWidget"; 28 29 StatusAreaWidget::StatusAreaWidget(aura::Window* status_container) 30 : status_area_widget_delegate_(new internal::StatusAreaWidgetDelegate), 31 system_tray_(NULL), 32 web_notification_tray_(NULL), 33 login_status_(user::LOGGED_IN_NONE) { 34 views::Widget::InitParams params( 35 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); 36 params.delegate = status_area_widget_delegate_; 37 params.parent = status_container; 38 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 39 Init(params); 40 set_focus_on_creation(false); 41 SetContentsView(status_area_widget_delegate_); 42 GetNativeView()->SetName(kNativeViewName); 43 } 44 45 StatusAreaWidget::~StatusAreaWidget() { 46 } 47 48 void StatusAreaWidget::CreateTrayViews() { 49 AddSystemTray(); 50 AddWebNotificationTray(); 51 SystemTrayDelegate* delegate = 52 ash::Shell::GetInstance()->system_tray_delegate(); 53 DCHECK(delegate); 54 // Initialize after all trays have been created. 55 if (system_tray_) 56 system_tray_->InitializeTrayItems(delegate); 57 if (web_notification_tray_) 58 web_notification_tray_->Initialize(); 59 UpdateAfterLoginStatusChange(delegate->GetUserLoginStatus()); 60 } 61 62 void StatusAreaWidget::Shutdown() { 63 // Destroy the trays early, causing them to be removed from the view 64 // hierarchy. Do not used scoped pointers since we don't want to destroy them 65 // in the destructor if Shutdown() is not called (e.g. in tests). 66 delete web_notification_tray_; 67 web_notification_tray_ = NULL; 68 delete system_tray_; 69 system_tray_ = NULL; 70 } 71 72 bool StatusAreaWidget::ShouldShowLauncher() const { 73 if ((system_tray_ && system_tray_->ShouldShowLauncher()) || 74 (web_notification_tray_ && 75 web_notification_tray_->ShouldBlockLauncherAutoHide())) 76 return true; 77 78 if (!RootWindowController::ForLauncher(GetNativeView())->shelf()->IsVisible()) 79 return false; 80 81 // If the launcher is currently visible, don't hide the launcher if the mouse 82 // is in any of the notification bubbles. 83 return (system_tray_ && system_tray_->IsMouseInNotificationBubble()) || 84 (web_notification_tray_ && 85 web_notification_tray_->IsMouseInNotificationBubble()); 86 } 87 88 bool StatusAreaWidget::IsMessageBubbleShown() const { 89 return ((system_tray_ && system_tray_->IsAnyBubbleVisible()) || 90 (web_notification_tray_ && 91 web_notification_tray_->IsMessageCenterBubbleVisible())); 92 } 93 94 void StatusAreaWidget::OnNativeWidgetActivationChanged(bool active) { 95 Widget::OnNativeWidgetActivationChanged(active); 96 if (active) 97 status_area_widget_delegate_->SetPaneFocusAndFocusDefault(); 98 } 99 100 void StatusAreaWidget::AddSystemTray() { 101 system_tray_ = new SystemTray(this); 102 status_area_widget_delegate_->AddTray(system_tray_); 103 } 104 105 void StatusAreaWidget::AddWebNotificationTray() { 106 web_notification_tray_ = new WebNotificationTray(this); 107 status_area_widget_delegate_->AddTray(web_notification_tray_); 108 } 109 110 void StatusAreaWidget::SetShelfAlignment(ShelfAlignment alignment) { 111 status_area_widget_delegate_->set_alignment(alignment); 112 if (system_tray_) 113 system_tray_->SetShelfAlignment(alignment); 114 if (web_notification_tray_) 115 web_notification_tray_->SetShelfAlignment(alignment); 116 status_area_widget_delegate_->UpdateLayout(); 117 } 118 119 void StatusAreaWidget::SetHideSystemNotifications(bool hide) { 120 if (system_tray_) 121 system_tray_->SetHideNotifications(hide); 122 } 123 124 bool StatusAreaWidget::ShouldShowWebNotifications() { 125 return !(system_tray_ && system_tray_->IsAnyBubbleVisible()); 126 } 127 128 void StatusAreaWidget::UpdateAfterLoginStatusChange( 129 user::LoginStatus login_status) { 130 if (login_status_ == login_status) 131 return; 132 login_status_ = login_status; 133 if (system_tray_) 134 system_tray_->UpdateAfterLoginStatusChange(login_status); 135 if (web_notification_tray_) 136 web_notification_tray_->UpdateAfterLoginStatusChange(login_status); 137 } 138 139 } // namespace internal 140 } // namespace ash 141