OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/system/power/tray_power.h" | 5 #include "ash/system/power/tray_power.h" |
6 | 6 |
7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/system/date/date_view.h" | 9 #include "ash/system/date/date_view.h" |
| 10 #include "ash/system/power/power_status_view.h" |
10 #include "ash/system/power/power_supply_status.h" | 11 #include "ash/system/power/power_supply_status.h" |
11 #include "ash/system/tray/system_tray_delegate.h" | 12 #include "ash/system/tray/system_tray_delegate.h" |
12 #include "ash/system/tray/tray_constants.h" | 13 #include "ash/system/tray/tray_constants.h" |
13 #include "ash/system/tray/tray_views.h" | 14 #include "ash/system/tray/tray_views.h" |
14 #include "base/command_line.h" | 15 #include "base/command_line.h" |
15 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
18 #include "grit/ash_strings.h" | 19 #include "grit/ash_strings.h" |
19 #include "grit/ui_resources_standard.h" | 20 #include "grit/ui_resources_standard.h" |
(...skipping 26 matching lines...) Expand all Loading... |
46 const int kNumPowerImages = 15; | 47 const int kNumPowerImages = 15; |
47 // Top/bottom padding of the text items. | 48 // Top/bottom padding of the text items. |
48 const int kPaddingVertical = 10; | 49 const int kPaddingVertical = 10; |
49 // Specify min width of status label for layout. | 50 // Specify min width of status label for layout. |
50 const int kLabelMinWidth = 120; | 51 const int kLabelMinWidth = 120; |
51 // Notification times. | 52 // Notification times. |
52 const int kCriticalSeconds = 5 * 60; | 53 const int kCriticalSeconds = 5 * 60; |
53 const int kLowPowerSeconds = 15 * 60; | 54 const int kLowPowerSeconds = 15 * 60; |
54 const int kNoWarningSeconds = 30 * 60; | 55 const int kNoWarningSeconds = 30 * 60; |
55 | 56 |
56 enum IconSet { | 57 } // namespace |
57 ICON_LIGHT, | |
58 ICON_DARK | |
59 }; | |
60 | 58 |
61 gfx::ImageSkia GetBatteryImage(const PowerSupplyStatus& supply_status, | 59 gfx::ImageSkia GetBatteryImage(const PowerSupplyStatus& supply_status, |
62 IconSet icon_set) { | 60 IconSet icon_set) { |
63 gfx::ImageSkia image; | 61 gfx::ImageSkia image; |
64 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 62 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
65 icon_set == ICON_DARK ? | 63 icon_set == ICON_DARK ? |
66 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); | 64 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); |
67 | 65 |
68 int image_index = 0; | 66 int image_index = 0; |
69 if (supply_status.battery_percentage >= 100) { | 67 if (supply_status.battery_percentage >= 100) { |
(...skipping 10 matching lines...) Expand all Loading... |
80 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See | 78 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See |
81 // crbug.com/119832. | 79 // crbug.com/119832. |
82 SkIRect region = SkIRect::MakeXYWH( | 80 SkIRect region = SkIRect::MakeXYWH( |
83 (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, | 81 (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, |
84 image_index * kBatteryImageHeight, | 82 image_index * kBatteryImageHeight, |
85 kBatteryImageWidth - 2, kBatteryImageHeight); | 83 kBatteryImageWidth - 2, kBatteryImageHeight); |
86 all.ToImageSkia()->extractSubset(&image, region); | 84 all.ToImageSkia()->extractSubset(&image, region); |
87 return image; | 85 return image; |
88 } | 86 } |
89 | 87 |
90 } // namespace | |
91 | |
92 namespace tray { | 88 namespace tray { |
93 | 89 |
94 // This view is used only for the tray. | 90 // This view is used only for the tray. |
95 class PowerTrayView : public views::ImageView { | 91 class PowerTrayView : public views::ImageView { |
96 public: | 92 public: |
97 PowerTrayView() { | 93 PowerTrayView() { |
98 UpdateImage(); | 94 UpdateImage(); |
99 } | 95 } |
100 | 96 |
101 virtual ~PowerTrayView() { | 97 virtual ~PowerTrayView() { |
(...skipping 12 matching lines...) Expand all Loading... |
114 private: | 110 private: |
115 void UpdateImage() { | 111 void UpdateImage() { |
116 SetImage(GetBatteryImage(supply_status_, ICON_LIGHT)); | 112 SetImage(GetBatteryImage(supply_status_, ICON_LIGHT)); |
117 } | 113 } |
118 | 114 |
119 PowerSupplyStatus supply_status_; | 115 PowerSupplyStatus supply_status_; |
120 | 116 |
121 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); | 117 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
122 }; | 118 }; |
123 | 119 |
124 // This view is used only for the popup. | |
125 class PowerStatusView : public views::View { | |
126 public: | |
127 enum ViewType { | |
128 VIEW_DEFAULT, | |
129 VIEW_NOTIFICATION | |
130 }; | |
131 | |
132 explicit PowerStatusView(ViewType view_type) : icon_(NULL) { | |
133 status_label_ = new views::Label; | |
134 time_label_ = new views::Label; | |
135 | |
136 if (view_type == VIEW_DEFAULT) | |
137 LayoutDefaultView(); | |
138 else | |
139 LayoutNotificationView(); | |
140 | |
141 Update(); | |
142 } | |
143 | |
144 virtual ~PowerStatusView() { | |
145 } | |
146 | |
147 void UpdatePowerStatus(const PowerSupplyStatus& status) { | |
148 supply_status_ = status; | |
149 // Sanitize. | |
150 if (supply_status_.battery_is_full) | |
151 supply_status_.battery_percentage = 100.0; | |
152 | |
153 Update(); | |
154 } | |
155 | |
156 private: | |
157 void LayoutDefaultView() { | |
158 set_border(views::Border::CreateEmptyBorder( | |
159 kPaddingVertical, kTrayPopupPaddingHorizontal, | |
160 kPaddingVertical, kTrayPopupPaddingHorizontal)); | |
161 | |
162 status_label_->SetHorizontalAlignment(views::Label::ALIGN_RIGHT); | |
163 time_label_->SetHorizontalAlignment(views::Label::ALIGN_RIGHT); | |
164 | |
165 icon_ = new views::ImageView; | |
166 | |
167 views::GridLayout* layout = new views::GridLayout(this); | |
168 SetLayoutManager(layout); | |
169 | |
170 views::ColumnSet* columns = layout->AddColumnSet(0); | |
171 | |
172 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal); | |
173 // Status + Time | |
174 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
175 0, views::GridLayout::USE_PREF, 0, kLabelMinWidth); | |
176 | |
177 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal/2); | |
178 | |
179 // Icon | |
180 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
181 0, views::GridLayout::USE_PREF, 0, 0); | |
182 | |
183 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal); | |
184 | |
185 layout->AddPaddingRow(0, kPaddingVertical); | |
186 | |
187 layout->StartRow(0, 0); | |
188 layout->AddView(status_label_); | |
189 layout->AddView(icon_, 1, 3); // 3 rows for icon | |
190 | |
191 layout->AddPaddingRow(0, kPaddingVertical/3); | |
192 layout->StartRow(0, 0); | |
193 layout->AddView(time_label_); | |
194 | |
195 layout->AddPaddingRow(0, kPaddingVertical); | |
196 } | |
197 | |
198 void LayoutNotificationView() { | |
199 SetLayoutManager( | |
200 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | |
201 status_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
202 AddChildView(status_label_); | |
203 | |
204 time_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
205 AddChildView(time_label_); | |
206 } | |
207 | |
208 void UpdateText() { | |
209 base::TimeDelta time = base::TimeDelta::FromSeconds( | |
210 supply_status_.line_power_on ? | |
211 supply_status_.averaged_battery_time_to_full : | |
212 supply_status_.averaged_battery_time_to_empty); | |
213 int hour = time.InHours(); | |
214 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); | |
215 | |
216 if (supply_status_.line_power_on && !hour && !min) { | |
217 status_label_->SetText( | |
218 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
219 IDS_ASH_STATUS_TRAY_BATTERY_FULL)); | |
220 } else { | |
221 status_label_->SetText( | |
222 l10n_util::GetStringFUTF16( | |
223 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT, | |
224 base::IntToString16( | |
225 static_cast<int>(supply_status_.battery_percentage)))); | |
226 } | |
227 | |
228 if (supply_status_.is_calculating_battery_time) { | |
229 time_label_->SetText( | |
230 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
231 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING)); | |
232 } else if (hour || min) { | |
233 time_label_->SetText( | |
234 l10n_util::GetStringFUTF16( | |
235 supply_status_.line_power_on ? | |
236 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL : | |
237 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_EMPTY, | |
238 base::IntToString16(hour), | |
239 base::IntToString16(min))); | |
240 } else { | |
241 time_label_->SetText(string16()); | |
242 } | |
243 } | |
244 | |
245 void UpdateIcon() { | |
246 if (icon_) { | |
247 icon_->SetImage(GetBatteryImage(supply_status_, ICON_DARK)); | |
248 icon_->SetVisible(true); | |
249 } | |
250 } | |
251 | |
252 void Update() { | |
253 UpdateText(); | |
254 UpdateIcon(); | |
255 } | |
256 | |
257 views::Label* status_label_; | |
258 views::Label* time_label_; | |
259 views::ImageView* icon_; | |
260 | |
261 PowerSupplyStatus supply_status_; | |
262 | |
263 DISALLOW_COPY_AND_ASSIGN(PowerStatusView); | |
264 }; | |
265 | |
266 class PowerNotificationView : public TrayNotificationView { | 120 class PowerNotificationView : public TrayNotificationView { |
267 public: | 121 public: |
268 explicit PowerNotificationView(TrayPower* tray) | 122 explicit PowerNotificationView(TrayPower* tray) |
269 : TrayNotificationView(0), | 123 : TrayNotificationView(0), |
270 tray_(tray) { | 124 tray_(tray) { |
271 power_status_view_ = | 125 power_status_view_ = |
272 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION); | 126 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION); |
273 InitView(power_status_view_); | 127 InitView(power_status_view_); |
274 } | 128 } |
275 | 129 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 return false; | 249 return false; |
396 case NOTIFICATION_CRITICAL: | 250 case NOTIFICATION_CRITICAL: |
397 return false; | 251 return false; |
398 } | 252 } |
399 NOTREACHED(); | 253 NOTREACHED(); |
400 return false; | 254 return false; |
401 } | 255 } |
402 | 256 |
403 } // namespace internal | 257 } // namespace internal |
404 } // namespace ash | 258 } // namespace ash |
OLD | NEW |