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

Side by Side 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: Nit fixes.: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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ash/system/power/tray_power.h" 5 #include "ash/system/power/tray_power.h"
6 6
7 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "ash/shell.h" 8 #include "ash/shell.h"
9 #include "ash/system/date/date_view.h" 9 #include "ash/system/date/date_view.h"
10 #include "ash/system/power/power_status_view.h" 10 #include "ash/system/power/power_status_view.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 const int kLowPowerSeconds = 15 * 60; 56 const int kLowPowerSeconds = 15 * 60;
57 const int kNoWarningSeconds = 30 * 60; 57 const int kNoWarningSeconds = 30 * 60;
58 58
59 } // namespace 59 } // namespace
60 60
61 namespace tray { 61 namespace tray {
62 62
63 // This view is used only for the tray. 63 // This view is used only for the tray.
64 class PowerTrayView : public views::ImageView { 64 class PowerTrayView : public views::ImageView {
65 public: 65 public:
66 PowerTrayView() { 66 PowerTrayView() : battery_icon_index_(-1) {
67 UpdateImage(); 67 UpdateImage();
68 } 68 }
69 69
70 virtual ~PowerTrayView() { 70 virtual ~PowerTrayView() {
71 } 71 }
72 72
73 void UpdatePowerStatus(const PowerSupplyStatus& status) { 73 void UpdatePowerStatus(const PowerSupplyStatus& status) {
74 supply_status_ = status; 74 supply_status_ = status;
75 // Sanitize. 75 // Sanitize.
76 if (supply_status_.battery_is_full) 76 if (supply_status_.battery_is_full)
77 supply_status_.battery_percentage = 100.0; 77 supply_status_.battery_percentage = 100.0;
78 78
79 UpdateImage(); 79 UpdateImage();
80 SetVisible(status.battery_is_present); 80 SetVisible(status.battery_is_present);
81 } 81 }
82 82
83 private: 83 private:
84 void UpdateImage() { 84 void UpdateImage() {
85 SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_LIGHT, 85 int index = TrayPower::GetBatteryImageIndex(supply_status_);
86 GetImage())); 86 if (battery_icon_index_ != index) {
87 battery_icon_index_ = index;
88 if (battery_icon_index_ == -1)
89 SetImage(GetImage());
90 else
91 SetImage(TrayPower::GetBatteryImage(battery_icon_index_, ICON_LIGHT));
92 }
87 } 93 }
88 94
89 PowerSupplyStatus supply_status_; 95 PowerSupplyStatus supply_status_;
90 96
97 // Index of the current icon in the icon array image, or -1 if unknown.
98 int battery_icon_index_;
99
91 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); 100 DISALLOW_COPY_AND_ASSIGN(PowerTrayView);
92 }; 101 };
93 102
94 class PowerNotificationView : public TrayNotificationView { 103 class PowerNotificationView : public TrayNotificationView {
95 public: 104 public:
96 explicit PowerNotificationView(TrayPower* tray) 105 explicit PowerNotificationView(TrayPower* tray)
97 : TrayNotificationView(tray, 0) { 106 : TrayNotificationView(tray, 0),
107 battery_icon_index_(-1) {
98 power_status_view_ = 108 power_status_view_ =
99 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); 109 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true);
100 InitView(power_status_view_); 110 InitView(power_status_view_);
101 } 111 }
102 112
103 void UpdatePowerStatus(const PowerSupplyStatus& status) { 113 void UpdatePowerStatus(const PowerSupplyStatus& status) {
104 SetIconImage(TrayPower::GetBatteryImage(status, ICON_DARK, 114 int index = TrayPower::GetBatteryImageIndex(status);
105 GetIconImage())); 115 if (battery_icon_index_ != index) {
116 battery_icon_index_ = index;
117 if (battery_icon_index_ == -1) {
118 SetIconImage(GetIconImage());
119 } else {
120 SetIconImage(TrayPower::GetBatteryImage(battery_icon_index_,
121 ICON_DARK));
122 }
123 }
106 power_status_view_->UpdatePowerStatus(status); 124 power_status_view_->UpdatePowerStatus(status);
107 } 125 }
108 126
109 private: 127 private:
110 PowerStatusView* power_status_view_; 128 PowerStatusView* power_status_view_;
111 129
130 // Index of the current icon in the icon array image, or -1 if unknown.
131 int battery_icon_index_;
132
112 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); 133 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView);
113 }; 134 };
114 135
115 } // namespace tray 136 } // namespace tray
116 137
117 using tray::PowerNotificationView; 138 using tray::PowerNotificationView;
118 139
119 TrayPower::TrayPower() 140 TrayPower::TrayPower()
120 : power_tray_(NULL), 141 : power_tray_(NULL),
121 notification_view_(NULL), 142 notification_view_(NULL),
122 notification_state_(NOTIFICATION_NONE) { 143 notification_state_(NOTIFICATION_NONE) {
123 } 144 }
124 145
125 TrayPower::~TrayPower() { 146 TrayPower::~TrayPower() {
126 } 147 }
127 148
128 // static 149 // static
129 gfx::ImageSkia TrayPower::GetBatteryImage( 150 int TrayPower::GetBatteryImageIndex(const PowerSupplyStatus& supply_status) {
130 const PowerSupplyStatus& supply_status,
131 IconSet icon_set,
132 const gfx::ImageSkia& default_image) {
133 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
134 icon_set == ICON_DARK ?
135 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
136
137 int image_index = 0; 151 int image_index = 0;
138 if (supply_status.battery_percentage >= 100) { 152 if (supply_status.battery_percentage >= 100) {
139 image_index = kNumPowerImages - 1; 153 image_index = kNumPowerImages - 1;
140 } else if (!supply_status.battery_is_present) { 154 } else if (!supply_status.battery_is_present) {
141 image_index = kNumPowerImages; 155 image_index = kNumPowerImages;
142 } else { 156 } else {
143 // If power supply is calculating battery time, the battery percentage 157 // If power supply is calculating battery time, the battery percentage
144 // is uncertain, just return |default_image|. 158 // is uncertain, just return -1.
145 if (supply_status.is_calculating_battery_time) 159 if (supply_status.is_calculating_battery_time)
146 return default_image; 160 return -1;
147 image_index = static_cast<int>(supply_status.battery_percentage / 161 image_index = static_cast<int>(supply_status.battery_percentage /
148 100.0 * (kNumPowerImages - 1)); 162 100.0 * (kNumPowerImages - 1));
149 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); 163 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0);
150 } 164 }
165 return (image_index << 1) | (supply_status.line_power_on ? 1 : 0);
166 }
167
168 // static
169 gfx::ImageSkia TrayPower::GetBatteryImage(int image_index, IconSet icon_set) {
170 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
171 icon_set == ICON_DARK ?
172 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
151 173
152 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See 174 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See
153 // crbug.com/119832. 175 // crbug.com/119832.
154 gfx::Rect region( 176 gfx::Rect region(
155 (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, 177 ((image_index & 0x1) ? kBatteryImageWidth : 0) + 2,
156 image_index * kBatteryImageHeight, 178 (image_index >> 1) * kBatteryImageHeight,
157 kBatteryImageWidth - 2, kBatteryImageHeight); 179 kBatteryImageWidth - 2, kBatteryImageHeight);
158 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); 180 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
159 } 181 }
160 182
161 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { 183 views::View* TrayPower::CreateTrayView(user::LoginStatus status) {
162 // There may not be enough information when this is created about whether 184 // There may not be enough information when this is created about whether
163 // there is a battery or not. So always create this, and adjust visibility as 185 // there is a battery or not. So always create this, and adjust visibility as
164 // necessary. 186 // necessary.
165 PowerSupplyStatus power_status = 187 PowerSupplyStatus power_status =
166 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); 188 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus();
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 return false; 276 return false;
255 case NOTIFICATION_CRITICAL: 277 case NOTIFICATION_CRITICAL:
256 return false; 278 return false;
257 } 279 }
258 NOTREACHED(); 280 NOTREACHED();
259 return false; 281 return false;
260 } 282 }
261 283
262 } // namespace internal 284 } // namespace internal
263 } // namespace ash 285 } // namespace ash
OLDNEW
« ash/system/power/power_status_view.cc ('K') | « ash/system/power/tray_power.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698