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/power/power_status_view.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/chromeos/power/power_status.h"
10 #include "ash/system/chromeos/power/tray_power.h"
11 #include "ash/system/tray/fixed_sized_image_view.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "grit/ash_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/l10n/time_format.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/views/controls/image_view.h"
20 #include "ui/views/controls/label.h"
21 #include "ui/views/layout/box_layout.h"
22 #include "ui/views/layout/grid_layout.h"
26 // Padding between battery status text and battery icon on default view.
27 const int kPaddingBetweenBatteryStatusAndIcon
= 3;
29 PowerStatusView::PowerStatusView(bool default_view_right_align
)
30 : default_view_right_align_(default_view_right_align
),
31 time_status_label_(new views::Label
),
32 percentage_label_(new views::Label
),
34 PowerStatus::Get()->AddObserver(this);
35 percentage_label_
->SetEnabledColor(kHeaderTextColorNormal
);
37 OnPowerStatusChanged();
40 PowerStatusView::~PowerStatusView() {
41 PowerStatus::Get()->RemoveObserver(this);
44 void PowerStatusView::OnPowerStatusChanged() {
49 PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK
));
50 icon_
->SetVisible(true);
54 void PowerStatusView::LayoutView() {
55 if (default_view_right_align_
) {
56 views::BoxLayout
* layout
=
57 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0,
58 kPaddingBetweenBatteryStatusAndIcon
);
59 SetLayoutManager(layout
);
61 AddChildView(percentage_label_
);
62 AddChildView(time_status_label_
);
64 icon_
= new views::ImageView
;
67 // PowerStatusView is left aligned on the system tray pop up item.
68 views::BoxLayout
* layout
=
69 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0,
70 kTrayPopupPaddingBetweenItems
);
71 SetLayoutManager(layout
);
73 icon_
= new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight
);
76 AddChildView(percentage_label_
);
77 AddChildView(time_status_label_
);
81 void PowerStatusView::UpdateText() {
82 const PowerStatus
& status
= *PowerStatus::Get();
83 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
84 base::string16 battery_percentage
;
85 base::string16 battery_time_status
;
87 if (status
.IsBatteryFull()) {
89 rb
.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_FULL
);
91 battery_percentage
= l10n_util::GetStringFUTF16(
92 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY
,
93 base::IntToString16(status
.GetRoundedBatteryPercent()));
94 if (status
.IsUsbChargerConnected()) {
95 battery_time_status
= rb
.GetLocalizedString(
96 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE
);
97 } else if (status
.IsBatteryTimeBeingCalculated()) {
99 rb
.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING
);
101 base::TimeDelta time
= status
.IsBatteryCharging() ?
102 status
.GetBatteryTimeToFull() : status
.GetBatteryTimeToEmpty();
103 if (PowerStatus::ShouldDisplayBatteryTime(time
) &&
104 !status
.IsBatteryDischargingOnLinePower()) {
105 int hour
= 0, min
= 0;
106 PowerStatus::SplitTimeIntoHoursAndMinutes(time
, &hour
, &min
);
107 base::string16 minute
= min
< 10 ?
108 base::ASCIIToUTF16("0") + base::IntToString16(min
) :
109 base::IntToString16(min
);
110 battery_time_status
=
111 l10n_util::GetStringFUTF16(
112 status
.IsBatteryCharging() ?
113 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_SHORT
:
114 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_SHORT
,
115 base::IntToString16(hour
),
119 battery_percentage
= battery_time_status
.empty() ?
120 battery_percentage
: battery_percentage
+ base::ASCIIToUTF16(" - ");
122 percentage_label_
->SetVisible(!battery_percentage
.empty());
123 percentage_label_
->SetText(battery_percentage
);
124 time_status_label_
->SetVisible(!battery_time_status
.empty());
125 time_status_label_
->SetText(battery_time_status
);
128 void PowerStatusView::ChildPreferredSizeChanged(views::View
* child
) {
129 PreferredSizeChanged();
132 gfx::Size
PowerStatusView::GetPreferredSize() const {
133 gfx::Size size
= views::View::GetPreferredSize();
134 return gfx::Size(size
.width(), kTrayPopupItemHeight
);
137 int PowerStatusView::GetHeightForWidth(int width
) const {
138 return kTrayPopupItemHeight
;
141 void PowerStatusView::Layout() {
142 views::View::Layout();
144 // Move the time_status_label_ closer to percentage_label_.
145 if (percentage_label_
&& time_status_label_
&&
146 percentage_label_
->visible() && time_status_label_
->visible()) {
147 time_status_label_
->SetX(percentage_label_
->bounds().right() + 1);