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"
7 #include "ash/session/session_state_delegate.h"
9 #include "ash/system/chromeos/power/power_status.h"
10 #include "ash/system/chromeos/power/power_status_view.h"
11 #include "ash/system/tray/actionable_view.h"
12 #include "ash/system/tray/fixed_sized_image_view.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/system/tray/tray_constants.h"
15 #include "base/logging.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "grit/ash_resources.h"
18 #include "grit/ash_strings.h"
19 #include "third_party/skia/include/core/SkColor.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"
31 class SettingsDefaultView
: public ActionableView
,
32 public PowerStatus::Observer
{
34 explicit SettingsDefaultView(user::LoginStatus status
)
35 : login_status_(status
),
37 power_status_view_(NULL
) {
38 PowerStatus::Get()->AddObserver(this);
39 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal
,
40 ash::kTrayPopupPaddingHorizontal
, 0,
41 ash::kTrayPopupPaddingBetweenItems
));
43 bool power_view_right_align
= false;
44 bool userAddingRunning
= ash::Shell::GetInstance()
45 ->session_state_delegate()
46 ->IsInSecondaryLoginScreen();
48 if (login_status_
!= user::LOGGED_IN_NONE
&&
49 login_status_
!= user::LOGGED_IN_LOCKED
&& !userAddingRunning
) {
50 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
51 views::ImageView
* icon
=
52 new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight
);
54 rb
.GetImageNamed(IDR_AURA_UBER_TRAY_SETTINGS
).ToImageSkia());
55 icon
->set_id(test::kSettingsTrayItemViewId
);
58 base::string16 text
= rb
.GetLocalizedString(IDS_ASH_STATUS_TRAY_SETTINGS
);
59 label_
= new views::Label(text
);
61 SetAccessibleName(text
);
63 power_view_right_align
= true;
66 if (PowerStatus::Get()->IsBatteryPresent()) {
67 power_status_view_
= new ash::PowerStatusView(
68 ash::PowerStatusView::VIEW_DEFAULT
, power_view_right_align
);
69 AddChildView(power_status_view_
);
70 OnPowerStatusChanged();
74 virtual ~SettingsDefaultView() {
75 PowerStatus::Get()->RemoveObserver(this);
78 // Overridden from ash::ActionableView.
79 virtual bool PerformAction(const ui::Event
& event
) override
{
80 bool userAddingRunning
= ash::Shell::GetInstance()
81 ->session_state_delegate()
82 ->IsInSecondaryLoginScreen();
84 if (login_status_
== user::LOGGED_IN_NONE
||
85 login_status_
== user::LOGGED_IN_LOCKED
|| userAddingRunning
)
88 ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
92 // Overridden from views::View.
93 virtual void Layout() override
{
94 views::View::Layout();
96 if (label_
&& power_status_view_
) {
97 // Let the box-layout do the layout first. Then move power_status_view_
98 // to right align if it is created.
99 gfx::Size size
= power_status_view_
->GetPreferredSize();
100 gfx::Rect
bounds(size
);
101 bounds
.set_x(width() - size
.width() - ash::kTrayPopupPaddingBetweenItems
);
102 bounds
.set_y((height() - size
.height()) / 2);
103 power_status_view_
->SetBoundsRect(bounds
);
107 // Overridden from views::View.
108 virtual void ChildPreferredSizeChanged(views::View
* child
) override
{
109 views::View::ChildPreferredSizeChanged(child
);
113 // Overridden from PowerStatus::Observer.
114 virtual void OnPowerStatusChanged() override
{
115 if (!PowerStatus::Get()->IsBatteryPresent())
118 base::string16 accessible_name
= label_
?
119 label_
->text() + base::ASCIIToUTF16(", ") +
120 PowerStatus::Get()->GetAccessibleNameString(true) :
121 PowerStatus::Get()->GetAccessibleNameString(true);
122 SetAccessibleName(accessible_name
);
126 user::LoginStatus login_status_
;
127 views::Label
* label_
;
128 ash::PowerStatusView
* power_status_view_
;
130 DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView
);
135 TraySettings::TraySettings(SystemTray
* system_tray
)
136 : SystemTrayItem(system_tray
),
137 default_view_(NULL
) {
140 TraySettings::~TraySettings() {
143 views::View
* TraySettings::CreateTrayView(user::LoginStatus status
) {
147 views::View
* TraySettings::CreateDefaultView(user::LoginStatus status
) {
148 if ((status
== user::LOGGED_IN_NONE
|| status
== user::LOGGED_IN_LOCKED
) &&
149 !PowerStatus::Get()->IsBatteryPresent())
151 if (!ash::Shell::GetInstance()->system_tray_delegate()->ShouldShowSettings())
153 CHECK(default_view_
== NULL
);
154 default_view_
= new tray::SettingsDefaultView(status
);
155 return default_view_
;
158 views::View
* TraySettings::CreateDetailedView(user::LoginStatus status
) {
163 void TraySettings::DestroyTrayView() {
166 void TraySettings::DestroyDefaultView() {
167 default_view_
= NULL
;
170 void TraySettings::DestroyDetailedView() {
173 void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status
) {