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