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

Unified 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: Fixed crash in VPN submenu. 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 side-by-side diff with in-line comments
Download patch
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..59d98113e0f84ab8439fd54ee7b234342b5c433c 100644
--- a/chrome/browser/chromeos/status/power_menu_button.cc
+++ b/chrome/browser/chromeos/status/power_menu_button.cc
@@ -13,14 +13,26 @@
#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;
+
+namespace {
+
+// Menu item ids.
+enum {
+ POWER_BATTERY_PERCENTAGE_ITEM,
+ POWER_BATTERY_IS_CHARGED_ITEM,
+};
-////////////////////////////////////////////////////////////////////////////////
-// PowerMenuButton
+const int kNumPowerImages = 16;
+
+} // namespace
+
+namespace chromeos {
-// static
-const int PowerMenuButton::kNumPowerImages = 16;
+// PowerMenuButton ------------------------------------------------------------
PowerMenuButton::PowerMenuButton()
: StatusAreaButton(this),
@@ -28,8 +40,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);
}
@@ -38,82 +49,116 @@ PowerMenuButton::~PowerMenuButton() {
CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
}
-////////////////////////////////////////////////////////////////////////////////
-// PowerMenuButton, ui::MenuModel implementation:
+// 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();
+ }
-int PowerMenuButton::GetItemCount() const {
- return 2;
+ return UTF16ToWide(label);
}
-ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const {
- return ui::MenuModel::TYPE_COMMAND;
+bool PowerMenuButton::IsCommandEnabled(int id) const {
+ return false;
}
-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();
- }
-}
+// PowerMenuButton, views::View implementation: -------------------------------
-////////////////////////////////////////////////////////////////////////////////
-// PowerMenuButton, views::View implementation:
void PowerMenuButton::OnLocaleChanged() {
UpdateIconAndLabelInfo();
}
-////////////////////////////////////////////////////////////////////////////////
-// PowerMenuButton, views::ViewMenuDelegate implementation:
+// 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_EQ(this, source);
+
+ 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 = GTK_WINDOW(source->GetWidget()->GetNativeView());
+
+ 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);
}
-////////////////////////////////////////////////////////////////////////////////
-// PowerMenuButton, PowerLibrary::Observer implementation:
+// PowerMenuButton, PowerLibrary::Observer implementation: --------------------
void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
UpdateIconAndLabelInfo();
}
-////////////////////////////////////////////////////////////////////////////////
-// 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 +235,7 @@ void PowerMenuButton::UpdateIconAndLabelInfo() {
}
SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_));
- SetTooltipText(UTF16ToWide(GetLabelAt(0)));
- power_menu_.Rebuild();
+ SetTooltipText(UTF16ToWide(GetBatteryPercentageText()));
SchedulePaint();
}

Powered by Google App Engine
This is Rietveld 408576698