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