tray_settings.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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/chromeos/settings/tray_settings.h"
6
7#include "ash/session/session_state_delegate.h"
8#include "ash/shell.h"
9#include "ash/system/chromeos/power/power_status.h"
10#include "ash/system/chromeos/power/power_status_view.h"
11#include "ash/system/tray/actionable_view.h"
12#include "ash/system/tray/fixed_sized_image_view.h"
13#include "ash/system/tray/system_tray_delegate.h"
14#include "ash/system/tray/tray_constants.h"
15#include "base/logging.h"
16#include "base/strings/utf_string_conversions.h"
17#include "grit/ash_resources.h"
18#include "grit/ash_strings.h"
19#include "third_party/skia/include/core/SkColor.h"
20#include "ui/base/resource/resource_bundle.h"
21#include "ui/gfx/image/image.h"
22#include "ui/views/controls/image_view.h"
23#include "ui/views/controls/label.h"
24#include "ui/views/layout/box_layout.h"
25#include "ui/views/layout/fill_layout.h"
26#include "ui/views/view.h"
27
28namespace ash {
29namespace tray {
30
31class SettingsDefaultView : public ActionableView,
32                            public PowerStatus::Observer {
33 public:
34  explicit SettingsDefaultView(user::LoginStatus status)
35      : login_status_(status),
36        label_(NULL),
37        power_status_view_(NULL) {
38    PowerStatus::Get()->AddObserver(this);
39    SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
40        ash::kTrayPopupPaddingHorizontal, 0,
41        ash::kTrayPopupPaddingBetweenItems));
42
43    bool power_view_right_align = false;
44    bool userAddingRunning = ash::Shell::GetInstance()
45                                 ->session_state_delegate()
46                                 ->IsInSecondaryLoginScreen();
47
48    if (login_status_ != user::LOGGED_IN_NONE &&
49        login_status_ != user::LOGGED_IN_LOCKED && !userAddingRunning) {
50      ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
51      views::ImageView* icon =
52          new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight);
53      icon->SetImage(
54          rb.GetImageNamed(IDR_AURA_UBER_TRAY_SETTINGS).ToImageSkia());
55      icon->set_id(test::kSettingsTrayItemViewId);
56      AddChildView(icon);
57
58      base::string16 text = rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_SETTINGS);
59      label_ = new views::Label(text);
60      AddChildView(label_);
61      SetAccessibleName(text);
62
63      power_view_right_align = true;
64    }
65
66    if (PowerStatus::Get()->IsBatteryPresent()) {
67      power_status_view_ = new ash::PowerStatusView(
68          ash::PowerStatusView::VIEW_DEFAULT, power_view_right_align);
69      AddChildView(power_status_view_);
70      OnPowerStatusChanged();
71    }
72  }
73
74  virtual ~SettingsDefaultView() {
75    PowerStatus::Get()->RemoveObserver(this);
76  }
77
78  // Overridden from ash::ActionableView.
79  virtual bool PerformAction(const ui::Event& event) OVERRIDE {
80    bool userAddingRunning = ash::Shell::GetInstance()
81                                 ->session_state_delegate()
82                                 ->IsInSecondaryLoginScreen();
83
84    if (login_status_ == user::LOGGED_IN_NONE ||
85        login_status_ == user::LOGGED_IN_LOCKED || userAddingRunning)
86      return false;
87
88    ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
89    return true;
90  }
91
92  // Overridden from views::View.
93  virtual void Layout() OVERRIDE {
94    views::View::Layout();
95
96    if (label_ && power_status_view_) {
97      // Let the box-layout do the layout first. Then move power_status_view_
98      // to right align if it is created.
99      gfx::Size size = power_status_view_->GetPreferredSize();
100      gfx::Rect bounds(size);
101      bounds.set_x(width() - size.width() - ash::kTrayPopupPaddingBetweenItems);
102      bounds.set_y((height() - size.height()) / 2);
103      power_status_view_->SetBoundsRect(bounds);
104    }
105  }
106
107  // Overridden from views::View.
108  virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE {
109    views::View::ChildPreferredSizeChanged(child);
110    Layout();
111  }
112
113  // Overridden from PowerStatus::Observer.
114  virtual void OnPowerStatusChanged() OVERRIDE {
115    if (!PowerStatus::Get()->IsBatteryPresent())
116      return;
117
118    base::string16 accessible_name = label_ ?
119        label_->text() + base::ASCIIToUTF16(", ") +
120            PowerStatus::Get()->GetAccessibleNameString(true) :
121        PowerStatus::Get()->GetAccessibleNameString(true);
122    SetAccessibleName(accessible_name);
123  }
124
125 private:
126  user::LoginStatus login_status_;
127  views::Label* label_;
128  ash::PowerStatusView* power_status_view_;
129
130  DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView);
131 };
132
133}  // namespace tray
134
135TraySettings::TraySettings(SystemTray* system_tray)
136    : SystemTrayItem(system_tray),
137      default_view_(NULL) {
138}
139
140TraySettings::~TraySettings() {
141}
142
143views::View* TraySettings::CreateTrayView(user::LoginStatus status) {
144  return NULL;
145}
146
147views::View* TraySettings::CreateDefaultView(user::LoginStatus status) {
148  if ((status == user::LOGGED_IN_NONE || status == user::LOGGED_IN_LOCKED) &&
149      !PowerStatus::Get()->IsBatteryPresent())
150    return NULL;
151  if (!ash::Shell::GetInstance()->system_tray_delegate()->ShouldShowSettings())
152    return NULL;
153  CHECK(default_view_ == NULL);
154  default_view_ =  new tray::SettingsDefaultView(status);
155  return default_view_;
156}
157
158views::View* TraySettings::CreateDetailedView(user::LoginStatus status) {
159  NOTIMPLEMENTED();
160  return NULL;
161}
162
163void TraySettings::DestroyTrayView() {
164}
165
166void TraySettings::DestroyDefaultView() {
167  default_view_ = NULL;
168}
169
170void TraySettings::DestroyDetailedView() {
171}
172
173void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status) {
174}
175
176}  // namespace ash
177