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