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

Side by Side Diff: chrome/browser/chromeos/status/power_menu_button.cc

Issue 3158014: Added tooltips to stats bar items. (Closed)
Patch Set: Fixed browser tests. Created 10 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/chromeos/status/power_menu_button.h" 5 #include "chrome/browser/chromeos/status/power_menu_button.h"
6 6
7 #include "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "app/resource_bundle.h" 8 #include "app/resource_bundle.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/chromeos/cros/cros_library.h" 12 #include "chrome/browser/chromeos/cros/cros_library.h"
13 #include "gfx/canvas.h" 13 #include "gfx/canvas.h"
14 #include "grit/generated_resources.h" 14 #include "grit/generated_resources.h"
15 #include "grit/theme_resources.h" 15 #include "grit/theme_resources.h"
16 16
17 namespace chromeos { 17 namespace chromeos {
18 18
19 //////////////////////////////////////////////////////////////////////////////// 19 ////////////////////////////////////////////////////////////////////////////////
20 // PowerMenuButton 20 // PowerMenuButton
21 21
22 // static 22 // static
23 const int PowerMenuButton::kNumPowerImages = 12; 23 const int PowerMenuButton::kNumPowerImages = 12;
24 24
25 PowerMenuButton::PowerMenuButton() 25 PowerMenuButton::PowerMenuButton()
26 : StatusAreaButton(this), 26 : StatusAreaButton(this),
27 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)), 27 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)),
28 battery_is_present_(false),
29 line_power_on_(false),
30 battery_fully_charged_(false),
31 battery_percentage_(0.0),
28 icon_id_(-1) { 32 icon_id_(-1) {
29 UpdateIcon(); 33 UpdateIconAndLabelInfo();
30 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); 34 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
31 } 35 }
32 36
33 PowerMenuButton::~PowerMenuButton() { 37 PowerMenuButton::~PowerMenuButton() {
34 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); 38 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
35 } 39 }
36 40
37 //////////////////////////////////////////////////////////////////////////////// 41 ////////////////////////////////////////////////////////////////////////////////
38 // PowerMenuButton, menus::MenuModel implementation: 42 // PowerMenuButton, menus::MenuModel implementation:
39 43
40 int PowerMenuButton::GetItemCount() const { 44 int PowerMenuButton::GetItemCount() const {
41 return 2; 45 return 2;
42 } 46 }
43 47
44 menus::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { 48 menus::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const {
45 return menus::MenuModel::TYPE_COMMAND; 49 return menus::MenuModel::TYPE_COMMAND;
46 } 50 }
47 51
48 string16 PowerMenuButton::GetLabelAt(int index) const { 52 string16 PowerMenuButton::GetLabelAt(int index) const {
49 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
50 // The first item shows the percentage of battery left. 53 // The first item shows the percentage of battery left.
51 if (index == 0) { 54 if (index == 0) {
52 // If fully charged, always show 100% even if internal number is a bit less.
53 double percent = cros->battery_fully_charged() ? 100 :
54 cros->battery_percentage();
55 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE, 55 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE,
56 base::IntToString16(static_cast<int>(percent))); 56 base::IntToString16(static_cast<int>(battery_percentage_)));
57 } 57 }
58 58
59 // The second item shows the battery is charged if it is. 59 // The second item shows the battery is charged if it is.
60 if (cros->battery_fully_charged()) 60 if (battery_fully_charged_)
61 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED); 61 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED);
62 62
63 // If battery is in an intermediate charge state, we show how much time left. 63 // If battery is in an intermediate charge state, we show how much time left.
64 base::TimeDelta time = cros->line_power_on() ? cros->battery_time_to_full() : 64 base::TimeDelta time = line_power_on_ ? battery_time_to_full_ :
65 cros->battery_time_to_empty(); 65 battery_time_to_empty_;
66 if (time.InSeconds() == 0) { 66 if (time.InSeconds() == 0) {
67 // If time is 0, then that means we are still calculating how much time. 67 // If time is 0, then that means we are still calculating how much time.
68 // Depending if line power is on, we either show a message saying that we 68 // Depending if line power is on, we either show a message saying that we
69 // are calculating time until full or calculating remaining time. 69 // are calculating time until full or calculating remaining time.
70 int msg = cros->line_power_on() ? 70 int msg = line_power_on_ ?
71 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL : 71 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL :
72 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY; 72 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY;
73 return l10n_util::GetStringUTF16(msg); 73 return l10n_util::GetStringUTF16(msg);
74 } else { 74 } else {
75 // Depending if line power is on, we either show a message saying XX:YY 75 // Depending if line power is on, we either show a message saying XX:YY
76 // until full or XX:YY remaining where XX is number of hours and YY is 76 // until full or XX:YY remaining where XX is number of hours and YY is
77 // number of minutes. 77 // number of minutes.
78 int msg = cros->line_power_on() ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL : 78 int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL :
79 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY; 79 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY;
80 int hour = time.InHours(); 80 int hour = time.InHours();
81 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); 81 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
82 string16 hour_str = base::IntToString16(hour); 82 string16 hour_str = base::IntToString16(hour);
83 string16 min_str = base::IntToString16(min); 83 string16 min_str = base::IntToString16(min);
84 // Append a "0" before the minute if it's only a single digit. 84 // Append a "0" before the minute if it's only a single digit.
85 if (min < 10) 85 if (min < 10)
86 min_str = ASCIIToUTF16("0") + min_str; 86 min_str = ASCIIToUTF16("0") + min_str;
87 return l10n_util::GetStringFUTF16(msg, hour_str, min_str); 87 return l10n_util::GetStringFUTF16(msg, hour_str, min_str);
88 } 88 }
89 } 89 }
90 90
91 //////////////////////////////////////////////////////////////////////////////// 91 ////////////////////////////////////////////////////////////////////////////////
92 // PowerMenuButton, views::ViewMenuDelegate implementation: 92 // PowerMenuButton, views::ViewMenuDelegate implementation:
93 93
94 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { 94 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
95 power_menu_.Rebuild(); 95 power_menu_.Rebuild();
96 power_menu_.UpdateStates(); 96 power_menu_.UpdateStates();
97 power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); 97 power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
98 } 98 }
99 99
100 //////////////////////////////////////////////////////////////////////////////// 100 ////////////////////////////////////////////////////////////////////////////////
101 // PowerMenuButton, PowerLibrary::Observer implementation: 101 // PowerMenuButton, PowerLibrary::Observer implementation:
102 102
103 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { 103 void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
104 UpdateIcon(); 104 UpdateIconAndLabelInfo();
105 } 105 }
106 106
107 //////////////////////////////////////////////////////////////////////////////// 107 ////////////////////////////////////////////////////////////////////////////////
108 // PowerMenuButton, StatusAreaButton implementation: 108 // PowerMenuButton, StatusAreaButton implementation:
109 109
110 void PowerMenuButton::DrawPressed(gfx::Canvas* canvas) { 110 void PowerMenuButton::DrawPressed(gfx::Canvas* canvas) {
111 DrawPowerIcon(canvas, *ResourceBundle::GetSharedInstance(). 111 DrawPowerIcon(canvas, *ResourceBundle::GetSharedInstance().
112 GetBitmapNamed(IDR_STATUSBAR_BATTERY_PRESSED)); 112 GetBitmapNamed(IDR_STATUSBAR_BATTERY_PRESSED));
113 } 113 }
114 114
115 void PowerMenuButton::DrawIcon(gfx::Canvas* canvas) { 115 void PowerMenuButton::DrawIcon(gfx::Canvas* canvas) {
116 DrawPowerIcon(canvas, icon()); 116 DrawPowerIcon(canvas, icon());
117 } 117 }
118 118
119 void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { 119 void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) {
120 // Draw the battery icon 5 pixels down to center it. 120 // Draw the battery icon 5 pixels down to center it.
121 static const int kIconVerticalPadding = 5; 121 static const int kIconVerticalPadding = 5;
122 canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding); 122 canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding);
123 } 123 }
124 124
125 void PowerMenuButton::UpdateIcon() { 125 void PowerMenuButton::UpdateIconAndLabelInfo() {
126 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); 126 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
127 icon_id_ = IDR_STATUSBAR_BATTERY_UNKNOWN; 127 if (!cros)
128 if (CrosLibrary::Get()->EnsureLoaded()) { 128 return;
129 if (!cros->battery_is_present()) {
130 icon_id_ = IDR_STATUSBAR_BATTERY_MISSING;
131 } else if (cros->line_power_on() && cros->battery_fully_charged()) {
132 icon_id_ = IDR_STATUSBAR_BATTERY_CHARGED;
133 } else {
134 // Get the power image depending on battery percentage. Percentage is
135 // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1.
136 // NOTE: Use an array rather than just calculating a resource number to
137 // avoid creating implicit ordering dependencies on the resource values.
138 static const int kChargingImages[kNumPowerImages] = {
139 IDR_STATUSBAR_BATTERY_CHARGING_1,
140 IDR_STATUSBAR_BATTERY_CHARGING_2,
141 IDR_STATUSBAR_BATTERY_CHARGING_3,
142 IDR_STATUSBAR_BATTERY_CHARGING_4,
143 IDR_STATUSBAR_BATTERY_CHARGING_5,
144 IDR_STATUSBAR_BATTERY_CHARGING_6,
145 IDR_STATUSBAR_BATTERY_CHARGING_7,
146 IDR_STATUSBAR_BATTERY_CHARGING_8,
147 IDR_STATUSBAR_BATTERY_CHARGING_9,
148 IDR_STATUSBAR_BATTERY_CHARGING_10,
149 IDR_STATUSBAR_BATTERY_CHARGING_11,
150 IDR_STATUSBAR_BATTERY_CHARGING_12,
151 };
152 static const int kDischargingImages[kNumPowerImages] = {
153 IDR_STATUSBAR_BATTERY_DISCHARGING_1,
154 IDR_STATUSBAR_BATTERY_DISCHARGING_2,
155 IDR_STATUSBAR_BATTERY_DISCHARGING_3,
156 IDR_STATUSBAR_BATTERY_DISCHARGING_4,
157 IDR_STATUSBAR_BATTERY_DISCHARGING_5,
158 IDR_STATUSBAR_BATTERY_DISCHARGING_6,
159 IDR_STATUSBAR_BATTERY_DISCHARGING_7,
160 IDR_STATUSBAR_BATTERY_DISCHARGING_8,
161 IDR_STATUSBAR_BATTERY_DISCHARGING_9,
162 IDR_STATUSBAR_BATTERY_DISCHARGING_10,
163 IDR_STATUSBAR_BATTERY_DISCHARGING_11,
164 IDR_STATUSBAR_BATTERY_DISCHARGING_12,
165 };
166 129
167 // If fully charged, always show 100% even if percentage is a bit less. 130 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded();
168 double percent = cros->battery_fully_charged() ? 131 if (cros_loaded) {
169 100 : cros->battery_percentage(); 132 battery_is_present_ = cros->battery_is_present();
170 int index = static_cast<int>(percent / 100.0 * 133 line_power_on_ = cros->line_power_on();
171 nextafter(static_cast<float>(kNumPowerImages), 0)); 134 battery_fully_charged_ = cros->battery_fully_charged();
172 index = std::max(std::min(index, kNumPowerImages - 1), 0); 135 battery_percentage_ = cros->battery_percentage();
173 icon_id_ = cros->line_power_on() ? 136 // If fully charged, always show 100% even if internal number is a bit less.
174 kChargingImages[index] : kDischargingImages[index]; 137 // Note: we always call cros->battery_percentage() for test predictability.
175 } 138 if (battery_fully_charged_)
139 battery_percentage_ = 100.0;
140 battery_time_to_full_ = cros->battery_time_to_full();
141 battery_time_to_empty_ = cros->battery_time_to_empty();
176 } 142 }
143
144 if (!cros_loaded) {
145 icon_id_ = IDR_STATUSBAR_BATTERY_UNKNOWN;
146 } else if (!battery_is_present_) {
147 icon_id_ = IDR_STATUSBAR_BATTERY_MISSING;
148 } else if (line_power_on_ && battery_fully_charged_) {
149 icon_id_ = IDR_STATUSBAR_BATTERY_CHARGED;
150 } else {
151 // Get the power image depending on battery percentage. Percentage is
152 // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1.
153 // NOTE: Use an array rather than just calculating a resource number to
154 // avoid creating implicit ordering dependencies on the resource values.
155 static const int kChargingImages[kNumPowerImages] = {
156 IDR_STATUSBAR_BATTERY_CHARGING_1,
157 IDR_STATUSBAR_BATTERY_CHARGING_2,
158 IDR_STATUSBAR_BATTERY_CHARGING_3,
159 IDR_STATUSBAR_BATTERY_CHARGING_4,
160 IDR_STATUSBAR_BATTERY_CHARGING_5,
161 IDR_STATUSBAR_BATTERY_CHARGING_6,
162 IDR_STATUSBAR_BATTERY_CHARGING_7,
163 IDR_STATUSBAR_BATTERY_CHARGING_8,
164 IDR_STATUSBAR_BATTERY_CHARGING_9,
165 IDR_STATUSBAR_BATTERY_CHARGING_10,
166 IDR_STATUSBAR_BATTERY_CHARGING_11,
167 IDR_STATUSBAR_BATTERY_CHARGING_12,
168 };
169 static const int kDischargingImages[kNumPowerImages] = {
170 IDR_STATUSBAR_BATTERY_DISCHARGING_1,
171 IDR_STATUSBAR_BATTERY_DISCHARGING_2,
172 IDR_STATUSBAR_BATTERY_DISCHARGING_3,
173 IDR_STATUSBAR_BATTERY_DISCHARGING_4,
174 IDR_STATUSBAR_BATTERY_DISCHARGING_5,
175 IDR_STATUSBAR_BATTERY_DISCHARGING_6,
176 IDR_STATUSBAR_BATTERY_DISCHARGING_7,
177 IDR_STATUSBAR_BATTERY_DISCHARGING_8,
178 IDR_STATUSBAR_BATTERY_DISCHARGING_9,
179 IDR_STATUSBAR_BATTERY_DISCHARGING_10,
180 IDR_STATUSBAR_BATTERY_DISCHARGING_11,
181 IDR_STATUSBAR_BATTERY_DISCHARGING_12,
182 };
183
184 int index = static_cast<int>(battery_percentage_ / 100.0 *
185 nextafter(static_cast<float>(kNumPowerImages), 0));
186 index = std::max(std::min(index, kNumPowerImages - 1), 0);
187 icon_id_ = line_power_on_ ?
188 kChargingImages[index] : kDischargingImages[index];
189 }
190
177 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); 191 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_));
192 SetTooltipText(UTF16ToWide(GetLabelAt(0)));
178 SchedulePaint(); 193 SchedulePaint();
179 } 194 }
180 195
181 } // namespace chromeos 196 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/status/power_menu_button.h ('k') | chrome/browser/chromeos/status/power_menu_button_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698