Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2975)

Unified Diff: ash/system/power/tray_power.cc

Issue 10907037: Only update system power indicators on change (Closed) Base URL: https://git.chromium.org/git/chromium/src@git-svn
Patch Set: Last nits, and some SetImage() removal:wq Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ash/system/power/tray_power.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..229d369bbaa9f6ee421bddc0495735976c979576 100644
--- a/ash/system/power/tray_power.cc
+++ b/ash/system/power/tray_power.cc
@@ -63,7 +63,7 @@ namespace tray {
// This view is used only for the tray.
class PowerTrayView : public views::ImageView {
public:
- PowerTrayView() {
+ PowerTrayView() : battery_icon_index_(-1) {
UpdateImage();
}
@@ -82,33 +82,50 @@ 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(TrayPower::GetBatteryImage(battery_icon_index_, ICON_LIGHT));
+ }
}
PowerSupplyStatus supply_status_;
+ // Index of the current icon in the icon array image, or -1 if unknown.
+ 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) {
+ 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, or -1 if unknown.
+ int battery_icon_index_;
+
DISALLOW_COPY_AND_ASSIGN(PowerNotificationView);
};
@@ -126,14 +143,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 +151,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);
}
« no previous file with comments | « ash/system/power/tray_power.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698