OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/status/power_menu_button.h" | 5 #include "chrome/browser/chromeos/status/power_menu_button.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 } else { | 109 } else { |
110 battery_index = static_cast<int> ( | 110 battery_index = static_cast<int> ( |
111 battery_percentage / 100.0 * | 111 battery_percentage / 100.0 * |
112 nextafter(static_cast<double>(kNumPowerImages - 1), 0)); | 112 nextafter(static_cast<double>(kNumPowerImages - 1), 0)); |
113 battery_index = | 113 battery_index = |
114 std::max(std::min(battery_index, kNumPowerImages - 2), 0); | 114 std::max(std::min(battery_index, kNumPowerImages - 2), 0); |
115 } | 115 } |
116 return GetImage(size, type, battery_index); | 116 return GetImage(size, type, battery_index); |
117 } | 117 } |
118 | 118 |
119 SkBitmap GetMissingImage(ImageSize size) { | |
120 return GetImage(size, DISCHARGING, kNumPowerImages); | |
121 } | |
122 | |
123 SkBitmap GetUnknownImage(ImageSize size) { | 119 SkBitmap GetUnknownImage(ImageSize size) { |
124 return GetImage(size, CHARGING, kNumPowerImages); | 120 return GetImage(size, CHARGING, kNumPowerImages); |
125 } | 121 } |
126 | 122 |
127 class BatteryIconView : public views::View { | 123 class BatteryIconView : public views::View { |
128 public: | 124 public: |
129 BatteryIconView() | 125 BatteryIconView() |
130 : battery_percentage_(0), | 126 : battery_percentage_(0), |
131 battery_is_present_(false), | 127 battery_is_present_(false), |
132 line_power_on_(false), | 128 line_power_on_(false), |
(...skipping 17 matching lines...) Expand all Loading... | |
150 } | 146 } |
151 | 147 |
152 void set_line_power_on(bool line_power_on) { | 148 void set_line_power_on(bool line_power_on) { |
153 line_power_on_ = line_power_on; | 149 line_power_on_ = line_power_on; |
154 SchedulePaint(); | 150 SchedulePaint(); |
155 } | 151 } |
156 | 152 |
157 protected: | 153 protected: |
158 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | 154 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
159 SkBitmap image; | 155 SkBitmap image; |
160 if (!battery_is_present_) { | 156 if (battery_is_present_) { |
161 image = GetMissingImage(LARGE); | |
162 } else { | |
163 image = GetImageWithPercentage(LARGE, | 157 image = GetImageWithPercentage(LARGE, |
164 line_power_on_ ? CHARGING : DISCHARGING, | 158 line_power_on_ ? CHARGING : DISCHARGING, |
165 battery_percentage_); | 159 battery_percentage_); |
160 } else { | |
161 NOTREACHED(); | |
162 return; | |
166 } | 163 } |
167 const int image_x = 0; | 164 const int image_x = 0; |
168 const int image_y = (height() - image.height()) / 2; | 165 const int image_y = (height() - image.height()) / 2; |
169 canvas->DrawBitmapInt(image, image_x, image_y); | 166 canvas->DrawBitmapInt(image, image_x, image_y); |
170 | 167 |
171 if (battery_is_present_ && (battery_percentage_ < 100 || !line_power_on_)) { | 168 if (battery_is_present_ && (battery_percentage_ < 100 || !line_power_on_)) { |
172 const string16 text = UTF8ToUTF16(base::StringPrintf( | 169 const string16 text = UTF8ToUTF16(base::StringPrintf( |
173 "%d%%", static_cast<int>(battery_percentage_))); | 170 "%d%%", static_cast<int>(battery_percentage_))); |
174 const int text_h = percentage_font_.GetHeight(); | 171 const int text_h = percentage_font_.GetHeight(); |
175 const int text_y = ((height() - text_h) / 2); | 172 const int text_y = ((height() - text_h) / 2); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 UpdateIconAndLabelInfo(); | 311 UpdateIconAndLabelInfo(); |
315 } | 312 } |
316 | 313 |
317 //////////////////////////////////////////////////////////////////////////////// | 314 //////////////////////////////////////////////////////////////////////////////// |
318 // PowerMenuButton, StatusAreaButton implementation: | 315 // PowerMenuButton, StatusAreaButton implementation: |
319 | 316 |
320 void PowerMenuButton::UpdateIconAndLabelInfo() { | 317 void PowerMenuButton::UpdateIconAndLabelInfo() { |
321 battery_is_present_ = power_status_.battery_is_present; | 318 battery_is_present_ = power_status_.battery_is_present; |
322 line_power_on_ = power_status_.line_power_on; | 319 line_power_on_ = power_status_.line_power_on; |
323 | 320 |
321 bool should_be_visible = battery_is_present_; | |
322 if (should_be_visible != IsVisible()) | |
323 SetVisible(should_be_visible); | |
324 | |
325 if (!should_be_visible) | |
326 return; | |
327 | |
324 // If fully charged, always show 100% even if internal number is a bit less. | 328 // If fully charged, always show 100% even if internal number is a bit less. |
325 if (power_status_.battery_is_full) | 329 if (power_status_.battery_is_full) |
326 battery_percentage_ = 100.0; | 330 battery_percentage_ = 100.0; |
327 else | 331 else |
328 battery_percentage_ = power_status_.battery_percentage; | 332 battery_percentage_ = power_status_.battery_percentage; |
329 | 333 |
330 UpdateBatteryTime(&battery_time_to_full_, | 334 UpdateBatteryTime(&battery_time_to_full_, |
331 TimeDelta::FromSeconds( | 335 TimeDelta::FromSeconds( |
332 power_status_.battery_seconds_to_full)); | 336 power_status_.battery_seconds_to_full)); |
333 UpdateBatteryTime(&battery_time_to_empty_, | 337 UpdateBatteryTime(&battery_time_to_empty_, |
334 TimeDelta::FromSeconds( | 338 TimeDelta::FromSeconds( |
335 power_status_.battery_seconds_to_empty)); | 339 power_status_.battery_seconds_to_empty)); |
336 | 340 |
337 string16 tooltip_text; | 341 SetIcon(GetImageWithPercentage( |
338 if (!battery_is_present_) { | 342 SMALL, line_power_on_ ? CHARGING : DISCHARGING, battery_percentage_)); |
339 SetIcon(GetMissingImage(SMALL)); | 343 const int message_id = line_power_on_ ? |
340 tooltip_text = l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_BATTERY); | 344 IDS_STATUSBAR_BATTERY_CHARGING_PERCENTAGE : |
DaveMoore
2011/11/16 23:35:27
Please remove the string too
| |
341 } else { | 345 IDS_STATUSBAR_BATTERY_USING_PERCENTAGE; |
342 SetIcon(GetImageWithPercentage( | 346 string16 tooltip_text = l10n_util::GetStringFUTF16( |
343 SMALL, line_power_on_ ? CHARGING : DISCHARGING, battery_percentage_)); | 347 message_id, base::IntToString16(static_cast<int>(battery_percentage_))); |
344 const int message_id = line_power_on_ ? | |
345 IDS_STATUSBAR_BATTERY_CHARGING_PERCENTAGE : | |
346 IDS_STATUSBAR_BATTERY_USING_PERCENTAGE; | |
347 tooltip_text = l10n_util::GetStringFUTF16( | |
348 message_id, base::IntToString16(static_cast<int>(battery_percentage_))); | |
349 } | |
350 SetTooltipText(tooltip_text); | 348 SetTooltipText(tooltip_text); |
351 SetAccessibleName(tooltip_text); | 349 SetAccessibleName(tooltip_text); |
352 SchedulePaint(); | 350 SchedulePaint(); |
353 UpdateStatusView(); | 351 UpdateStatusView(); |
354 } | 352 } |
355 | 353 |
356 void PowerMenuButton::UpdateStatusView() { | 354 void PowerMenuButton::UpdateStatusView() { |
357 if (status_) { | 355 if (status_) { |
358 string16 charging_text; | 356 string16 charging_text; |
359 if (battery_is_present_) { | 357 if (battery_is_present_) { |
(...skipping 20 matching lines...) Expand all Loading... | |
380 // If previous is 0, then it either was never set (initial condition) | 378 // If previous is 0, then it either was never set (initial condition) |
381 // or got down to 0. | 379 // or got down to 0. |
382 if (*previous == TimeDelta::FromMicroseconds(kInitialMS) || | 380 if (*previous == TimeDelta::FromMicroseconds(kInitialMS) || |
383 diff < kMinDiff || | 381 diff < kMinDiff || |
384 diff > kMaxDiff) { | 382 diff > kMaxDiff) { |
385 *previous = current; | 383 *previous = current; |
386 } | 384 } |
387 } | 385 } |
388 | 386 |
389 } // namespace chromeos | 387 } // namespace chromeos |
OLD | NEW |