Chromium Code Reviews| Index: chrome/browser/chromeos/status/power_menu_button.cc |
| diff --git a/chrome/browser/chromeos/status/power_menu_button.cc b/chrome/browser/chromeos/status/power_menu_button.cc |
| index 6692276d426caf350d9eebf67afae947c1b54b24..dda027f712ece6813ba7555750e5c351caa965fb 100644 |
| --- a/chrome/browser/chromeos/status/power_menu_button.cc |
| +++ b/chrome/browser/chromeos/status/power_menu_button.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// 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. |
| @@ -13,9 +13,19 @@ |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/base/resource/resource_bundle.h" |
| #include "ui/gfx/canvas.h" |
| +#include "views/widget/widget.h" |
| +#include "views/window/window.h" |
| + |
| +using views::MenuItemView; |
| namespace chromeos { |
| +// Menu item ids. |
| +enum { |
| + POWER_BATTERY_PERCENTAGE_ITEM, |
| + POWER_BATTERY_IS_CHARGED_ITEM, |
| +}; |
|
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
|
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // PowerMenuButton |
| @@ -28,8 +38,7 @@ PowerMenuButton::PowerMenuButton() |
| line_power_on_(false), |
| battery_fully_charged_(false), |
| battery_percentage_(0.0), |
| - icon_id_(-1), |
| - ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) { |
| + icon_id_(-1) { |
| UpdateIconAndLabelInfo(); |
| CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
| } |
| @@ -39,64 +48,73 @@ PowerMenuButton::~PowerMenuButton() { |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| -// PowerMenuButton, ui::MenuModel implementation: |
| - |
| -int PowerMenuButton::GetItemCount() const { |
| - return 2; |
| -} |
| +// PowerMenuButton, views::MenuDelegate implementation: |
| +std::wstring PowerMenuButton::GetLabel(int id) const |
| +{ |
|
oshima
2011/04/14 17:27:31
move { to previous line
rhashimoto
2011/04/14 18:27:04
Done.
|
| + string16 label; |
| + switch (id) { |
| + case POWER_BATTERY_PERCENTAGE_ITEM: |
| + label = GetBatteryPercentageText(); |
| + break; |
| + case POWER_BATTERY_IS_CHARGED_ITEM: |
| + label = GetBatteryIsChargedText(); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + 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.
|
| + } |
| -ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { |
| - return ui::MenuModel::TYPE_COMMAND; |
| + 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.
|
| } |
| -string16 PowerMenuButton::GetLabelAt(int index) const { |
| - // The first item shows the percentage of battery left. |
| - if (index == 0) { |
| - return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE, |
| - base::IntToString16(static_cast<int>(battery_percentage_))); |
| - } else if (index == 1) { |
| - // 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. |
| - base::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 - base::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); |
| - } |
| - } else { |
| - NOTREACHED(); |
| - return string16(); |
| - } |
| +bool PowerMenuButton::IsCommandEnabled(int id) const |
| +{ |
|
oshima
2011/04/14 17:27:31
same here
rhashimoto
2011/04/14 18:27:04
Done.
|
| + return false; |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| // PowerMenuButton, views::ViewMenuDelegate implementation: |
| void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
| - power_menu_.Rebuild(); |
| - power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
| + // View passed in must be a views::MenuButton, i.e. the PowerMenuButton. |
| + DCHECK(source == this); |
| + |
| + if (!menu_.get()) { |
| + menu_.reset(new MenuItemView(this)); |
| + |
| + // Create menu items whose text will be supplied by GetLabel(). |
| + menu_->AppendDelegateMenuItem(POWER_BATTERY_PERCENTAGE_ITEM); |
| + menu_->AppendDelegateMenuItem(POWER_BATTERY_IS_CHARGED_ITEM); |
| + } |
| + |
| + // TODO(rhashimoto): Remove this workaround when WebUI provides a |
| + // top-level widget on the ChromeOS login screen that is a window. |
| + // The current BackgroundView class for the ChromeOS login screen |
| + // creates a owning Widget that has a native GtkWindow but is not a |
| + // Window. This makes it impossible to get the NativeWindow via |
| + // the views API. This workaround casts the top-level NativeWidget |
| + // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). |
| + gfx::NativeWindow window; |
| + if (source->GetWindow()) { |
| + // This is the normal case with a browser. |
| + window = source->GetWindow()->GetNativeWindow(); |
| + } else { |
| +#if defined(OS_WIN) |
| + NOTREACHED(); |
| +#elif defined(USE_X11) |
| + window = GTK_WINDOW(source->GetWidget()->GetNativeView()); |
| +#endif |
| + } |
| + |
| + gfx::Point screen_loc; |
| + views::View::ConvertPointToScreen(source, &screen_loc); |
| + gfx::Rect bounds(screen_loc, source->size()); |
| + menu_->RunMenuAt( |
| + window, |
| + this, |
| + bounds, |
| + base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, |
| + true); |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -107,7 +125,48 @@ void PowerMenuButton::PowerChanged(PowerLibrary* obj) { |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| -// PowerMenuButton, StatusAreaButton implementation: |
| +// PowerMenuButton implementation: |
| + |
| +string16 PowerMenuButton::GetBatteryPercentageText() const |
| +{ |
| + return l10n_util::GetStringFUTF16( |
| + IDS_STATUSBAR_BATTERY_PERCENTAGE, |
| + base::IntToString16(static_cast<int>(battery_percentage_))); |
| +} |
| + |
| +string16 PowerMenuButton::GetBatteryIsChargedText() const |
| +{ |
| + 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. |
| + base::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 - base::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); |
| + } |
| +} |
| void PowerMenuButton::UpdateIconAndLabelInfo() { |
| PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
| @@ -184,8 +243,7 @@ void PowerMenuButton::UpdateIconAndLabelInfo() { |
| } |
| SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); |
| - SetTooltipText(UTF16ToWide(GetLabelAt(0))); |
| - power_menu_.Rebuild(); |
| + SetTooltipText(UTF16ToWide(GetBatteryPercentageText())); |
| SchedulePaint(); |
| } |