1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/status/power_menu_button.h"
#include <algorithm>
#include "base/string_number_conversions.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/controls/menu/submenu_view.h"
#include "views/widget/widget.h"
namespace {
// Menu item ids.
enum {
POWER_BATTERY_PERCENTAGE_ITEM = 1000,
POWER_BATTERY_IS_CHARGED_ITEM,
POWER_NO_BATTERY,
};
} // namespace
namespace chromeos {
using base::TimeDelta;
////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton
// static
const int PowerMenuButton::kNumPowerImages = 19;
PowerMenuButton::PowerMenuButton(StatusAreaHost* host)
: StatusAreaButton(host, this),
battery_is_present_(false),
line_power_on_(false),
battery_fully_charged_(false),
battery_percentage_(0.0),
icon_id_(-1) {
UpdateIconAndLabelInfo();
CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
}
PowerMenuButton::~PowerMenuButton() {
CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
}
// PowerMenuButton, views::MenuDelegate implementation:
std::wstring PowerMenuButton::GetLabel(int id) const {
string16 label;
switch (id) {
case POWER_BATTERY_PERCENTAGE_ITEM:
label = GetBatteryPercentageText();
break;
case POWER_BATTERY_IS_CHARGED_ITEM:
label = GetBatteryIsChargedText();
break;
default:
NOTREACHED();
}
return UTF16ToWide(label);
}
bool PowerMenuButton::IsCommandEnabled(int id) const {
return false;
}
string16 PowerMenuButton::GetBatteryPercentageText() const {
return l10n_util::GetStringFUTF16(
IDS_STATUSBAR_BATTERY_PERCENTAGE,
base::IntToString16(static_cast<int>(battery_percentage_)));
}
string16 PowerMenuButton::GetBatteryIsChargedText() const {
// The second item shows the battery is charged if it is.
if (battery_fully_charged_)
return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED);
// If battery is in an intermediate charge state, show how much time left.
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 = line_power_on_ ?
IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL :
IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY;
return l10n_util::GetStringUTF16(msg);
} else {
// 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 = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL :
IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY;
int hour = time.InHours();
int min = (time - TimeDelta::FromHours(hour)).InMinutes();
string16 hour_str = base::IntToString16(hour);
string16 min_str = base::IntToString16(min);
// Append a "0" before the minute if it's only a single digit.
if (min < 10)
min_str = ASCIIToUTF16("0") + min_str;
return l10n_util::GetStringFUTF16(msg, hour_str, min_str);
}
}
int PowerMenuButton::icon_width() {
return 26;
}
////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, views::View implementation:
void PowerMenuButton::OnLocaleChanged() {
UpdateIconAndLabelInfo();
}
////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, views::ViewMenuDelegate implementation:
void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
UpdateMenu();
gfx::Point screen_location;
views::View::ConvertPointToScreen(source, &screen_location);
gfx::Rect bounds(screen_location, source->size());
menu_->RunMenuAt(
source->GetWidget()->GetTopLevelWidget(),
this,
bounds,
views::MenuItemView::TOPRIGHT,
true);
}
////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, PowerLibrary::Observer implementation:
void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
UpdateIconAndLabelInfo();
}
////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, StatusAreaButton implementation:
void PowerMenuButton::UpdateIconAndLabelInfo() {
PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
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;
// Map 1-100 to 0-100, so 1% shows up as 0%, and 100% remains 100%.
static const double k = 100.0/99;
battery_percentage_ = (battery_percentage_ > 1.0 ?
battery_percentage_ : 1.0) * k - k;
UpdateBatteryTime(&battery_time_to_full_, cros->battery_time_to_full());
UpdateBatteryTime(&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,
IDR_STATUSBAR_BATTERY_CHARGING_13,
IDR_STATUSBAR_BATTERY_CHARGING_14,
IDR_STATUSBAR_BATTERY_CHARGING_15,
IDR_STATUSBAR_BATTERY_CHARGING_16,
IDR_STATUSBAR_BATTERY_CHARGING_17,
IDR_STATUSBAR_BATTERY_CHARGING_18,
IDR_STATUSBAR_BATTERY_CHARGING_19,
};
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,
IDR_STATUSBAR_BATTERY_DISCHARGING_13,
IDR_STATUSBAR_BATTERY_DISCHARGING_14,
IDR_STATUSBAR_BATTERY_DISCHARGING_15,
IDR_STATUSBAR_BATTERY_DISCHARGING_16,
IDR_STATUSBAR_BATTERY_DISCHARGING_17,
IDR_STATUSBAR_BATTERY_DISCHARGING_18,
IDR_STATUSBAR_BATTERY_DISCHARGING_19,
};
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(GetLabel(POWER_BATTERY_PERCENTAGE_ITEM));
SetAccessibleName(GetBatteryPercentageText());
UpdateMenu();
SchedulePaint();
}
void PowerMenuButton::UpdateBatteryTime(TimeDelta* previous,
const TimeDelta& current) {
static const TimeDelta kMaxDiff(TimeDelta::FromMinutes(10));
static const TimeDelta kMinDiff(TimeDelta::FromMinutes(0));
const TimeDelta diff = current - *previous;
// If the diff is small and positive, ignore it in favor of
// keeping time monotonically decreasing.
if (diff < kMinDiff || diff > kMaxDiff)
*previous = current;
}
void PowerMenuButton::UpdateMenu() {
if (!menu_.get())
menu_.reset(new views::MenuItemView(this));
// Remove existing items.
const int old_count = menu_->CreateSubmenu()->child_count();
for (int i = 0; i < old_count; ++i)
menu_->RemoveMenuItemAt(0);
if (battery_is_present_) {
// Create menu items whose text will be supplied by GetLabel().
menu_->AppendDelegateMenuItem(POWER_BATTERY_PERCENTAGE_ITEM);
menu_->AppendDelegateMenuItem(POWER_BATTERY_IS_CHARGED_ITEM);
} else {
menu_->AppendMenuItemWithLabel(
POWER_NO_BATTERY,
UTF16ToWide(l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_BATTERY)));
}
menu_->ChildrenChanged();
}
} // namespace chromeos
|