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

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

Issue 6811025: Change status button menu implementation from Menu2 to MenuItemView. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Changed refcount guard to use OS_CHROMEOS. Created 9 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/string_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "base/time.h" 8 #include "base/time.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/chromeos/cros/cros_library.h" 10 #include "chrome/browser/chromeos/cros/cros_library.h"
11 #include "grit/generated_resources.h" 11 #include "grit/generated_resources.h"
12 #include "grit/theme_resources.h" 12 #include "grit/theme_resources.h"
13 #include "ui/base/l10n/l10n_util.h" 13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h" 14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/canvas.h" 15 #include "ui/gfx/canvas.h"
16 #include "views/widget/widget.h"
17 #include "views/window/window.h"
18
19 using views::MenuItemView;
16 20
17 namespace chromeos { 21 namespace chromeos {
18 22
23 // Menu item ids.
24 enum {
25 POWER_BATTERY_PERCENTAGE_ITEM,
26 POWER_BATTERY_IS_CHARGED_ITEM,
27 };
oshima 2011/04/14 17:27:31 move this (and const below) to anonymous namespace
rhashimoto 2011/04/14 18:27:04 enum moved. The const below is a class static mem
28
19 //////////////////////////////////////////////////////////////////////////////// 29 ////////////////////////////////////////////////////////////////////////////////
20 // PowerMenuButton 30 // PowerMenuButton
21 31
22 // static 32 // static
23 const int PowerMenuButton::kNumPowerImages = 16; 33 const int PowerMenuButton::kNumPowerImages = 16;
24 34
25 PowerMenuButton::PowerMenuButton() 35 PowerMenuButton::PowerMenuButton()
26 : StatusAreaButton(this), 36 : StatusAreaButton(this),
27 battery_is_present_(false), 37 battery_is_present_(false),
28 line_power_on_(false), 38 line_power_on_(false),
29 battery_fully_charged_(false), 39 battery_fully_charged_(false),
30 battery_percentage_(0.0), 40 battery_percentage_(0.0),
31 icon_id_(-1), 41 icon_id_(-1) {
32 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) {
33 UpdateIconAndLabelInfo(); 42 UpdateIconAndLabelInfo();
34 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); 43 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
35 } 44 }
36 45
37 PowerMenuButton::~PowerMenuButton() { 46 PowerMenuButton::~PowerMenuButton() {
38 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); 47 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
39 } 48 }
40 49
41 //////////////////////////////////////////////////////////////////////////////// 50 ////////////////////////////////////////////////////////////////////////////////
42 // PowerMenuButton, ui::MenuModel implementation: 51 // PowerMenuButton, views::MenuDelegate implementation:
52 std::wstring PowerMenuButton::GetLabel(int id) const
53 {
oshima 2011/04/14 17:27:31 move { to previous line
rhashimoto 2011/04/14 18:27:04 Done.
54 string16 label;
55 switch (id) {
56 case POWER_BATTERY_PERCENTAGE_ITEM:
57 label = GetBatteryPercentageText();
58 break;
59 case POWER_BATTERY_IS_CHARGED_ITEM:
60 label = GetBatteryIsChargedText();
61 break;
62 default:
63 NOTREACHED();
64 break;
oshima 2011/04/14 17:27:31 no need for break (chrome tends to prefer shorter
rhashimoto 2011/04/14 18:27:04 Done.
65 }
43 66
44 int PowerMenuButton::GetItemCount() const { 67 return UTF16ToWide(label);
oshima 2011/04/14 17:27:31 or you can simply return UTF16ToWide(Get...) it's
rhashimoto 2011/04/14 18:27:04 Done.
45 return 2;
46 } 68 }
47 69
48 ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { 70 bool PowerMenuButton::IsCommandEnabled(int id) const
49 return ui::MenuModel::TYPE_COMMAND; 71 {
oshima 2011/04/14 17:27:31 same here
rhashimoto 2011/04/14 18:27:04 Done.
50 } 72 return false;
51
52 string16 PowerMenuButton::GetLabelAt(int index) const {
53 // The first item shows the percentage of battery left.
54 if (index == 0) {
55 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE,
56 base::IntToString16(static_cast<int>(battery_percentage_)));
57 } else if (index == 1) {
58 // The second item shows the battery is charged if it is.
59 if (battery_fully_charged_)
60 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED);
61
62 // If battery is in an intermediate charge state, show how much time left.
63 base::TimeDelta time = line_power_on_ ? battery_time_to_full_ :
64 battery_time_to_empty_;
65 if (time.InSeconds() == 0) {
66 // If time is 0, then that means we are still calculating how much time.
67 // Depending if line power is on, we either show a message saying that we
68 // are calculating time until full or calculating remaining time.
69 int msg = line_power_on_ ?
70 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL :
71 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY;
72 return l10n_util::GetStringUTF16(msg);
73 } else {
74 // Depending if line power is on, we either show a message saying XX:YY
75 // until full or XX:YY remaining where XX is number of hours and YY is
76 // number of minutes.
77 int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL :
78 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY;
79 int hour = time.InHours();
80 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
81 string16 hour_str = base::IntToString16(hour);
82 string16 min_str = base::IntToString16(min);
83 // Append a "0" before the minute if it's only a single digit.
84 if (min < 10)
85 min_str = ASCIIToUTF16("0") + min_str;
86 return l10n_util::GetStringFUTF16(msg, hour_str, min_str);
87 }
88 } else {
89 NOTREACHED();
90 return string16();
91 }
92 } 73 }
93 74
94 //////////////////////////////////////////////////////////////////////////////// 75 ////////////////////////////////////////////////////////////////////////////////
95 // PowerMenuButton, views::ViewMenuDelegate implementation: 76 // PowerMenuButton, views::ViewMenuDelegate implementation:
96 77
97 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { 78 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
98 power_menu_.Rebuild(); 79 // View passed in must be a views::MenuButton, i.e. the PowerMenuButton.
99 power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); 80 DCHECK(source == this);
81
82 if (!menu_.get()) {
83 menu_.reset(new MenuItemView(this));
84
85 // Create menu items whose text will be supplied by GetLabel().
86 menu_->AppendDelegateMenuItem(POWER_BATTERY_PERCENTAGE_ITEM);
87 menu_->AppendDelegateMenuItem(POWER_BATTERY_IS_CHARGED_ITEM);
88 }
89
90 // TODO(rhashimoto): Remove this workaround when WebUI provides a
91 // top-level widget on the ChromeOS login screen that is a window.
92 // The current BackgroundView class for the ChromeOS login screen
93 // creates a owning Widget that has a native GtkWindow but is not a
94 // Window. This makes it impossible to get the NativeWindow via
95 // the views API. This workaround casts the top-level NativeWidget
96 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt().
97 gfx::NativeWindow window;
98 if (source->GetWindow()) {
99 // This is the normal case with a browser.
100 window = source->GetWindow()->GetNativeWindow();
101 } else {
102 #if defined(OS_WIN)
103 NOTREACHED();
104 #elif defined(USE_X11)
105 window = GTK_WINDOW(source->GetWidget()->GetNativeView());
106 #endif
107 }
108
109 gfx::Point screen_loc;
110 views::View::ConvertPointToScreen(source, &screen_loc);
111 gfx::Rect bounds(screen_loc, source->size());
112 menu_->RunMenuAt(
113 window,
114 this,
115 bounds,
116 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT,
117 true);
100 } 118 }
101 119
102 //////////////////////////////////////////////////////////////////////////////// 120 ////////////////////////////////////////////////////////////////////////////////
103 // PowerMenuButton, PowerLibrary::Observer implementation: 121 // PowerMenuButton, PowerLibrary::Observer implementation:
104 122
105 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { 123 void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
106 UpdateIconAndLabelInfo(); 124 UpdateIconAndLabelInfo();
107 } 125 }
108 126
109 //////////////////////////////////////////////////////////////////////////////// 127 ////////////////////////////////////////////////////////////////////////////////
110 // PowerMenuButton, StatusAreaButton implementation: 128 // PowerMenuButton implementation:
129
130 string16 PowerMenuButton::GetBatteryPercentageText() const
131 {
132 return l10n_util::GetStringFUTF16(
133 IDS_STATUSBAR_BATTERY_PERCENTAGE,
134 base::IntToString16(static_cast<int>(battery_percentage_)));
135 }
136
137 string16 PowerMenuButton::GetBatteryIsChargedText() const
138 {
139 if (battery_fully_charged_)
140 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED);
141
142 // If battery is in an intermediate charge state, show how
143 // much time left.
144 base::TimeDelta time = line_power_on_ ? battery_time_to_full_ :
145 battery_time_to_empty_;
146 if (time.InSeconds() == 0) {
147 // If time is 0, then that means we are still calculating how much time.
148 // Depending if line power is on, we either show a message saying that we
149 // are calculating time until full or calculating remaining time.
150 int msg = line_power_on_ ?
151 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL :
152 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY;
153 return l10n_util::GetStringUTF16(msg);
154 } else {
155 // Depending if line power is on, we either show a message saying XX:YY
156 // until full or XX:YY remaining where XX is number of hours and YY is
157 // number of minutes.
158 int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL :
159 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY;
160 int hour = time.InHours();
161 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
162 string16 hour_str = base::IntToString16(hour);
163 string16 min_str = base::IntToString16(min);
164 // Append a "0" before the minute if it's only a single digit.
165 if (min < 10)
166 min_str = ASCIIToUTF16("0") + min_str;
167 return l10n_util::GetStringFUTF16(msg, hour_str, min_str);
168 }
169 }
111 170
112 void PowerMenuButton::UpdateIconAndLabelInfo() { 171 void PowerMenuButton::UpdateIconAndLabelInfo() {
113 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); 172 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
114 if (!cros) 173 if (!cros)
115 return; 174 return;
116 175
117 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded(); 176 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded();
118 if (cros_loaded) { 177 if (cros_loaded) {
119 battery_is_present_ = cros->battery_is_present(); 178 battery_is_present_ = cros->battery_is_present();
120 line_power_on_ = cros->line_power_on(); 179 line_power_on_ = cros->line_power_on();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 }; 236 };
178 237
179 int index = static_cast<int>(battery_percentage_ / 100.0 * 238 int index = static_cast<int>(battery_percentage_ / 100.0 *
180 nextafter(static_cast<float>(kNumPowerImages), 0)); 239 nextafter(static_cast<float>(kNumPowerImages), 0));
181 index = std::max(std::min(index, kNumPowerImages - 1), 0); 240 index = std::max(std::min(index, kNumPowerImages - 1), 0);
182 icon_id_ = line_power_on_ ? 241 icon_id_ = line_power_on_ ?
183 kChargingImages[index] : kDischargingImages[index]; 242 kChargingImages[index] : kDischargingImages[index];
184 } 243 }
185 244
186 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); 245 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_));
187 SetTooltipText(UTF16ToWide(GetLabelAt(0))); 246 SetTooltipText(UTF16ToWide(GetBatteryPercentageText()));
188 power_menu_.Rebuild();
189 SchedulePaint(); 247 SchedulePaint();
190 } 248 }
191 249
192 } // namespace chromeos 250 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698