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