OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "ash/system/power/power_status_view.h" | |
6 | |
7 #include "ash/system/power/tray_power.h" | |
8 #include "ash/system/tray/tray_constants.h" | |
9 #include "base/string_number_conversions.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "grit/ash_strings.h" | |
12 #include "ui/base/resource/resource_bundle.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 #include "ui/views/controls/image_view.h" | |
15 #include "ui/views/controls/label.h" | |
16 #include "ui/views/layout/box_layout.h" | |
17 #include "ui/views/layout/grid_layout.h" | |
18 | |
19 namespace ash { | |
20 namespace internal { | |
21 | |
22 namespace { | |
23 | |
24 // Top/bottom padding of the text items. | |
25 const int kPaddingVertical = 10; | |
26 // Specify min width of status label for layout. | |
27 const int kLabelMinWidth = 120; | |
28 // Padding between battery status text and battery icon on default view. | |
29 const int kPaddingBetweenBatteryStatusAndIcon = 3; | |
30 } // namespace | |
31 | |
32 PowerStatusView::PowerStatusView(ViewType view_type) | |
33 : icon_(NULL), | |
34 view_type_(view_type) { | |
sadrul
2012/06/08 13:22:39
Initialize all the labels here to NULL
jennyz
2012/06/08 16:48:03
Done.
| |
35 if (view_type == VIEW_DEFAULT) { | |
36 time_status_label_ = new views::Label; | |
37 LayoutDefaultView(); | |
38 } else { | |
39 status_label_ = new views::Label; | |
40 time_label_ = new views::Label; | |
41 LayoutNotificationView(); | |
42 } | |
43 Update(); | |
44 } | |
45 | |
46 void PowerStatusView::UpdatePowerStatus(const PowerSupplyStatus& status) { | |
47 supply_status_ = status; | |
48 // Sanitize. | |
49 if (supply_status_.battery_is_full) | |
50 supply_status_.battery_percentage = 100.0; | |
51 | |
52 Update(); | |
53 } | |
54 | |
55 void PowerStatusView::LayoutDefaultView() { | |
56 views::BoxLayout* layout = | |
57 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | |
58 kPaddingBetweenBatteryStatusAndIcon); | |
59 SetLayoutManager(layout); | |
60 | |
61 time_status_label_->SetHorizontalAlignment(views::Label::ALIGN_RIGHT); | |
62 AddChildView(time_status_label_); | |
63 | |
64 icon_ = new views::ImageView; | |
65 AddChildView(icon_); | |
66 } | |
67 | |
68 void PowerStatusView::LayoutNotificationView() { | |
69 SetLayoutManager( | |
70 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | |
71 status_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
72 AddChildView(status_label_); | |
73 | |
74 time_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
75 AddChildView(time_label_); | |
76 } | |
77 | |
78 void PowerStatusView::UpdateText() { | |
79 view_type_ == VIEW_DEFAULT ? | |
80 UpdateTextForDefaultView() : UpdateTextForNotificationView(); | |
81 } | |
82 | |
83 void PowerStatusView::UpdateTextForDefaultView() { | |
84 base::TimeDelta time = base::TimeDelta::FromSeconds( | |
85 supply_status_.averaged_battery_time_to_empty); | |
sadrul
2012/06/08 13:22:39
You cannot rely on |time| here giving you the righ
jennyz
2012/06/08 16:48:03
Done.
| |
86 int hour = time.InHours(); | |
87 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); | |
88 | |
89 if (supply_status_.line_power_on && !hour && !min) { | |
sadrul
2012/06/08 13:22:39
Is checking for .line_power_on && .battery_is_full
jennyz
2012/06/08 16:48:03
Done.
| |
90 time_status_label_->SetText( | |
91 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
92 IDS_ASH_STATUS_TRAY_BATTERY_FULL)); | |
93 } else { | |
94 string16 battery_percentage = l10n_util::GetStringFUTF16( | |
95 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY, | |
96 base::IntToString16( | |
97 static_cast<int>(supply_status_.battery_percentage))); | |
98 string16 battery_time = string16(); | |
99 if (supply_status_.is_calculating_battery_time) { | |
100 battery_time = | |
101 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
102 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING); | |
103 } else if (hour || min){ | |
104 battery_time = | |
105 l10n_util::GetStringFUTF16( | |
106 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ONLY, | |
107 base::IntToString16(hour), | |
108 base::IntToString16(min)) + | |
109 ASCIIToUTF16(" - "); | |
110 } | |
111 string16 battery_status = battery_time + battery_percentage; | |
112 time_status_label_->SetText(battery_status); | |
113 } | |
114 } | |
115 | |
116 void PowerStatusView::UpdateTextForNotificationView() { | |
117 base::TimeDelta time = base::TimeDelta::FromSeconds( | |
118 supply_status_.line_power_on ? | |
119 supply_status_.averaged_battery_time_to_full : | |
120 supply_status_.averaged_battery_time_to_empty); | |
121 int hour = time.InHours(); | |
122 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); | |
123 | |
124 if (supply_status_.line_power_on && !hour && !min) { | |
125 status_label_->SetText( | |
126 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
127 IDS_ASH_STATUS_TRAY_BATTERY_FULL)); | |
128 } else { | |
129 status_label_->SetText( | |
130 l10n_util::GetStringFUTF16( | |
131 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT, | |
132 base::IntToString16( | |
133 static_cast<int>(supply_status_.battery_percentage)))); | |
134 } | |
135 | |
136 if (supply_status_.is_calculating_battery_time) { | |
137 time_label_->SetText( | |
138 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
139 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING)); | |
140 } else if (hour || min) { | |
141 time_label_->SetText( | |
142 l10n_util::GetStringFUTF16( | |
143 supply_status_.line_power_on ? | |
144 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL : | |
145 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_EMPTY, | |
146 base::IntToString16(hour), | |
147 base::IntToString16(min))); | |
148 } else { | |
149 time_label_->SetText(string16()); | |
150 } | |
151 | |
152 } | |
153 | |
154 void PowerStatusView::UpdateIcon() { | |
155 if (icon_) { | |
156 icon_->SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_DARK)); | |
157 icon_->SetVisible(true); | |
158 } | |
159 } | |
160 | |
161 void PowerStatusView::Update() { | |
162 UpdateText(); | |
163 UpdateIcon(); | |
164 } | |
165 | |
166 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { | |
167 PreferredSizeChanged(); | |
168 } | |
169 | |
170 gfx::Size PowerStatusView::GetPreferredSize() { | |
171 gfx::Size size = views::View::GetPreferredSize(); | |
172 return gfx::Size(size.width(), kTrayPopupItemHeight); | |
173 } | |
174 | |
175 } // namespace internal | |
176 } // namespace ash | |
OLD | NEW |