OLD | NEW |
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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/auto_reset.h" |
9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
10 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
11 #include "base/time.h" | 12 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
14 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
15 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
19 #include "ui/gfx/canvas_skia.h" | 20 #include "ui/gfx/canvas_skia.h" |
20 #include "ui/gfx/font.h" | 21 #include "ui/gfx/font.h" |
21 #include "views/controls/menu/menu_item_view.h" | 22 #include "views/controls/menu/menu_item_view.h" |
| 23 #include "views/controls/menu/menu_runner.h" |
22 #include "views/controls/menu/submenu_view.h" | 24 #include "views/controls/menu/submenu_view.h" |
23 #include "views/widget/widget.h" | 25 #include "views/widget/widget.h" |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 // Menu item ids. | 29 // Menu item ids. |
28 enum { | 30 enum { |
29 POWER_BATTERY_PERCENTAGE_ITEM = 1000, | 31 POWER_BATTERY_PERCENTAGE_ITEM = 1000, |
30 POWER_BATTERY_IS_CHARGED_ITEM, | 32 POWER_BATTERY_IS_CHARGED_ITEM, |
31 POWER_NO_BATTERY, | 33 POWER_NO_BATTERY, |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 text_x, estimate_y, width() - text_x, text_h, | 215 text_x, estimate_y, width() - text_x, text_h, |
214 gfx::Canvas::TEXT_ALIGN_LEFT); | 216 gfx::Canvas::TEXT_ALIGN_LEFT); |
215 } | 217 } |
216 | 218 |
217 bool OnMousePressed(const views::MouseEvent& event) { | 219 bool OnMousePressed(const views::MouseEvent& event) { |
218 return true; | 220 return true; |
219 } | 221 } |
220 | 222 |
221 void OnMouseReleased(const views::MouseEvent& event) { | 223 void OnMouseReleased(const views::MouseEvent& event) { |
222 if (event.IsLeftMouseButton()) { | 224 if (event.IsLeftMouseButton()) { |
223 DCHECK(menu_button_->menu_); | 225 DCHECK(menu_button_->menu_runner_); |
224 menu_button_->menu_->Cancel(); | 226 menu_button_->menu_runner_->Cancel(); |
225 } | 227 } |
226 } | 228 } |
227 | 229 |
228 private: | 230 private: |
229 PowerMenuButton* menu_button_; | 231 PowerMenuButton* menu_button_; |
230 gfx::Font percentage_font_; | 232 gfx::Font percentage_font_; |
231 gfx::Font estimate_font_; | 233 gfx::Font estimate_font_; |
232 }; | 234 }; |
233 | 235 |
234 //////////////////////////////////////////////////////////////////////////////// | 236 //////////////////////////////////////////////////////////////////////////////// |
235 // PowerMenuButton | 237 // PowerMenuButton |
236 | 238 |
237 PowerMenuButton::PowerMenuButton(StatusAreaHost* host) | 239 PowerMenuButton::PowerMenuButton(StatusAreaHost* host) |
238 : StatusAreaButton(host, this), | 240 : StatusAreaButton(host, this), |
239 battery_is_present_(false), | 241 battery_is_present_(false), |
240 line_power_on_(false), | 242 line_power_on_(false), |
241 battery_percentage_(0.0), | 243 battery_percentage_(0.0), |
242 battery_index_(-1), | 244 battery_index_(-1), |
243 battery_time_to_full_(TimeDelta::FromMicroseconds(kInitialMS)), | 245 battery_time_to_full_(TimeDelta::FromMicroseconds(kInitialMS)), |
244 battery_time_to_empty_(TimeDelta::FromMicroseconds(kInitialMS)), | 246 battery_time_to_empty_(TimeDelta::FromMicroseconds(kInitialMS)), |
245 status_(NULL), | 247 status_(NULL), |
246 menu_(NULL) { | 248 menu_runner_(NULL) { |
247 UpdateIconAndLabelInfo(); | 249 UpdateIconAndLabelInfo(); |
248 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | 250 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
249 } | 251 } |
250 | 252 |
251 PowerMenuButton::~PowerMenuButton() { | 253 PowerMenuButton::~PowerMenuButton() { |
252 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); | 254 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); |
253 } | 255 } |
254 | 256 |
255 // PowerMenuButton, views::MenuDelegate implementation: | 257 // PowerMenuButton, views::MenuDelegate implementation: |
256 | 258 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 //////////////////////////////////////////////////////////////////////////////// | 310 //////////////////////////////////////////////////////////////////////////////// |
309 // PowerMenuButton, views::View implementation: | 311 // PowerMenuButton, views::View implementation: |
310 void PowerMenuButton::OnLocaleChanged() { | 312 void PowerMenuButton::OnLocaleChanged() { |
311 UpdateIconAndLabelInfo(); | 313 UpdateIconAndLabelInfo(); |
312 } | 314 } |
313 | 315 |
314 //////////////////////////////////////////////////////////////////////////////// | 316 //////////////////////////////////////////////////////////////////////////////// |
315 // PowerMenuButton, views::ViewMenuDelegate implementation: | 317 // PowerMenuButton, views::ViewMenuDelegate implementation: |
316 | 318 |
317 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 319 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
318 menu_ = new views::MenuItemView(this); | 320 views::MenuItemView* menu = new views::MenuItemView(this); |
319 views::MenuItemView* submenu = | 321 // MenuRunner takes ownership of |menu|. |
320 menu_->AppendMenuItem( | 322 views::MenuRunner menu_runner(menu); |
| 323 views::MenuItemView* submenu = menu->AppendMenuItem( |
321 POWER_BATTERY_PERCENTAGE_ITEM, | 324 POWER_BATTERY_PERCENTAGE_ITEM, |
322 std::wstring(), | 325 std::wstring(), |
323 views::MenuItemView::NORMAL); | 326 views::MenuItemView::NORMAL); |
324 status_ = new StatusView(this); | 327 status_ = new StatusView(this); |
325 submenu->AddChildView(status_); | 328 submenu->AddChildView(status_); |
326 menu_->CreateSubmenu()->set_resize_open_menu(true); | 329 menu->CreateSubmenu()->set_resize_open_menu(true); |
327 menu_->SetMargins(0, 0); | 330 menu->SetMargins(0, 0); |
328 submenu->SetMargins(0, 0); | 331 submenu->SetMargins(0, 0); |
329 menu_->ChildrenChanged(); | 332 menu->ChildrenChanged(); |
330 | 333 |
331 gfx::Point screen_location; | 334 gfx::Point screen_location; |
332 views::View::ConvertPointToScreen(source, &screen_location); | 335 views::View::ConvertPointToScreen(source, &screen_location); |
333 gfx::Rect bounds(screen_location, source->size()); | 336 gfx::Rect bounds(screen_location, source->size()); |
334 menu_->RunMenuAt( | 337 AutoReset<views::MenuRunner*> menu_runner_reseter(&menu_runner_, |
335 source->GetWidget()->GetTopLevelWidget(), | 338 &menu_runner); |
336 this, | 339 if (menu_runner.RunMenuAt( |
337 bounds, | 340 source->GetWidget()->GetTopLevelWidget(), this, bounds, |
338 views::MenuItemView::TOPRIGHT, | 341 views::MenuItemView::TOPRIGHT, views::MenuRunner::HAS_MNEMONICS) == |
339 true); | 342 views::MenuRunner::MENU_DELETED) |
340 delete menu_; | 343 return; |
341 status_ = NULL; | 344 status_ = NULL; |
342 menu_ = NULL; | |
343 } | 345 } |
344 | 346 |
345 //////////////////////////////////////////////////////////////////////////////// | 347 //////////////////////////////////////////////////////////////////////////////// |
346 // PowerMenuButton, PowerLibrary::Observer implementation: | 348 // PowerMenuButton, PowerLibrary::Observer implementation: |
347 | 349 |
348 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { | 350 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { |
349 UpdateIconAndLabelInfo(); | 351 UpdateIconAndLabelInfo(); |
350 } | 352 } |
351 | 353 |
352 //////////////////////////////////////////////////////////////////////////////// | 354 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 // If previous is 0, then it either was never set (initial condition) | 416 // If previous is 0, then it either was never set (initial condition) |
415 // or got down to 0. | 417 // or got down to 0. |
416 if (*previous == TimeDelta::FromMicroseconds(kInitialMS) || | 418 if (*previous == TimeDelta::FromMicroseconds(kInitialMS) || |
417 diff < kMinDiff || | 419 diff < kMinDiff || |
418 diff > kMaxDiff) { | 420 diff > kMaxDiff) { |
419 *previous = current; | 421 *previous = current; |
420 } | 422 } |
421 } | 423 } |
422 | 424 |
423 } // namespace chromeos | 425 } // namespace chromeos |
OLD | NEW |