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 a076c25d093c70d06b479b77bfb04a46ac1406ac..04132aad32b84e59f126f6a5dc2774dbbc1ed5ec 100644 |
--- a/chrome/browser/chromeos/status/power_menu_button.cc |
+++ b/chrome/browser/chromeos/status/power_menu_button.cc |
@@ -13,23 +13,36 @@ |
#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" |
-namespace chromeos { |
+using views::MenuItemView; |
-//////////////////////////////////////////////////////////////////////////////// |
-// PowerMenuButton |
+namespace { |
+ |
+// Menu item ids. |
+enum { |
+ POWER_BATTERY_PERCENTAGE_ITEM, |
+ POWER_BATTERY_IS_CHARGED_ITEM, |
+}; |
+ |
+} // namespace |
+ |
+namespace chromeos { |
// static |
const int PowerMenuButton::kNumPowerImages = 16; |
oshima
2011/04/15 17:40:12
looks like this can move to anonymous namespace (n
rhashimoto
2011/04/15 21:20:27
Done.
|
+//////////////////////////////////////////////////////////////////////////////// |
+// PowerMenuButton |
+ |
PowerMenuButton::PowerMenuButton() |
: StatusAreaButton(this), |
battery_is_present_(false), |
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,56 +52,25 @@ PowerMenuButton::~PowerMenuButton() { |
} |
//////////////////////////////////////////////////////////////////////////////// |
-// PowerMenuButton, ui::MenuModel implementation: |
- |
-int PowerMenuButton::GetItemCount() const { |
- return 2; |
-} |
+// PowerMenuButton, views::MenuDelegate implementation: |
+std::wstring PowerMenuButton::GetLabel(int id) const { |
+ string16 label; |
+ switch (id) { |
+ case POWER_BATTERY_PERCENTAGE_ITEM: |
+ label = GetBatteryPercentageText(); |
+ break; |
+ case POWER_BATTERY_IS_CHARGED_ITEM: |
+ label = GetBatteryIsChargedText(); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
-ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { |
- return ui::MenuModel::TYPE_COMMAND; |
+ return UTF16ToWide(label); |
} |
-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 { |
+ return false; |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -101,8 +83,45 @@ void PowerMenuButton::OnLocaleChanged() { |
// 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); |
oshima
2011/04/15 17:40:12
DCHECK_EQ
rhashimoto
2011/04/15 21:20:27
Done.
|
+ |
+ 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); |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -113,7 +132,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(); |
@@ -190,8 +250,7 @@ void PowerMenuButton::UpdateIconAndLabelInfo() { |
} |
SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); |
- SetTooltipText(UTF16ToWide(GetLabelAt(0))); |
- power_menu_.Rebuild(); |
+ SetTooltipText(UTF16ToWide(GetBatteryPercentageText())); |
SchedulePaint(); |
} |