Chromium Code Reviews| Index: ash/system/power/tray_power.cc |
| diff --git a/ash/system/power/tray_power.cc b/ash/system/power/tray_power.cc |
| index b2a7aa1d9a78f539d5f5282e7d698d692a0eec26..57c91cca4ae0ce9f255fe9d58a18fed89c9bec4c 100644 |
| --- a/ash/system/power/tray_power.cc |
| +++ b/ash/system/power/tray_power.cc |
| @@ -63,7 +63,8 @@ namespace tray { |
| // This view is used only for the tray. |
| class PowerTrayView : public views::ImageView { |
| public: |
| - PowerTrayView() { |
| + PowerTrayView() |
| + : battery_icon_index_(-1) { |
|
Daniel Erat
2012/09/11 22:17:09
nit: move to end of previous line
|
| UpdateImage(); |
| } |
| @@ -82,33 +83,53 @@ class PowerTrayView : public views::ImageView { |
| private: |
| void UpdateImage() { |
| - SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_LIGHT, |
| - GetImage())); |
| + int index = TrayPower::GetBatteryImageIndex(supply_status_); |
| + if (battery_icon_index_ != index) { |
| + battery_icon_index_ = index; |
| + if (battery_icon_index_ == -1) |
| + SetImage(GetImage()); |
|
Daniel Erat
2012/09/11 22:17:09
don't understand this either
|
| + else |
| + SetImage(TrayPower::GetBatteryImage(battery_icon_index_, ICON_LIGHT)); |
| + } |
| } |
| PowerSupplyStatus supply_status_; |
| + // index of the current icon in the icon array image |
|
Daniel Erat
2012/09/11 22:17:09
nit: capitalize, add period, and mention what -1 m
|
| + int battery_icon_index_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| }; |
| class PowerNotificationView : public TrayNotificationView { |
| public: |
| explicit PowerNotificationView(TrayPower* tray) |
| - : TrayNotificationView(tray, 0) { |
| + : TrayNotificationView(tray, 0), |
| + battery_icon_index_(-1) { |
| power_status_view_ = |
| new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); |
| InitView(power_status_view_); |
| } |
| void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| - SetIconImage(TrayPower::GetBatteryImage(status, ICON_DARK, |
| - GetIconImage())); |
| + int index = TrayPower::GetBatteryImageIndex(status); |
| + if (battery_icon_index_ != index) { |
| + battery_icon_index_ = index; |
| + if (battery_icon_index_ == -1) |
|
Daniel Erat
2012/09/11 22:17:09
nit: add curly brackets
|
| + SetIconImage(GetIconImage()); |
|
Daniel Erat
2012/09/11 22:17:09
same as above
|
| + else |
| + SetIconImage(TrayPower::GetBatteryImage(battery_icon_index_, |
| + ICON_DARK)); |
| + } |
| power_status_view_->UpdatePowerStatus(status); |
| } |
| private: |
| PowerStatusView* power_status_view_; |
| + // index of the current icon in the icon array image |
|
Daniel Erat
2012/09/11 22:17:09
nit: capitalize and period
|
| + int battery_icon_index_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); |
| }; |
| @@ -126,14 +147,7 @@ TrayPower::~TrayPower() { |
| } |
| // static |
| -gfx::ImageSkia TrayPower::GetBatteryImage( |
| - const PowerSupplyStatus& supply_status, |
| - IconSet icon_set, |
| - const gfx::ImageSkia& default_image) { |
| - gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| - icon_set == ICON_DARK ? |
| - IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); |
| - |
| +int TrayPower::GetBatteryImageIndex(const PowerSupplyStatus& supply_status) { |
| int image_index = 0; |
| if (supply_status.battery_percentage >= 100) { |
| image_index = kNumPowerImages - 1; |
| @@ -141,19 +155,27 @@ gfx::ImageSkia TrayPower::GetBatteryImage( |
| image_index = kNumPowerImages; |
| } else { |
| // If power supply is calculating battery time, the battery percentage |
| - // is uncertain, just return |default_image|. |
| + // is uncertain, just return -1. |
| if (supply_status.is_calculating_battery_time) |
| - return default_image; |
| + return -1; |
| image_index = static_cast<int>(supply_status.battery_percentage / |
| 100.0 * (kNumPowerImages - 1)); |
| image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); |
| } |
| + return (image_index << 1) | (supply_status.line_power_on ? 1 : 0); |
| +} |
| + |
| +// static |
| +gfx::ImageSkia TrayPower::GetBatteryImage(int image_index, IconSet icon_set) { |
| + gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| + icon_set == ICON_DARK ? |
| + IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); |
| // TODO(mbolohan): Remove the 2px offset when the assets are centered. See |
| // crbug.com/119832. |
| gfx::Rect region( |
| - (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, |
| - image_index * kBatteryImageHeight, |
| + ((image_index & 0x1) ? kBatteryImageWidth : 0) + 2, |
| + (image_index >> 1) * kBatteryImageHeight, |
| kBatteryImageWidth - 2, kBatteryImageHeight); |
| return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); |
| } |