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/chromeos/settings/tray_settings.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"
30 class SettingsDefaultView
: public ActionableView
,
31 public PowerStatus::Observer
{
33 explicit SettingsDefaultView(user::LoginStatus status
)
34 : login_status_(status
),
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
));
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
);
49 rb
.GetImageNamed(IDR_AURA_UBER_TRAY_SETTINGS
).ToImageSkia());
50 icon
->set_id(test::kSettingsTrayItemViewId
);
53 base::string16 text
= rb
.GetLocalizedString(IDS_ASH_STATUS_TRAY_SETTINGS
);
54 label_
= new views::Label(text
);
56 SetAccessibleName(text
);
58 power_view_right_align
= true;
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();
69 virtual ~SettingsDefaultView() {
70 PowerStatus::Get()->RemoveObserver(this);
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
)
79 ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
83 // Overridden from views::View.
84 virtual void Layout() OVERRIDE
{
85 views::View::Layout();
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
);
98 // Overridden from views::View.
99 virtual void ChildPreferredSizeChanged(views::View
* child
) OVERRIDE
{
100 views::View::ChildPreferredSizeChanged(child
);
104 // Overridden from PowerStatus::Observer.
105 virtual void OnPowerStatusChanged() OVERRIDE
{
106 if (!PowerStatus::Get()->IsBatteryPresent())
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
);
117 user::LoginStatus login_status_
;
118 views::Label
* label_
;
119 ash::PowerStatusView
* power_status_view_
;
121 DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView
);
126 TraySettings::TraySettings(SystemTray
* system_tray
)
127 : SystemTrayItem(system_tray
),
128 default_view_(NULL
) {
131 TraySettings::~TraySettings() {
134 views::View
* TraySettings::CreateTrayView(user::LoginStatus status
) {
138 views::View
* TraySettings::CreateDefaultView(user::LoginStatus status
) {
139 if ((status
== user::LOGGED_IN_NONE
|| status
== user::LOGGED_IN_LOCKED
) &&
140 !PowerStatus::Get()->IsBatteryPresent())
142 if (!ash::Shell::GetInstance()->system_tray_delegate()->ShouldShowSettings())
144 CHECK(default_view_
== NULL
);
145 default_view_
= new tray::SettingsDefaultView(status
);
146 return default_view_
;
149 views::View
* TraySettings::CreateDetailedView(user::LoginStatus status
) {
154 void TraySettings::DestroyTrayView() {
157 void TraySettings::DestroyDefaultView() {
158 default_view_
= NULL
;
161 void TraySettings::DestroyDetailedView() {
164 void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status
) {