tray_settings.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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_view.h"
9#include "ash/system/tray/actionable_view.h"
10#include "ash/system/tray/fixed_sized_image_view.h"
11#include "ash/system/tray/system_tray_delegate.h"
12#include "ash/system/tray/tray_constants.h"
13#include "base/logging.h"
14#include "base/strings/utf_string_conversions.h"
15#include "chromeos/dbus/power_manager_client.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:
35  explicit SettingsDefaultView(user::LoginStatus status)
36      : login_status_(status),
37        label_(NULL),
38        power_status_view_(NULL) {
39    SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
40        ash::kTrayPopupPaddingHorizontal, 0,
41        ash::kTrayPopupPaddingBetweenItems));
42
43    bool power_view_right_align = false;
44    if (login_status_ != user::LOGGED_IN_NONE &&
45        login_status_ != user::LOGGED_IN_LOCKED) {
46      ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
47      views::ImageView* icon =
48          new ash::internal::FixedSizedImageView(0, ash::kTrayPopupItemHeight);
49      icon->SetImage(
50          rb.GetImageNamed(IDR_AURA_UBER_TRAY_SETTINGS).ToImageSkia());
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    chromeos::PowerSupplyStatus power_status =
62        chromeos::PowerManagerHandler::Get()->GetPowerSupplyStatus();
63    if (power_status.battery_is_present) {
64      power_status_view_ = new ash::internal::PowerStatusView(
65          ash::internal::PowerStatusView::VIEW_DEFAULT, power_view_right_align);
66      AddChildView(power_status_view_);
67      UpdatePowerStatus(power_status);
68    }
69  }
70
71  virtual ~SettingsDefaultView() {}
72
73  void UpdatePowerStatus(const chromeos::PowerSupplyStatus& status) {
74    if (!power_status_view_)
75      return;
76    power_status_view_->UpdatePowerStatus(status);
77    base::string16 accessible_name = label_ ?
78        label_->text() + ASCIIToUTF16(", ") +
79            power_status_view_->accessible_name() :
80        power_status_view_->accessible_name();
81    SetAccessibleName(accessible_name);
82  }
83
84  // Overridden from ash::internal::ActionableView.
85  virtual bool PerformAction(const ui::Event& event) OVERRIDE {
86    if (login_status_ == user::LOGGED_IN_NONE ||
87        login_status_ == user::LOGGED_IN_LOCKED)
88      return false;
89
90    ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
91    return true;
92  }
93
94  // Overridden from views::View.
95  virtual void Layout() OVERRIDE {
96    views::View::Layout();
97
98    if (label_ && power_status_view_) {
99      // Let the box-layout do the layout first. Then move power_status_view_
100      // to right align if it is created.
101      gfx::Size size = power_status_view_->GetPreferredSize();
102      gfx::Rect bounds(size);
103      bounds.set_x(width() - size.width() - ash::kTrayPopupPaddingBetweenItems);
104      bounds.set_y((height() - size.height()) / 2);
105      power_status_view_->SetBoundsRect(bounds);
106    }
107  }
108
109  // Overridden from views::View.
110  virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE {
111    views::View::ChildPreferredSizeChanged(child);
112    Layout();
113  }
114
115 private:
116  user::LoginStatus login_status_;
117  views::Label* label_;
118  ash::internal::PowerStatusView* power_status_view_;
119
120  DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView);
121 };
122
123}  // namespace tray
124
125TraySettings::TraySettings(SystemTray* system_tray)
126    : SystemTrayItem(system_tray),
127      default_view_(NULL) {
128  chromeos::PowerManagerHandler::Get()->AddObserver(this);
129}
130
131TraySettings::~TraySettings() {
132  if (chromeos::PowerManagerHandler::IsInitialized())
133    chromeos::PowerManagerHandler::Get()->RemoveObserver(this);
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      (chromeos::PowerManagerHandler::IsInitialized() &&
143       !chromeos::PowerManagerHandler::Get()->
144           GetPowerSupplyStatus().battery_is_present))
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
170void TraySettings::OnPowerStatusChanged(
171    const chromeos::PowerSupplyStatus& status) {
172  if (default_view_)
173    default_view_->UpdatePowerStatus(status);
174}
175
176}  // namespace internal
177}  // namespace ash
178