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 ~SettingsDefaultView() override
{ PowerStatus::Get()->RemoveObserver(this); }
76 // Overridden from ash::ActionableView.
77 bool PerformAction(const ui::Event
& event
) override
{
78 bool userAddingRunning
= ash::Shell::GetInstance()
79 ->session_state_delegate()
80 ->IsInSecondaryLoginScreen();
82 if (login_status_
== user::LOGGED_IN_NONE
||
83 login_status_
== user::LOGGED_IN_LOCKED
|| userAddingRunning
)
86 ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
90 // Overridden from views::View.
91 void Layout() override
{
92 views::View::Layout();
94 if (label_
&& power_status_view_
) {
95 // Let the box-layout do the layout first. Then move power_status_view_
96 // to right align if it is created.
97 gfx::Size size
= power_status_view_
->GetPreferredSize();
98 gfx::Rect
bounds(size
);
99 bounds
.set_x(width() - size
.width() - ash::kTrayPopupPaddingBetweenItems
);
100 bounds
.set_y((height() - size
.height()) / 2);
101 power_status_view_
->SetBoundsRect(bounds
);
105 // Overridden from views::View.
106 void ChildPreferredSizeChanged(views::View
* child
) override
{
107 views::View::ChildPreferredSizeChanged(child
);
111 // Overridden from PowerStatus::Observer.
112 void OnPowerStatusChanged() override
{
113 if (!PowerStatus::Get()->IsBatteryPresent())
116 base::string16 accessible_name
= label_
?
117 label_
->text() + base::ASCIIToUTF16(", ") +
118 PowerStatus::Get()->GetAccessibleNameString(true) :
119 PowerStatus::Get()->GetAccessibleNameString(true);
120 SetAccessibleName(accessible_name
);
124 user::LoginStatus login_status_
;
125 views::Label
* label_
;
126 ash::PowerStatusView
* power_status_view_
;
128 DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView
);
133 TraySettings::TraySettings(SystemTray
* system_tray
)
134 : SystemTrayItem(system_tray
),
135 default_view_(NULL
) {
138 TraySettings::~TraySettings() {
141 views::View
* TraySettings::CreateTrayView(user::LoginStatus status
) {
145 views::View
* TraySettings::CreateDefaultView(user::LoginStatus status
) {
146 if ((status
== user::LOGGED_IN_NONE
|| status
== user::LOGGED_IN_LOCKED
) &&
147 !PowerStatus::Get()->IsBatteryPresent())
149 if (!ash::Shell::GetInstance()->system_tray_delegate()->ShouldShowSettings())
151 CHECK(default_view_
== NULL
);
152 default_view_
= new tray::SettingsDefaultView(status
);
153 return default_view_
;
156 views::View
* TraySettings::CreateDetailedView(user::LoginStatus status
) {
161 void TraySettings::DestroyTrayView() {
164 void TraySettings::DestroyDefaultView() {
165 default_view_
= NULL
;
168 void TraySettings::DestroyDetailedView() {
171 void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status
) {