Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ash / system / settings / tray_settings.cc
blob2580f2446bffedbe70df7eef2caff2d8107c5675
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.
5 #include "ash/system/settings/tray_settings.h"
7 #include "ash/shell.h"
8 #include "ash/system/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/system_tray_notifier.h"
13 #include "ash/system/tray/tray_constants.h"
14 #include "base/logging.h"
15 #include "base/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"
28 namespace ash {
29 namespace internal {
31 namespace tray {
33 class 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));
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);
53 string16 text = rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_SETTINGS);
54 label_ = new views::Label(text);
55 AddChildView(label_);
56 SetAccessibleName(text);
58 power_view_right_align = true;
61 PowerSupplyStatus power_status = ash::Shell::GetInstance()->
62 system_tray_delegate()->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);
71 virtual ~SettingsDefaultView() {}
73 void UpdatePowerStatus(const PowerSupplyStatus& status) {
74 if (!power_status_view_)
75 return;
76 power_status_view_->UpdatePowerStatus(status);
77 string16 accessible_name = label_ ?
78 label_->text() + ASCIIToUTF16(", ") +
79 power_status_view_->accessible_name() :
80 power_status_view_->accessible_name();
81 SetAccessibleName(accessible_name);
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;
90 ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
91 return true;
94 // Overridden from views::View.
95 virtual void Layout() OVERRIDE {
96 views::View::Layout();
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);
109 // Overridden from views::View.
110 virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE {
111 views::View::ChildPreferredSizeChanged(child);
112 Layout();
115 private:
116 user::LoginStatus login_status_;
117 views::Label* label_;
118 ash::internal::PowerStatusView* power_status_view_;
120 DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView);
123 } // namespace tray
125 TraySettings::TraySettings(SystemTray* system_tray)
126 : SystemTrayItem(system_tray),
127 default_view_(NULL) {
128 Shell::GetInstance()->system_tray_notifier()->AddPowerStatusObserver(this);
131 TraySettings::~TraySettings() {
132 Shell::GetInstance()->system_tray_notifier()->RemovePowerStatusObserver(this);
135 views::View* TraySettings::CreateTrayView(user::LoginStatus status) {
136 return NULL;
139 views::View* TraySettings::CreateDefaultView(user::LoginStatus status) {
140 if ((status == user::LOGGED_IN_NONE || status == user::LOGGED_IN_LOCKED) &&
141 (!ash::Shell::GetInstance()->system_tray_delegate()->
142 GetPowerSupplyStatus().battery_is_present))
143 return NULL;
145 CHECK(default_view_ == NULL);
146 default_view_ = new tray::SettingsDefaultView(status);
147 return default_view_;
150 views::View* TraySettings::CreateDetailedView(user::LoginStatus status) {
151 NOTIMPLEMENTED();
152 return NULL;
155 void TraySettings::DestroyTrayView() {
158 void TraySettings::DestroyDefaultView() {
159 default_view_ = NULL;
162 void TraySettings::DestroyDetailedView() {
165 void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status) {
168 // Overridden from PowerStatusObserver.
169 void TraySettings::OnPowerStatusChanged(const PowerSupplyStatus& status) {
170 if (default_view_)
171 default_view_->UpdatePowerStatus(status);
174 } // namespace internal
175 } // namespace ash