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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 static_cast<int>(type) * image_width, | 99 static_cast<int>(type) * image_width, |
100 index * image_height, | 100 index * image_height, |
101 image_width, | 101 image_width, |
102 image_height); | 102 image_height); |
103 | 103 |
104 SkBitmap image; | 104 SkBitmap image; |
105 all_images->extractSubset(&image, subset); | 105 all_images->extractSubset(&image, subset); |
106 return image; | 106 return image; |
107 } | 107 } |
108 | 108 |
109 SkBitmap GetMissingImage(ImageSize size) { | |
110 return GetImage(size, DISCHARGING, kNumPowerImages); | |
111 } | |
112 | |
113 SkBitmap GetUnknownImage(ImageSize size) { | 109 SkBitmap GetUnknownImage(ImageSize size) { |
114 return GetImage(size, CHARGING, kNumPowerImages); | 110 return GetImage(size, CHARGING, kNumPowerImages); |
115 } | 111 } |
116 | 112 |
117 } // namespace | 113 } // namespace |
118 | 114 |
119 namespace chromeos { | 115 namespace chromeos { |
120 | 116 |
121 using base::TimeDelta; | 117 using base::TimeDelta; |
122 | 118 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // Force a paint even if the size didn't change. | 151 // Force a paint even if the size didn't change. |
156 SchedulePaint(); | 152 SchedulePaint(); |
157 } | 153 } |
158 | 154 |
159 protected: | 155 protected: |
160 void OnPaint(gfx::Canvas* canvas) { | 156 void OnPaint(gfx::Canvas* canvas) { |
161 SkBitmap image; | 157 SkBitmap image; |
162 | 158 |
163 bool draw_percentage_text = false; | 159 bool draw_percentage_text = false; |
164 bool battery_is_present = menu_button_->battery_is_present_; | 160 bool battery_is_present = menu_button_->battery_is_present_; |
165 if (!battery_is_present) { | 161 if (battery_is_present) { |
166 image = GetMissingImage(LARGE); | |
167 } else { | |
168 image = GetImage( | 162 image = GetImage( |
169 LARGE, | 163 LARGE, |
170 menu_button_->line_power_on_ ? CHARGING : DISCHARGING, | 164 menu_button_->line_power_on_ ? CHARGING : DISCHARGING, |
171 menu_button_->battery_index_); | 165 menu_button_->battery_index_); |
172 if (menu_button_->battery_percentage_ < 100 || | 166 if (menu_button_->battery_percentage_ < 100 || |
173 !menu_button_->line_power_on_) { | 167 !menu_button_->line_power_on_) { |
174 draw_percentage_text = true; | 168 draw_percentage_text = true; |
175 } | 169 } |
| 170 } else { |
| 171 NOTREACHED(); |
| 172 return; |
176 } | 173 } |
177 int image_x = kPadLeftX, image_y = (height() - image.height()) / 2; | 174 int image_x = kPadLeftX, image_y = (height() - image.height()) / 2; |
178 canvas->DrawBitmapInt(image, image_x, image_y); | 175 canvas->DrawBitmapInt(image, image_x, image_y); |
179 | 176 |
180 if (draw_percentage_text) { | 177 if (draw_percentage_text) { |
181 string16 text = UTF8ToUTF16(base::StringPrintf( | 178 string16 text = UTF8ToUTF16(base::StringPrintf( |
182 "%d%%", | 179 "%d%%", |
183 static_cast<int>(menu_button_->battery_percentage_))); | 180 static_cast<int>(menu_button_->battery_percentage_))); |
184 int text_h = percentage_font_.GetHeight(); | 181 int text_h = percentage_font_.GetHeight(); |
185 int text_y = ((height() - text_h) / 2); | 182 int text_y = ((height() - text_h) / 2); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 353 |
357 //////////////////////////////////////////////////////////////////////////////// | 354 //////////////////////////////////////////////////////////////////////////////// |
358 // PowerMenuButton, StatusAreaButton implementation: | 355 // PowerMenuButton, StatusAreaButton implementation: |
359 | 356 |
360 void PowerMenuButton::UpdateIconAndLabelInfo() { | 357 void PowerMenuButton::UpdateIconAndLabelInfo() { |
361 PowerLibrary* power_lib = CrosLibrary::Get()->GetPowerLibrary(); | 358 PowerLibrary* power_lib = CrosLibrary::Get()->GetPowerLibrary(); |
362 | 359 |
363 battery_is_present_ = power_lib->IsBatteryPresent(); | 360 battery_is_present_ = power_lib->IsBatteryPresent(); |
364 line_power_on_ = power_lib->IsLinePowerOn(); | 361 line_power_on_ = power_lib->IsLinePowerOn(); |
365 | 362 |
| 363 bool should_be_visible = battery_is_present_; |
| 364 if (should_be_visible != IsVisible()) |
| 365 SetVisible(should_be_visible); |
| 366 |
| 367 if (!should_be_visible) { |
| 368 battery_index_ = -1; |
| 369 return; |
| 370 } |
| 371 |
366 // If fully charged, always show 100% even if internal number is a bit less. | 372 // If fully charged, always show 100% even if internal number is a bit less. |
367 if (power_lib->IsBatteryFullyCharged()) { | 373 if (power_lib->IsBatteryFullyCharged()) { |
368 // We always call power_lib->GetBatteryPercentage() for test predictability. | 374 // We always call power_lib->GetBatteryPercentage() for test predictability. |
369 power_lib->GetBatteryPercentage(); | 375 power_lib->GetBatteryPercentage(); |
370 battery_percentage_ = 100.0; | 376 battery_percentage_ = 100.0; |
371 } else { | 377 } else { |
372 battery_percentage_ = power_lib->GetBatteryPercentage(); | 378 battery_percentage_ = power_lib->GetBatteryPercentage(); |
373 } | 379 } |
374 | 380 |
375 UpdateBatteryTime(&battery_time_to_full_, power_lib->GetBatteryTimeToFull()); | 381 UpdateBatteryTime(&battery_time_to_full_, power_lib->GetBatteryTimeToFull()); |
376 UpdateBatteryTime(&battery_time_to_empty_, | 382 UpdateBatteryTime(&battery_time_to_empty_, |
377 power_lib->GetBatteryTimeToEmpty()); | 383 power_lib->GetBatteryTimeToEmpty()); |
378 | 384 |
379 string16 tooltip_text; | 385 // Preserve the fully charged icon for 100% only. |
380 if (!battery_is_present_) { | 386 if (battery_percentage_ >= 100) { |
381 battery_index_ = -1; | 387 battery_index_ = kNumPowerImages - 1; |
382 SetIcon(GetMissingImage(SMALL)); | |
383 tooltip_text = l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_BATTERY); | |
384 } else { | 388 } else { |
385 // Preserve the fully charged icon for 100% only. | 389 battery_index_ = |
386 if (battery_percentage_ >= 100) { | |
387 battery_index_ = kNumPowerImages - 1; | |
388 } else { | |
389 battery_index_ = | |
390 static_cast<int>(battery_percentage_ / 100.0 * | 390 static_cast<int>(battery_percentage_ / 100.0 * |
391 nextafter(static_cast<float>(kNumPowerImages - 1), 0)); | 391 nextafter(static_cast<float>(kNumPowerImages - 1), 0)); |
392 battery_index_ = | 392 battery_index_ = |
393 std::max(std::min(battery_index_, kNumPowerImages - 2), 0); | 393 std::max(std::min(battery_index_, kNumPowerImages - 2), 0); |
394 } | 394 } |
395 SetIcon(GetImage( | 395 SetIcon(GetImage( |
396 SMALL, line_power_on_ ? CHARGING : DISCHARGING, battery_index_)); | 396 SMALL, line_power_on_ ? CHARGING : DISCHARGING, battery_index_)); |
397 | 397 |
398 tooltip_text = l10n_util::GetStringFUTF16( | 398 string16 tooltip_text = l10n_util::GetStringFUTF16( |
399 IDS_STATUSBAR_BATTERY_PERCENTAGE, | 399 IDS_STATUSBAR_BATTERY_PERCENTAGE, |
400 base::IntToString16(static_cast<int>(battery_percentage_))); | 400 base::IntToString16(static_cast<int>(battery_percentage_))); |
401 } | |
402 SetTooltipText(tooltip_text); | 401 SetTooltipText(tooltip_text); |
403 SetAccessibleName(tooltip_text); | 402 SetAccessibleName(tooltip_text); |
404 SchedulePaint(); | 403 SchedulePaint(); |
405 if (status_) | 404 if (status_) |
406 status_->Update(); | 405 status_->Update(); |
407 } | 406 } |
408 | 407 |
409 void PowerMenuButton::UpdateBatteryTime(TimeDelta* previous, | 408 void PowerMenuButton::UpdateBatteryTime(TimeDelta* previous, |
410 const TimeDelta& current) { | 409 const TimeDelta& current) { |
411 static const TimeDelta kMaxDiff(TimeDelta::FromMinutes(10)); | 410 static const TimeDelta kMaxDiff(TimeDelta::FromMinutes(10)); |
412 static const TimeDelta kMinDiff(TimeDelta::FromMinutes(0)); | 411 static const TimeDelta kMinDiff(TimeDelta::FromMinutes(0)); |
413 const TimeDelta diff = current - *previous; | 412 const TimeDelta diff = current - *previous; |
414 // If the diff is small and positive, ignore it in favor of | 413 // If the diff is small and positive, ignore it in favor of |
415 // keeping time monotonically decreasing. | 414 // keeping time monotonically decreasing. |
416 // If previous is 0, then it either was never set (initial condition) | 415 // If previous is 0, then it either was never set (initial condition) |
417 // or got down to 0. | 416 // or got down to 0. |
418 if (*previous == TimeDelta::FromMicroseconds(kInitialMS) || | 417 if (*previous == TimeDelta::FromMicroseconds(kInitialMS) || |
419 diff < kMinDiff || | 418 diff < kMinDiff || |
420 diff > kMaxDiff) { | 419 diff > kMaxDiff) { |
421 *previous = current; | 420 *previous = current; |
422 } | 421 } |
423 } | 422 } |
424 | 423 |
425 } // namespace chromeos | 424 } // namespace chromeos |
OLD | NEW |