Index: chrome/browser/chromeos/status/power_menu_button.cc |
diff --git a/chrome/browser/chromeos/status/power_menu_button.cc b/chrome/browser/chromeos/status/power_menu_button.cc |
index 98fb75e06ed687a5c9bda55b1ee134c2f1762750..98cda08ab8a3476aad39939b1f0562b315e587de 100644 |
--- a/chrome/browser/chromeos/status/power_menu_button.cc |
+++ b/chrome/browser/chromeos/status/power_menu_button.cc |
@@ -25,8 +25,12 @@ const int PowerMenuButton::kNumPowerImages = 12; |
PowerMenuButton::PowerMenuButton() |
: StatusAreaButton(this), |
ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)), |
+ battery_is_present_(false), |
+ line_power_on_(false), |
+ battery_fully_charged_(false), |
+ battery_percentage_(0.0), |
icon_id_(-1) { |
- UpdateIcon(); |
+ UpdateIconAndLabelInfo(); |
CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
} |
@@ -46,28 +50,24 @@ menus::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { |
} |
string16 PowerMenuButton::GetLabelAt(int index) const { |
- PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
// The first item shows the percentage of battery left. |
if (index == 0) { |
- // If fully charged, always show 100% even if internal number is a bit less. |
- double percent = cros->battery_fully_charged() ? 100 : |
- cros->battery_percentage(); |
return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE, |
- base::IntToString16(static_cast<int>(percent))); |
+ base::IntToString16(static_cast<int>(battery_percentage_))); |
} |
// The second item shows the battery is charged if it is. |
- if (cros->battery_fully_charged()) |
+ if (battery_fully_charged_) |
return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED); |
// If battery is in an intermediate charge state, we show how much time left. |
- base::TimeDelta time = cros->line_power_on() ? cros->battery_time_to_full() : |
- cros->battery_time_to_empty(); |
+ base::TimeDelta time = line_power_on_ ? battery_time_to_full_ : |
+ battery_time_to_empty_; |
if (time.InSeconds() == 0) { |
// If time is 0, then that means we are still calculating how much time. |
// Depending if line power is on, we either show a message saying that we |
// are calculating time until full or calculating remaining time. |
- int msg = cros->line_power_on() ? |
+ int msg = line_power_on_ ? |
IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL : |
IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY; |
return l10n_util::GetStringUTF16(msg); |
@@ -75,8 +75,8 @@ string16 PowerMenuButton::GetLabelAt(int index) const { |
// Depending if line power is on, we either show a message saying XX:YY |
// until full or XX:YY remaining where XX is number of hours and YY is |
// number of minutes. |
- int msg = cros->line_power_on() ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL : |
- IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY; |
+ int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL : |
+ IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY; |
int hour = time.InHours(); |
int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); |
string16 hour_str = base::IntToString16(hour); |
@@ -101,7 +101,7 @@ void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
// PowerMenuButton, PowerLibrary::Observer implementation: |
void PowerMenuButton::PowerChanged(PowerLibrary* obj) { |
- UpdateIcon(); |
+ UpdateIconAndLabelInfo(); |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -122,59 +122,74 @@ void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { |
canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding); |
} |
-void PowerMenuButton::UpdateIcon() { |
+void PowerMenuButton::UpdateIconAndLabelInfo() { |
PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
- icon_id_ = IDR_STATUSBAR_BATTERY_UNKNOWN; |
- if (CrosLibrary::Get()->EnsureLoaded()) { |
- if (!cros->battery_is_present()) { |
- icon_id_ = IDR_STATUSBAR_BATTERY_MISSING; |
- } else if (cros->line_power_on() && cros->battery_fully_charged()) { |
- icon_id_ = IDR_STATUSBAR_BATTERY_CHARGED; |
- } else { |
- // Get the power image depending on battery percentage. Percentage is |
- // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1. |
- // NOTE: Use an array rather than just calculating a resource number to |
- // avoid creating implicit ordering dependencies on the resource values. |
- static const int kChargingImages[kNumPowerImages] = { |
- IDR_STATUSBAR_BATTERY_CHARGING_1, |
- IDR_STATUSBAR_BATTERY_CHARGING_2, |
- IDR_STATUSBAR_BATTERY_CHARGING_3, |
- IDR_STATUSBAR_BATTERY_CHARGING_4, |
- IDR_STATUSBAR_BATTERY_CHARGING_5, |
- IDR_STATUSBAR_BATTERY_CHARGING_6, |
- IDR_STATUSBAR_BATTERY_CHARGING_7, |
- IDR_STATUSBAR_BATTERY_CHARGING_8, |
- IDR_STATUSBAR_BATTERY_CHARGING_9, |
- IDR_STATUSBAR_BATTERY_CHARGING_10, |
- IDR_STATUSBAR_BATTERY_CHARGING_11, |
- IDR_STATUSBAR_BATTERY_CHARGING_12, |
- }; |
- static const int kDischargingImages[kNumPowerImages] = { |
- IDR_STATUSBAR_BATTERY_DISCHARGING_1, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_2, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_3, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_4, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_5, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_6, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_7, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_8, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_9, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_10, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_11, |
- IDR_STATUSBAR_BATTERY_DISCHARGING_12, |
- }; |
- |
- // If fully charged, always show 100% even if percentage is a bit less. |
- double percent = cros->battery_fully_charged() ? |
- 100 : cros->battery_percentage(); |
- int index = static_cast<int>(percent / 100.0 * |
- nextafter(static_cast<float>(kNumPowerImages), 0)); |
- index = std::max(std::min(index, kNumPowerImages - 1), 0); |
- icon_id_ = cros->line_power_on() ? |
- kChargingImages[index] : kDischargingImages[index]; |
- } |
+ if (!cros) |
+ return; |
+ |
+ bool cros_loaded = CrosLibrary::Get()->EnsureLoaded(); |
+ if (cros_loaded) { |
+ battery_is_present_ = cros->battery_is_present(); |
+ line_power_on_ = cros->line_power_on(); |
+ battery_fully_charged_ = cros->battery_fully_charged(); |
+ battery_percentage_ = cros->battery_percentage(); |
+ // If fully charged, always show 100% even if internal number is a bit less. |
+ // Note: we always call cros->battery_percentage() for test predictability. |
+ if (battery_fully_charged_) |
+ battery_percentage_ = 100.0; |
+ battery_time_to_full_ = cros->battery_time_to_full(); |
+ battery_time_to_empty_ = cros->battery_time_to_empty(); |
} |
+ |
+ if (!cros_loaded) { |
+ icon_id_ = IDR_STATUSBAR_BATTERY_UNKNOWN; |
+ } else if (!battery_is_present_) { |
+ icon_id_ = IDR_STATUSBAR_BATTERY_MISSING; |
+ } else if (line_power_on_ && battery_fully_charged_) { |
+ icon_id_ = IDR_STATUSBAR_BATTERY_CHARGED; |
+ } else { |
+ // Get the power image depending on battery percentage. Percentage is |
+ // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1. |
+ // NOTE: Use an array rather than just calculating a resource number to |
+ // avoid creating implicit ordering dependencies on the resource values. |
+ static const int kChargingImages[kNumPowerImages] = { |
+ IDR_STATUSBAR_BATTERY_CHARGING_1, |
+ IDR_STATUSBAR_BATTERY_CHARGING_2, |
+ IDR_STATUSBAR_BATTERY_CHARGING_3, |
+ IDR_STATUSBAR_BATTERY_CHARGING_4, |
+ IDR_STATUSBAR_BATTERY_CHARGING_5, |
+ IDR_STATUSBAR_BATTERY_CHARGING_6, |
+ IDR_STATUSBAR_BATTERY_CHARGING_7, |
+ IDR_STATUSBAR_BATTERY_CHARGING_8, |
+ IDR_STATUSBAR_BATTERY_CHARGING_9, |
+ IDR_STATUSBAR_BATTERY_CHARGING_10, |
+ IDR_STATUSBAR_BATTERY_CHARGING_11, |
+ IDR_STATUSBAR_BATTERY_CHARGING_12, |
+ }; |
+ static const int kDischargingImages[kNumPowerImages] = { |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_1, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_2, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_3, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_4, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_5, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_6, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_7, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_8, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_9, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_10, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_11, |
+ IDR_STATUSBAR_BATTERY_DISCHARGING_12, |
+ }; |
+ |
+ int index = static_cast<int>(battery_percentage_ / 100.0 * |
+ nextafter(static_cast<float>(kNumPowerImages), 0)); |
+ index = std::max(std::min(index, kNumPowerImages - 1), 0); |
+ icon_id_ = line_power_on_ ? |
+ kChargingImages[index] : kDischargingImages[index]; |
+ } |
+ |
SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); |
+ SetTooltipText(UTF16ToWide(GetLabelAt(0))); |
SchedulePaint(); |
} |