15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/system/chromeos/settings/tray_settings.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ash/session/session_state_delegate.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h"
9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ash/system/chromeos/power/power_status.h"
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/system/chromeos/power/power_status_view.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ash/system/tray/actionable_view.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ash/system/tray/fixed_sized_image_view.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray_delegate.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_constants.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_resources.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_strings.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkColor.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/image_view.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/label.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/box_layout.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/fill_layout.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/view.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace tray {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass SettingsDefaultView : public ActionableView,
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                            public PowerStatus::Observer {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit SettingsDefaultView(user::LoginStatus status)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : login_status_(status),
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        label_(NULL),
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        power_status_view_(NULL) {
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    PowerStatus::Get()->AddObserver(this);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ash::kTrayPopupPaddingHorizontal, 0,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ash::kTrayPopupPaddingBetweenItems));
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool power_view_right_align = false;
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool userAddingRunning = ash::Shell::GetInstance()
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 ->session_state_delegate()
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 ->IsInSecondaryLoginScreen();
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (login_status_ != user::LOGGED_IN_NONE &&
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        login_status_ != user::LOGGED_IN_LOCKED && !userAddingRunning) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      views::ImageView* icon =
52c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch          new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      icon->SetImage(
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          rb.GetImageNamed(IDR_AURA_UBER_TRAY_SETTINGS).ToImageSkia());
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      icon->set_id(test::kSettingsTrayItemViewId);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      AddChildView(icon);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::string16 text = rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_SETTINGS);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      label_ = new views::Label(text);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      AddChildView(label_);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SetAccessibleName(text);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      power_view_right_align = true;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (PowerStatus::Get()->IsBatteryPresent()) {
67c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      power_status_view_ = new ash::PowerStatusView(
68c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch          ash::PowerStatusView::VIEW_DEFAULT, power_view_right_align);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      AddChildView(power_status_view_);
70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      OnPowerStatusChanged();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual ~SettingsDefaultView() {
75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    PowerStatus::Get()->RemoveObserver(this);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
78c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Overridden from ash::ActionableView.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool PerformAction(const ui::Event& event) OVERRIDE {
806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool userAddingRunning = ash::Shell::GetInstance()
816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 ->session_state_delegate()
826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 ->IsInSecondaryLoginScreen();
836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (login_status_ == user::LOGGED_IN_NONE ||
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        login_status_ == user::LOGGED_IN_LOCKED || userAddingRunning)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from views::View.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Layout() OVERRIDE {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    views::View::Layout();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (label_ && power_status_view_) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Let the box-layout do the layout first. Then move power_status_view_
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // to right align if it is created.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::Size size = power_status_view_->GetPreferredSize();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::Rect bounds(size);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bounds.set_x(width() - size.width() - ash::kTrayPopupPaddingBetweenItems);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bounds.set_y((height() - size.height()) / 2);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      power_status_view_->SetBoundsRect(bounds);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from views::View.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    views::View::ChildPreferredSizeChanged(child);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Layout();
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Overridden from PowerStatus::Observer.
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnPowerStatusChanged() OVERRIDE {
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!PowerStatus::Get()->IsBatteryPresent())
116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return;
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::string16 accessible_name = label_ ?
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        label_->text() + base::ASCIIToUTF16(", ") +
1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            PowerStatus::Get()->GetAccessibleNameString(true) :
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        PowerStatus::Get()->GetAccessibleNameString(true);
122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SetAccessibleName(accessible_name);
123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  user::LoginStatus login_status_;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  views::Label* label_;
128c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ash::PowerStatusView* power_status_view_;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) };
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace tray
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TraySettings::TraySettings(SystemTray* system_tray)
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : SystemTrayItem(system_tray),
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      default_view_(NULL) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TraySettings::~TraySettings() {
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)views::View* TraySettings::CreateTrayView(user::LoginStatus status) {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)views::View* TraySettings::CreateDefaultView(user::LoginStatus status) {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((status == user::LOGGED_IN_NONE || status == user::LOGGED_IN_LOCKED) &&
149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      !PowerStatus::Get()->IsBatteryPresent())
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!ash::Shell::GetInstance()->system_tray_delegate()->ShouldShowSettings())
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return NULL;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(default_view_ == NULL);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  default_view_ =  new tray::SettingsDefaultView(status);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return default_view_;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)views::View* TraySettings::CreateDetailedView(user::LoginStatus status) {
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TraySettings::DestroyTrayView() {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TraySettings::DestroyDefaultView() {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  default_view_ = NULL;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TraySettings::DestroyDetailedView() {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ash
177