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

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: Revert MenuController.cc to trunk. Coding style fixes. 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) 2011 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;
20
21 namespace {
22
23 // Menu item ids.
24 enum {
25 POWER_BATTERY_PERCENTAGE_ITEM,
26 POWER_BATTERY_IS_CHARGED_ITEM,
27 };
28
29 const int kNumPowerImages = 16;
30
31 } // namespace
16 32
17 namespace chromeos { 33 namespace chromeos {
18 34
19 //////////////////////////////////////////////////////////////////////////////// 35 ////////////////////////////////////////////////////////////////////////////////
20 // PowerMenuButton 36 // PowerMenuButton
21 37
22 // static
23 const int PowerMenuButton::kNumPowerImages = 16;
24
25 PowerMenuButton::PowerMenuButton() 38 PowerMenuButton::PowerMenuButton()
26 : StatusAreaButton(this), 39 : StatusAreaButton(this),
27 battery_is_present_(false), 40 battery_is_present_(false),
28 line_power_on_(false), 41 line_power_on_(false),
29 battery_fully_charged_(false), 42 battery_fully_charged_(false),
30 battery_percentage_(0.0), 43 battery_percentage_(0.0),
31 icon_id_(-1), 44 icon_id_(-1) {
32 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) {
33 UpdateIconAndLabelInfo(); 45 UpdateIconAndLabelInfo();
34 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); 46 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
35 } 47 }
36 48
37 PowerMenuButton::~PowerMenuButton() { 49 PowerMenuButton::~PowerMenuButton() {
38 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); 50 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
39 } 51 }
40 52
41 //////////////////////////////////////////////////////////////////////////////// 53 ////////////////////////////////////////////////////////////////////////////////
42 // PowerMenuButton, ui::MenuModel implementation: 54 // PowerMenuButton, views::MenuDelegate implementation:
55 std::wstring PowerMenuButton::GetLabel(int id) const {
56 string16 label;
57 switch (id) {
58 case POWER_BATTERY_PERCENTAGE_ITEM:
59 label = GetBatteryPercentageText();
60 break;
61 case POWER_BATTERY_IS_CHARGED_ITEM:
62 label = GetBatteryIsChargedText();
63 break;
64 default:
65 NOTREACHED();
66 }
43 67
44 int PowerMenuButton::GetItemCount() const { 68 return UTF16ToWide(label);
45 return 2;
46 } 69 }
47 70
48 ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { 71 bool PowerMenuButton::IsCommandEnabled(int id) const {
49 return ui::MenuModel::TYPE_COMMAND; 72 return false;
50 }
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::View implementation: 76 // PowerMenuButton, views::View implementation:
96 void PowerMenuButton::OnLocaleChanged() { 77 void PowerMenuButton::OnLocaleChanged() {
97 UpdateIconAndLabelInfo(); 78 UpdateIconAndLabelInfo();
98 } 79 }
99 80
100 //////////////////////////////////////////////////////////////////////////////// 81 ////////////////////////////////////////////////////////////////////////////////
101 // PowerMenuButton, views::ViewMenuDelegate implementation: 82 // PowerMenuButton, views::ViewMenuDelegate implementation:
102 83
103 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { 84 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
104 power_menu_.Rebuild(); 85 // View passed in must be a views::MenuButton, i.e. the PowerMenuButton.
105 power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); 86 DCHECK_EQ(this, source);
87
88 if (!menu_.get()) {
89 menu_.reset(new MenuItemView(this));
90
91 // Create menu items whose text will be supplied by GetLabel().
92 menu_->AppendDelegateMenuItem(POWER_BATTERY_PERCENTAGE_ITEM);
93 menu_->AppendDelegateMenuItem(POWER_BATTERY_IS_CHARGED_ITEM);
94 }
95
96 // TODO(rhashimoto): Remove this workaround when WebUI provides a
97 // top-level widget on the ChromeOS login screen that is a window.
98 // The current BackgroundView class for the ChromeOS login screen
99 // creates a owning Widget that has a native GtkWindow but is not a
100 // Window. This makes it impossible to get the NativeWindow via
101 // the views API. This workaround casts the top-level NativeWidget
102 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt().
103 gfx::NativeWindow window;
104 if (source->GetWindow())
105 window = source->GetWindow()->GetNativeWindow();
106 else
107 window = GTK_WINDOW(source->GetWidget()->GetNativeView());
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);
106 } 118 }
107 119
108 //////////////////////////////////////////////////////////////////////////////// 120 ////////////////////////////////////////////////////////////////////////////////
109 // PowerMenuButton, PowerLibrary::Observer implementation: 121 // PowerMenuButton, PowerLibrary::Observer implementation:
110 122
111 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { 123 void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
112 UpdateIconAndLabelInfo(); 124 UpdateIconAndLabelInfo();
113 } 125 }
114 126
115 //////////////////////////////////////////////////////////////////////////////// 127 ////////////////////////////////////////////////////////////////////////////////
116 // 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 }
117 170
118 void PowerMenuButton::UpdateIconAndLabelInfo() { 171 void PowerMenuButton::UpdateIconAndLabelInfo() {
119 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); 172 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
120 if (!cros) 173 if (!cros)
121 return; 174 return;
122 175
123 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded(); 176 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded();
124 if (cros_loaded) { 177 if (cros_loaded) {
125 battery_is_present_ = cros->battery_is_present(); 178 battery_is_present_ = cros->battery_is_present();
126 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
183 }; 236 };
184 237
185 int index = static_cast<int>(battery_percentage_ / 100.0 * 238 int index = static_cast<int>(battery_percentage_ / 100.0 *
186 nextafter(static_cast<float>(kNumPowerImages), 0)); 239 nextafter(static_cast<float>(kNumPowerImages), 0));
187 index = std::max(std::min(index, kNumPowerImages - 1), 0); 240 index = std::max(std::min(index, kNumPowerImages - 1), 0);
188 icon_id_ = line_power_on_ ? 241 icon_id_ = line_power_on_ ?
189 kChargingImages[index] : kDischargingImages[index]; 242 kChargingImages[index] : kDischargingImages[index];
190 } 243 }
191 244
192 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); 245 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_));
193 SetTooltipText(UTF16ToWide(GetLabelAt(0))); 246 SetTooltipText(UTF16ToWide(GetBatteryPercentageText()));
194 power_menu_.Rebuild();
195 SchedulePaint(); 247 SchedulePaint();
196 } 248 }
197 249
198 } // namespace chromeos 250 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698