Index: chrome/browser/chromeos/status/clock_menu_button.cc |
diff --git a/chrome/browser/chromeos/status/clock_menu_button.cc b/chrome/browser/chromeos/status/clock_menu_button.cc |
index 82ebe796b4300cfba59892eecb36e698b643ead8..3a9ce82bf0ba2246ff5a64e37781f94114033a9f 100644 |
--- a/chrome/browser/chromeos/status/clock_menu_button.cc |
+++ b/chrome/browser/chromeos/status/clock_menu_button.cc |
@@ -20,8 +20,18 @@ |
#include "ui/base/resource/resource_bundle.h" |
#include "ui/gfx/canvas.h" |
#include "ui/gfx/font.h" |
+#include "views/widget/widget.h" |
+#include "views/window/window.h" |
-namespace chromeos { |
+using views::MenuItemView; |
+ |
+namespace { |
+ |
+// MenuItemView item ids |
+enum { |
+ CLOCK_DISPLAY_ITEM, |
+ CLOCK_OPEN_OPTIONS_ITEM, |
+}; |
// Amount of slop to add into the timer to make sure we're into the next minute |
// when the timer goes off. |
@@ -33,6 +43,12 @@ const int kFontSizeDelta = 0; |
const int kFontSizeDelta = 1; |
#endif |
+} // namespace |
+ |
+namespace chromeos { |
+ |
+// ClockMenuButton ------------------------------------------------------------ |
+ |
ClockMenuButton::ClockMenuButton(StatusAreaHost* host) |
: StatusAreaButton(this), |
host_(host) { |
@@ -101,8 +117,7 @@ void ClockMenuButton::UpdateText() { |
SchedulePaint(); |
} |
-//////////////////////////////////////////////////////////////////////////////// |
-// ClockMenuButton, NotificationObserver implementation: |
+// ClockMenuButton, NotificationObserver implementation: ---------------------- |
void ClockMenuButton::Observe(NotificationType type, |
const NotificationSource& source, |
@@ -115,69 +130,88 @@ void ClockMenuButton::Observe(NotificationType type, |
} |
} |
- |
-//////////////////////////////////////////////////////////////////////////////// |
-// ClockMenuButton, ui::MenuModel implementation: |
- |
-int ClockMenuButton::GetItemCount() const { |
- // If options dialog is unavailable, don't count a separator and configure |
- // menu item. |
- return host_->ShouldOpenButtonOptions(this) ? 3 : 1; |
-} |
- |
-ui::MenuModel::ItemType ClockMenuButton::GetTypeAt(int index) const { |
- // There's a separator between the current date and the menu item to open |
- // the options menu. |
- return index == 1 ? ui::MenuModel::TYPE_SEPARATOR: |
- ui::MenuModel::TYPE_COMMAND; |
-} |
- |
-string16 ClockMenuButton::GetLabelAt(int index) const { |
- if (index == 0) |
- return base::TimeFormatFriendlyDate(base::Time::Now()); |
- return l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); |
+// ClockMenuButton, views::MenuDelegate implementation: |
+std::wstring ClockMenuButton::GetLabel(int id) const { |
+ DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); |
+ const string16 label = base::TimeFormatFriendlyDate(base::Time::Now()); |
+ return UTF16ToWide(label); |
} |
-bool ClockMenuButton::IsEnabledAt(int index) const { |
- // The 1st item is the current date, which is disabled. |
- return index != 0; |
+bool ClockMenuButton::IsCommandEnabled(int id) const { |
+ DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
+ return id == CLOCK_OPEN_OPTIONS_ITEM; |
} |
-void ClockMenuButton::ActivatedAt(int index) { |
+void ClockMenuButton::ExecuteCommand(int id) { |
+ DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); |
host_->OpenButtonOptions(this); |
} |
-/////////////////////////////////////////////////////////////////////////////// |
-// ClockMenuButton, PowerLibrary::Observer implementation: |
+// ClockMenuButton, PowerLibrary::Observer implementation: -------------------- |
void ClockMenuButton::SystemResumed() { |
UpdateText(); |
} |
-/////////////////////////////////////////////////////////////////////////////// |
-// ClockMenuButton, SystemAccess::Observer implementation: |
+// ClockMenuButton, SystemLibrary::Observer implementation: ------------------- |
void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { |
UpdateText(); |
} |
-//////////////////////////////////////////////////////////////////////////////// |
-// ClockMenuButton, views::ViewMenuDelegate implementation: |
+// ClockMenuButton, views::ViewMenuDelegate implementation: ------------------- |
void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
- if (!clock_menu_.get()) |
- clock_menu_.reset(new views::Menu2(this)); |
- else |
- clock_menu_->Rebuild(); |
- clock_menu_->UpdateStates(); |
- clock_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
+ // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
+ DCHECK_EQ(source, this); |
+ |
+ EnsureMenu(); |
+ |
+ // 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); |
} |
-//////////////////////////////////////////////////////////////////////////////// |
-// ClockMenuButton, views::View implementation: |
+// ClockMenuButton, views::View implementation: ------------------------------- |
void ClockMenuButton::OnLocaleChanged() { |
UpdateText(); |
} |
+void ClockMenuButton::EnsureMenu() { |
+ if (!menu_.get()) { |
+ menu_.reset(new MenuItemView(this)); |
+ |
+ // Text for this item will be set by GetLabel(). |
+ menu_->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); |
+ |
+ // If options dialog is unavailable, don't count a separator and configure |
+ // menu item. |
+ if (host_->ShouldOpenButtonOptions(this)) { |
+ menu_->AppendSeparator(); |
+ |
+ const string16 clock_open_options_label = |
+ l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); |
+ menu_->AppendMenuItemWithLabel( |
+ CLOCK_OPEN_OPTIONS_ITEM, |
+ UTF16ToWide(clock_open_options_label)); |
+ } |
+ } |
+} |
+ |
} // namespace chromeos |