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

Side by Side Diff: ash/system/power/power_status_view.cc

Issue 10907037: Only update system power indicators on change (Closed) Base URL: https://git.chromium.org/git/chromium/src@git-svn
Patch Set: 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/power_status_view.h" 5 #include "ash/system/power/power_status_view.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/shell_delegate.h" 8 #include "ash/shell_delegate.h"
9 #include "ash/system/power/tray_power.h" 9 #include "ash/system/power/tray_power.h"
10 #include "ash/system/tray/tray_constants.h" 10 #include "ash/system/tray/tray_constants.h"
(...skipping 21 matching lines...) Expand all
32 const int kPaddingBetweenBatteryStatusAndIcon = 3; 32 const int kPaddingBetweenBatteryStatusAndIcon = 3;
33 } // namespace 33 } // namespace
34 34
35 PowerStatusView::PowerStatusView(ViewType view_type, 35 PowerStatusView::PowerStatusView(ViewType view_type,
36 bool default_view_right_align) 36 bool default_view_right_align)
37 : default_view_right_align_(default_view_right_align), 37 : default_view_right_align_(default_view_right_align),
38 status_label_(NULL), 38 status_label_(NULL),
39 time_label_(NULL), 39 time_label_(NULL),
40 time_status_label_(NULL), 40 time_status_label_(NULL),
41 icon_(NULL), 41 icon_(NULL),
42 battery_image_index_(-1),
42 view_type_(view_type) { 43 view_type_(view_type) {
43 if (view_type == VIEW_DEFAULT) { 44 if (view_type == VIEW_DEFAULT) {
44 time_status_label_ = new views::Label; 45 time_status_label_ = new views::Label;
45 LayoutDefaultView(); 46 LayoutDefaultView();
46 } else { 47 } else {
47 status_label_ = new views::Label; 48 status_label_ = new views::Label;
48 time_label_ = new views::Label; 49 time_label_ = new views::Label;
49 LayoutNotificationView(); 50 LayoutNotificationView();
50 } 51 }
51 Update(); 52 Update();
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 // call in a crOS session. Until this bug is fixed, use 111 // call in a crOS session. Until this bug is fixed, use
111 // supply_status_.battery_seconds_to_empty to render battery time. 112 // supply_status_.battery_seconds_to_empty to render battery time.
112 // Change back to use averaged_battery_time_to_empty to display in UI 113 // Change back to use averaged_battery_time_to_empty to display in UI
113 // once crosbug.com/31633 is fixed. 114 // once crosbug.com/31633 is fixed.
114 base::TimeDelta time = base::TimeDelta::FromSeconds( 115 base::TimeDelta time = base::TimeDelta::FromSeconds(
115 supply_status_.averaged_battery_time_to_empty); 116 supply_status_.averaged_battery_time_to_empty);
116 hour = time.InHours(); 117 hour = time.InHours();
117 min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); 118 min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
118 } 119 }
119 120
121 // Only update label text if the text changes, to save a GPU repaint
122
123 string16 time_status_text;
120 if (supply_status_.line_power_on && supply_status_.battery_is_full) { 124 if (supply_status_.line_power_on && supply_status_.battery_is_full) {
121 time_status_label_->SetText( 125 time_status_text = ui::ResourceBundle::GetSharedInstance()
122 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( 126 .GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_FULL);
123 IDS_ASH_STATUS_TRAY_BATTERY_FULL));
124 } else { 127 } else {
125 string16 battery_percentage = l10n_util::GetStringFUTF16( 128 string16 battery_percentage = l10n_util::GetStringFUTF16(
126 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY, 129 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY,
127 base::IntToString16( 130 base::IntToString16(
128 static_cast<int>(supply_status_.battery_percentage))); 131 static_cast<int>(supply_status_.battery_percentage)));
129 string16 battery_time = string16(); 132 string16 battery_time = string16();
130 if (supply_status_.is_calculating_battery_time) { 133 if (supply_status_.is_calculating_battery_time) {
131 battery_time = 134 battery_time =
132 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( 135 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
133 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING); 136 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING);
134 } else if (hour || min){ 137 } else if (hour || min){
135 string16 minute = min < 10 ? 138 string16 minute = min < 10 ?
136 ASCIIToUTF16("0") + base::IntToString16(min) : 139 ASCIIToUTF16("0") + base::IntToString16(min) :
137 base::IntToString16(min); 140 base::IntToString16(min);
138 battery_time = 141 battery_time =
139 l10n_util::GetStringFUTF16( 142 l10n_util::GetStringFUTF16(
140 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ONLY, 143 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ONLY,
141 base::IntToString16(hour), 144 base::IntToString16(hour),
142 minute); 145 minute);
143 } 146 }
144 string16 battery_status = battery_time.empty() ? 147 time_status_text = battery_time.empty() ?
145 battery_percentage : 148 battery_percentage :
146 battery_percentage + ASCIIToUTF16(" - ") + battery_time; 149 battery_percentage + ASCIIToUTF16(" - ") + battery_time;
147 time_status_label_->SetText(battery_status);
148 } 150 }
151 if (time_status_text != time_status_label_->text())
152 time_status_label_->SetText(time_status_text);
Daniel Erat 2012/09/04 21:51:58 This seems like a worthwhile optimization to do el
149 } 153 }
150 154
151 void PowerStatusView::UpdateTextForNotificationView() { 155 void PowerStatusView::UpdateTextForNotificationView() {
152 int hour = 0; 156 int hour = 0;
153 int min = 0; 157 int min = 0;
154 if (!supply_status_.is_calculating_battery_time) { 158 if (!supply_status_.is_calculating_battery_time) {
155 base::TimeDelta time = base::TimeDelta::FromSeconds( 159 base::TimeDelta time = base::TimeDelta::FromSeconds(
156 supply_status_.line_power_on ? 160 supply_status_.line_power_on ?
157 supply_status_.averaged_battery_time_to_full : 161 supply_status_.averaged_battery_time_to_full :
158 supply_status_.averaged_battery_time_to_empty); 162 supply_status_.averaged_battery_time_to_empty);
159 hour = time.InHours(); 163 hour = time.InHours();
160 min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); 164 min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
161 } 165 }
162 166
167 // Only update label text if the text changes, to save a GPU repaint
168
169 string16 status_text;
163 if (supply_status_.line_power_on && supply_status_.battery_is_full) { 170 if (supply_status_.line_power_on && supply_status_.battery_is_full) {
164 status_label_->SetText( 171 status_text = ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
165 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( 172 IDS_ASH_STATUS_TRAY_BATTERY_FULL);
166 IDS_ASH_STATUS_TRAY_BATTERY_FULL));
167 } else { 173 } else {
168 status_label_->SetText( 174 status_text = l10n_util::GetStringFUTF16(
169 l10n_util::GetStringFUTF16( 175 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT,
170 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT, 176 base::IntToString16(
171 base::IntToString16( 177 static_cast<int>(supply_status_.battery_percentage)));
172 static_cast<int>(supply_status_.battery_percentage))));
173 } 178 }
179 if (status_text != status_label_->text())
180 status_label_->SetText(status_text);
174 181
182 string16 time_text;
175 if (supply_status_.is_calculating_battery_time) { 183 if (supply_status_.is_calculating_battery_time) {
176 time_label_->SetText( 184 time_text = ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
177 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( 185 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING);
178 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING));
179 } else if (hour || min) { 186 } else if (hour || min) {
180 if (supply_status_.line_power_on) { 187 if (supply_status_.line_power_on) {
181 time_label_->SetText( 188 time_text = l10n_util::GetStringFUTF16(
182 l10n_util::GetStringFUTF16( 189 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL,
183 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL, 190 base::IntToString16(hour),
184 base::IntToString16(hour), 191 base::IntToString16(min));
185 base::IntToString16(min)));
186 } else { 192 } else {
187 // This is a low battery warning, which prompts user when battery 193 // This is a low battery warning, which prompts user when battery
188 // time left is not much (ie in minutes). 194 // time left is not much (ie in minutes).
189 min = hour * 60 + min; 195 min = hour * 60 + min;
190 ShellDelegate* delegate = Shell::GetInstance()->delegate(); 196 ShellDelegate* delegate = Shell::GetInstance()->delegate();
191 if (delegate) { 197 if (delegate) {
192 time_label_->SetText(delegate->GetTimeRemainingString( 198 time_text = delegate->GetTimeRemainingString(
193 base::TimeDelta::FromMinutes(min))); 199 base::TimeDelta::FromMinutes(min));
194 } else {
195 time_label_->SetText(string16());
196 } 200 }
197 } 201 }
198 } else {
199 time_label_->SetText(string16());
200 } 202 }
201 203 if (time_text != time_label_->text())
204 time_label_->SetText(time_text);
202 } 205 }
203 206
204 void PowerStatusView::UpdateIcon() { 207 void PowerStatusView::UpdateIcon() {
205 if (icon_) { 208 if (icon_) {
206 icon_->SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_DARK)); 209 int index = TrayPower::GetBatteryImageIndex(supply_status_);
210 if (battery_image_index_ != index) {
211 battery_image_index_ = index;
212 icon_->SetImage(
213 TrayPower::GetBatteryImage(battery_image_index_, ICON_DARK));
214 }
207 icon_->SetVisible(true); 215 icon_->SetVisible(true);
208 } 216 }
209 } 217 }
210 218
211 void PowerStatusView::Update() { 219 void PowerStatusView::Update() {
212 UpdateText(); 220 UpdateText();
213 UpdateIcon(); 221 UpdateIcon();
214 } 222 }
215 223
216 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { 224 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) {
217 PreferredSizeChanged(); 225 PreferredSizeChanged();
218 } 226 }
219 227
220 gfx::Size PowerStatusView::GetPreferredSize() { 228 gfx::Size PowerStatusView::GetPreferredSize() {
221 gfx::Size size = views::View::GetPreferredSize(); 229 gfx::Size size = views::View::GetPreferredSize();
222 return gfx::Size(size.width(), kTrayPopupItemHeight); 230 return gfx::Size(size.width(), kTrayPopupItemHeight);
223 } 231 }
224 232
225 } // namespace internal 233 } // namespace internal
226 } // namespace ash 234 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698