summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaintlou@chromium.org <saintlou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-25 19:08:17 +0000
committersaintlou@chromium.org <saintlou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-25 19:08:17 +0000
commit434d5c04054dd81df1973b80ffbc8f92b8673d70 (patch)
treeda7f160d3d1e79554b2fc13b938b3a63612a50d3
parent7df4c0f837df5d338a6dd9fd2e39b243f6f45dac (diff)
downloadchromium_src-434d5c04054dd81df1973b80ffbc8f92b8673d70.zip
chromium_src-434d5c04054dd81df1973b80ffbc8f92b8673d70.tar.gz
chromium_src-434d5c04054dd81df1973b80ffbc8f92b8673d70.tar.bz2
Revert "Change status button menu"
This reverts commit d73c5c63912ce402c82777bee1ec8f1a31544d9f. BUG=none TEST=none Review URL: http://codereview.chromium.org/6904003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@82906 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/chromeos/status/clock_menu_button.cc118
-rw-r--r--chrome/browser/chromeos/status/clock_menu_button.h35
-rw-r--r--chrome/browser/chromeos/status/network_dropdown_button.cc23
-rw-r--r--chrome/browser/chromeos/status/network_dropdown_button.h14
-rw-r--r--chrome/browser/chromeos/status/network_menu.cc370
-rw-r--r--chrome/browser/chromeos/status/network_menu.h138
-rw-r--r--chrome/browser/chromeos/status/network_menu_button.cc4
-rw-r--r--chrome/browser/chromeos/status/network_menu_button.h1
-rw-r--r--chrome/browser/chromeos/status/power_menu_button.cc176
-rw-r--r--chrome/browser/chromeos/status/power_menu_button.h42
-rw-r--r--views/controls/menu/menu_delegate.cc5
-rw-r--r--views/controls/menu/menu_delegate.h9
-rw-r--r--views/controls/menu/menu_item_view.cc15
-rw-r--r--views/controls/menu/menu_item_view.h7
-rw-r--r--views/controls/menu/menu_item_view_gtk.cc4
-rw-r--r--views/controls/menu/menu_item_view_win.cc4
16 files changed, 376 insertions, 589 deletions
diff --git a/chrome/browser/chromeos/status/clock_menu_button.cc b/chrome/browser/chromeos/status/clock_menu_button.cc
index 3a9ce82..82ebe79 100644
--- a/chrome/browser/chromeos/status/clock_menu_button.cc
+++ b/chrome/browser/chromeos/status/clock_menu_button.cc
@@ -20,18 +20,8 @@
#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"
-using views::MenuItemView;
-
-namespace {
-
-// MenuItemView item ids
-enum {
- CLOCK_DISPLAY_ITEM,
- CLOCK_OPEN_OPTIONS_ITEM,
-};
+namespace chromeos {
// Amount of slop to add into the timer to make sure we're into the next minute
// when the timer goes off.
@@ -43,12 +33,6 @@ const int kFontSizeDelta = 0;
const int kFontSizeDelta = 1;
#endif
-} // namespace
-
-namespace chromeos {
-
-// ClockMenuButton ------------------------------------------------------------
-
ClockMenuButton::ClockMenuButton(StatusAreaHost* host)
: StatusAreaButton(this),
host_(host) {
@@ -117,7 +101,8 @@ void ClockMenuButton::UpdateText() {
SchedulePaint();
}
-// ClockMenuButton, NotificationObserver implementation: ----------------------
+////////////////////////////////////////////////////////////////////////////////
+// ClockMenuButton, NotificationObserver implementation:
void ClockMenuButton::Observe(NotificationType type,
const NotificationSource& source,
@@ -130,88 +115,69 @@ void ClockMenuButton::Observe(NotificationType type,
}
}
-// 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);
+
+////////////////////////////////////////////////////////////////////////////////
+// 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);
}
-bool ClockMenuButton::IsCommandEnabled(int id) const {
- DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM);
- return id == CLOCK_OPEN_OPTIONS_ITEM;
+bool ClockMenuButton::IsEnabledAt(int index) const {
+ // The 1st item is the current date, which is disabled.
+ return index != 0;
}
-void ClockMenuButton::ExecuteCommand(int id) {
- DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id);
+void ClockMenuButton::ActivatedAt(int index) {
host_->OpenButtonOptions(this);
}
-// ClockMenuButton, PowerLibrary::Observer implementation: --------------------
+///////////////////////////////////////////////////////////////////////////////
+// ClockMenuButton, PowerLibrary::Observer implementation:
void ClockMenuButton::SystemResumed() {
UpdateText();
}
-// ClockMenuButton, SystemLibrary::Observer implementation: -------------------
+///////////////////////////////////////////////////////////////////////////////
+// ClockMenuButton, SystemAccess::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) {
- // 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);
+ 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);
}
-// 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
diff --git a/chrome/browser/chromeos/status/clock_menu_button.h b/chrome/browser/chromeos/status/clock_menu_button.h
index ee60d0f..b7d5352 100644
--- a/chrome/browser/chromeos/status/clock_menu_button.h
+++ b/chrome/browser/chromeos/status/clock_menu_button.h
@@ -6,7 +6,6 @@
#define CHROME_BROWSER_CHROMEOS_STATUS_CLOCK_MENU_BUTTON_H_
#pragma once
-#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/timer.h"
#include "chrome/browser/chromeos/cros/power_library.h"
@@ -18,7 +17,7 @@
#include "chrome/browser/chromeos/system_access.h"
#include "unicode/calendar.h"
#include "views/controls/button/menu_button.h"
-#include "views/controls/menu/menu_delegate.h"
+#include "views/controls/menu/menu_2.h"
#include "views/controls/menu/view_menu_delegate.h"
namespace chromeos {
@@ -28,8 +27,8 @@ class StatusAreaHost;
// The clock menu button in the status area.
// This button shows the current time.
class ClockMenuButton : public StatusAreaButton,
- public views::MenuDelegate,
public views::ViewMenuDelegate,
+ public ui::MenuModel,
public NotificationObserver,
public PowerLibrary::Observer,
public SystemAccess::Observer {
@@ -37,10 +36,27 @@ class ClockMenuButton : public StatusAreaButton,
explicit ClockMenuButton(StatusAreaHost* host);
virtual ~ClockMenuButton();
- // views::MenuDelegate implementation
- virtual std::wstring GetLabel(int id) const OVERRIDE;
- virtual bool IsCommandEnabled(int id) const OVERRIDE;
- virtual void ExecuteCommand(int id) OVERRIDE;
+ // ui::MenuModel implementation.
+ virtual bool HasIcons() const { return false; }
+ virtual int GetItemCount() const;
+ virtual ui::MenuModel::ItemType GetTypeAt(int index) const;
+ virtual int GetCommandIdAt(int index) const { return index; }
+ virtual string16 GetLabelAt(int index) const;
+ virtual bool IsItemDynamicAt(int index) const { return true; }
+ virtual bool GetAcceleratorAt(int index,
+ ui::Accelerator* accelerator) const { return false; }
+ virtual bool IsItemCheckedAt(int index) const { return false; }
+ virtual int GetGroupIdAt(int index) const { return 0; }
+ virtual bool GetIconAt(int index, SkBitmap* icon) { return false; }
+ virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const {
+ return NULL;
+ }
+ virtual bool IsEnabledAt(int index) const;
+ virtual ui::MenuModel* GetSubmenuModelAt(int index) const { return NULL; }
+ virtual void HighlightChangedTo(int index) {}
+ virtual void ActivatedAt(int index);
+ virtual void MenuWillShow() {}
+ virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {}
// Overridden from ResumeLibrary::Observer:
virtual void PowerChanged(PowerLibrary* obj) {}
@@ -68,9 +84,6 @@ class ClockMenuButton : public StatusAreaButton,
// views::ViewMenuDelegate implementation.
virtual void RunMenu(views::View* source, const gfx::Point& pt);
- // Create and initialize menu if not already present.
- void EnsureMenu();
-
// Updates text and schedules the timer to fire at the next minute interval.
void UpdateTextAndSetNextTimer();
@@ -79,7 +92,7 @@ class ClockMenuButton : public StatusAreaButton,
// The clock menu.
// NOTE: we use a scoped_ptr here as menu calls into 'this' from the
// constructor.
- scoped_ptr<views::MenuItemView> menu_;
+ scoped_ptr<views::Menu2> clock_menu_;
StatusAreaHost* host_;
diff --git a/chrome/browser/chromeos/status/network_dropdown_button.cc b/chrome/browser/chromeos/status/network_dropdown_button.cc
index 07e8a43..9149cab 100644
--- a/chrome/browser/chromeos/status/network_dropdown_button.cc
+++ b/chrome/browser/chromeos/status/network_dropdown_button.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 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.
@@ -125,25 +125,4 @@ void NetworkDropdownButton::OnNetworkManagerChanged(NetworkLibrary* cros) {
UpdateMenu();
}
-// NetworkDropdownButton, NetworkMenu implementation: --------------------------
-
-bool NetworkDropdownButton::IsBrowserMode() const {
- return browser_mode_;
-}
-
-views::MenuButton* NetworkDropdownButton::GetMenuButton() {
- return this;
-}
-
-gfx::NativeWindow NetworkDropdownButton::GetNativeWindow() const {
- return parent_window_;
-}
-
-void NetworkDropdownButton::OpenButtonOptions() {
-}
-
-bool NetworkDropdownButton::ShouldOpenButtonOptions() const {
- return false;
-}
-
} // namespace chromeos
diff --git a/chrome/browser/chromeos/status/network_dropdown_button.h b/chrome/browser/chromeos/status/network_dropdown_button.h
index 2ab2e7e..8a9ac7aa 100644
--- a/chrome/browser/chromeos/status/network_dropdown_button.h
+++ b/chrome/browser/chromeos/status/network_dropdown_button.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 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.
@@ -35,13 +35,11 @@ class NetworkDropdownButton : public DropDownButton,
void Refresh();
protected:
- // NetworkMenu implementation: -----------------------------------------------
-
- virtual bool IsBrowserMode() const OVERRIDE;
- virtual views::MenuButton* GetMenuButton() OVERRIDE;
- virtual gfx::NativeWindow GetNativeWindow() const OVERRIDE;
- virtual void OpenButtonOptions() OVERRIDE;
- virtual bool ShouldOpenButtonOptions() const OVERRIDE;
+ // NetworkMenu implementation:
+ virtual bool IsBrowserMode() const { return browser_mode_; }
+ virtual gfx::NativeWindow GetNativeWindow() const { return parent_window_; }
+ virtual void OpenButtonOptions() {}
+ virtual bool ShouldOpenButtonOptions() const { return false; }
private:
bool browser_mode_;
diff --git a/chrome/browser/chromeos/status/network_menu.cc b/chrome/browser/chromeos/status/network_menu.cc
index 6fdc441..524e44d 100644
--- a/chrome/browser/chromeos/status/network_menu.cc
+++ b/chrome/browser/chromeos/status/network_menu.cc
@@ -25,165 +25,19 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/skbitmap_operations.h"
-#include "views/controls/button/menu_button.h"
-#include "views/controls/menu/menu_item_view.h"
-#include "views/controls/menu/submenu_view.h"
-#include "views/widget/widget.h"
+#include "views/controls/menu/menu_2.h"
#include "views/window/window.h"
-using views::MenuItemView;
-
-namespace {
-
-// Offset for VPN menu items.
-const int kVPNCommandIndexOffset = 10000;
-
-} // namespace
-
namespace chromeos {
-class NetworkMenuModel : public views::MenuDelegate {
- public:
- struct NetworkInfo {
- NetworkInfo() :
- need_passphrase(false), remembered(true), auto_connect(true) {}
- // "ethernet" | "wifi" | "cellular" | "other".
- std::string network_type;
- // "connected" | "connecting" | "disconnected" | "error".
- std::string status;
- // status message or error message, empty if unknown status.
- std::string message;
- // IP address (if network is active, empty otherwise)
- std::string ip_address;
- // Remembered passphrase.
- std::string passphrase;
- // true if the network requires a passphrase.
- bool need_passphrase;
- // true if the network is currently remembered.
- bool remembered;
- // true if the network is auto connect (meaningful for Wifi only).
- bool auto_connect;
- };
-
- explicit NetworkMenuModel(NetworkMenu* owner) : owner_(owner) {}
- virtual ~NetworkMenuModel() {}
-
- // views::MenuDelegate implementation ----------------------------------------
-
- virtual const gfx::Font& GetLabelFont(int id) const OVERRIDE;
- virtual bool IsItemChecked(int id) const OVERRIDE;
- virtual bool IsCommandEnabled(int id) const OVERRIDE;
- virtual void ExecuteCommand(int id) OVERRIDE;
-
- // Connect or reconnect to the network at |index|.
- // If remember >= 0, set the favorite state of the network.
- // Returns true if a connect occurred (e.g. menu should be closed).
- bool ConnectToNetworkAt(int index,
- const std::string& passphrase,
- const std::string& ssid,
- int remember) const;
-
- // Called by NetworkMenu::RunMenu to initialize list of menu items.
- virtual void InitMenuItems(bool is_browser_mode,
- bool should_open_button_options) {}
-
- // PopulateMenu() clears and reinstalls the menu items defined in this
- // instance by calling PopulateMenuItem() on each one. Subclasses with
- // submenus should override PopulateMenuItem(), deferring to the base
- // class implementation for non-submenu items and calling PopulateMenu()
- // for the submenu after adding it.
- virtual void PopulateMenu(views::MenuItemView* menu);
- virtual void PopulateMenuItem(
- views::MenuItemView* menu,
- int index,
- int command_id);
-
- protected:
- enum MenuItemFlags {
- FLAG_DISABLED = 1 << 0,
- FLAG_TOGGLE_ETHERNET = 1 << 1,
- FLAG_TOGGLE_WIFI = 1 << 2,
- FLAG_TOGGLE_CELLULAR = 1 << 3,
- FLAG_TOGGLE_OFFLINE = 1 << 4,
- FLAG_ASSOCIATED = 1 << 5,
- FLAG_ETHERNET = 1 << 6,
- FLAG_WIFI = 1 << 7,
- FLAG_CELLULAR = 1 << 8,
- FLAG_PRIVATE_NETWORKS = 1 << 9,
- FLAG_OPTIONS = 1 << 10,
- FLAG_ADD_WIFI = 1 << 11,
- FLAG_ADD_CELLULAR = 1 << 12,
- FLAG_VPN = 1 << 13,
- FLAG_ADD_VPN = 1 << 14,
- FLAG_DISCONNECT_VPN = 1 << 15,
- };
-
- struct MenuItem {
- MenuItem()
- : type(ui::MenuModel::TYPE_SEPARATOR),
- sub_menu_model(NULL),
- flags(0) {}
- MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon,
- const std::string& service_path, int flags)
- : type(type),
- label(label),
- icon(icon),
- service_path(service_path),
- sub_menu_model(NULL),
- flags(flags) {}
- MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon,
- NetworkMenuModel* sub_menu_model, int flags)
- : type(type),
- label(label),
- icon(icon),
- sub_menu_model(sub_menu_model),
- flags(flags) {}
-
- ui::MenuModel::ItemType type;
- string16 label;
- SkBitmap icon;
- std::string service_path;
- NetworkMenuModel* sub_menu_model; // Weak.
- int flags;
- };
- typedef std::vector<MenuItem> MenuItemVector;
-
- // Our menu items.
- MenuItemVector menu_items_;
-
- NetworkMenu* owner_; // Weak pointer to NetworkMenu that owns this MenuModel.
-
- private:
- // Shows network details in Web UI options window.
- void ShowTabbedNetworkSettings(const Network* network) const;
-
- // Show a NetworkConfigView modal dialog instance.
- void ShowNetworkConfigView(NetworkConfigView* view) const;
-
- void ActivateCellular(const CellularNetwork* cellular) const;
- void ShowOther(ConnectionType type) const;
- void ShowOtherCellular() const;
-
- DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel);
-};
-
class MainMenuModel : public NetworkMenuModel {
public:
explicit MainMenuModel(NetworkMenu* owner);
virtual ~MainMenuModel() {}
- // NetworkMenuModel implementation -------------------------------------------
+ // NetworkMenuModel implementation.
virtual void InitMenuItems(bool is_browser_mode,
- bool should_open_button_options) OVERRIDE;
- virtual void PopulateMenuItem(views::MenuItemView* menu,
- int index,
- int command_id) OVERRIDE;
-
- // MenuDelegate implementaion ------------------------------------------------
- virtual const gfx::Font& GetLabelFont(int id) const OVERRIDE;
- virtual bool IsItemChecked(int id) const OVERRIDE;
- virtual bool IsCommandEnabled(int id) const OVERRIDE;
- virtual void ExecuteCommand(int id) OVERRIDE;
+ bool should_open_button_options);
private:
scoped_ptr<NetworkMenuModel> vpn_menu_model_;
@@ -199,10 +53,6 @@ class VPNMenuModel : public NetworkMenuModel {
// NetworkMenuModel implementation.
virtual void InitMenuItems(bool is_browser_mode,
bool should_open_button_options);
- virtual void PopulateMenuItem(
- views::MenuItemView* menu,
- int index,
- int command_id) OVERRIDE;
static SkBitmap IconForDisplay(const Network* network);
@@ -210,7 +60,8 @@ class VPNMenuModel : public NetworkMenuModel {
DISALLOW_COPY_AND_ASSIGN(VPNMenuModel);
};
-// NetworkMenuModel, public methods: -------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+// NetworkMenuModel, public methods:
bool NetworkMenuModel::ConnectToNetworkAt(int index,
const std::string& passphrase,
@@ -306,36 +157,55 @@ bool NetworkMenuModel::ConnectToNetworkAt(int index,
return true;
}
-// NetworkMenuModel, views::MenuDelegate implementation ------------------------
+////////////////////////////////////////////////////////////////////////////////
+// NetworkMenuModel, ui::MenuModel implementation:
+
+int NetworkMenuModel::GetItemCount() const {
+ return static_cast<int>(menu_items_.size());
+}
+
+ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const {
+ return menu_items_[index].type;
+}
-const gfx::Font& NetworkMenuModel::GetLabelFont(int id) const {
- DCHECK_LE(0, id);
- DCHECK_GT(static_cast<int>(menu_items_.size()), id);
- return (menu_items_[id].flags & FLAG_ASSOCIATED) ?
- ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) :
- ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
+string16 NetworkMenuModel::GetLabelAt(int index) const {
+ return menu_items_[index].label;
}
-bool NetworkMenuModel::IsItemChecked(int id) const {
+const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const {
+ return (menu_items_[index].flags & FLAG_ASSOCIATED) ?
+ &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) :
+ NULL;
+}
+
+bool NetworkMenuModel::IsItemCheckedAt(int index) const {
+ // All ui::MenuModel::TYPE_CHECK menu items are checked.
return true;
}
-bool NetworkMenuModel::IsCommandEnabled(int id) const {
- if (id < static_cast<int>(menu_items_.size()))
- return !(menu_items_[id].flags & FLAG_DISABLED);
- else
+bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) {
+ if (!menu_items_[index].icon.empty()) {
+ *icon = menu_items_[index].icon;
return true;
+ }
+ return false;
+}
+
+bool NetworkMenuModel::IsEnabledAt(int index) const {
+ return !(menu_items_[index].flags & FLAG_DISABLED);
+}
+
+ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const {
+ return menu_items_[index].sub_menu_model;
}
-void NetworkMenuModel::ExecuteCommand(int id) {
- DCHECK_LE(0, id);
- DCHECK_GT(static_cast<int>(menu_items_.size()), id);
+void NetworkMenuModel::ActivatedAt(int index) {
// When we are refreshing the menu, ignore menu item activation.
if (owner_->refreshing_menu_)
return;
NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
- int flags = menu_items_[id].flags;
+ int flags = menu_items_[index].flags;
if (flags & FLAG_OPTIONS) {
owner_->OpenButtonOptions();
} else if (flags & FLAG_TOGGLE_ETHERNET) {
@@ -365,7 +235,7 @@ void NetworkMenuModel::ExecuteCommand(int id) {
} else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI |
FLAG_CELLULAR | FLAG_ADD_CELLULAR |
FLAG_VPN | FLAG_ADD_VPN)) {
- ConnectToNetworkAt(id, std::string(), std::string(), -1);
+ ConnectToNetworkAt(index, std::string(), std::string(), -1);
} else if (flags & FLAG_DISCONNECT_VPN) {
const VirtualNetwork* active_vpn = cros->virtual_network();
if (active_vpn)
@@ -373,42 +243,8 @@ void NetworkMenuModel::ExecuteCommand(int id) {
}
}
-void NetworkMenuModel::PopulateMenu(views::MenuItemView* menu) {
- menu->CreateSubmenu()->RemoveAllChildViews(true);
- const int menu_items_count = static_cast<int>(menu_items_.size());
- for (int i = 0; i < menu_items_count; ++i)
- PopulateMenuItem(menu, i, i);
-
- menu->ChildrenChanged();
-}
-
-void NetworkMenuModel::PopulateMenuItem(
- views::MenuItemView* menu,
- int index,
- int command_id) {
- DCHECK_GT(static_cast<int>(menu_items_.size()), index);
- const MenuItem& item = menu_items_[index];
- switch (item.type) {
- case ui::MenuModel::TYPE_SEPARATOR:
- menu->AppendSeparator();
- break;
- case ui::MenuModel::TYPE_COMMAND:
- if (item.icon.empty()) {
- menu->AppendMenuItemWithLabel(command_id, UTF16ToWide(item.label));
- } else {
- menu->AppendMenuItemWithIcon(
- command_id,
- UTF16ToWide(item.label),
- item.icon);
- }
- break;
- default:
- // Submenus should be handled by subclasses.
- NOTREACHED();
- }
-}
-
-// NetworkMenuModel, private methods: ------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+// NetworkMenuModel, private methods:
void NetworkMenuModel::ShowTabbedNetworkSettings(const Network* network) const {
DCHECK(network);
@@ -449,15 +285,14 @@ void NetworkMenuModel::ShowOtherCellular() const {
ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow());
}
-// MainMenuModel ---------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+// MainMenuModel
MainMenuModel::MainMenuModel(NetworkMenu* owner)
: NetworkMenuModel(owner) {
vpn_menu_model_.reset(new VPNMenuModel(owner));
}
-// MainMenuModel, NetworkMenuModel implementation: -----------------------------
-
void MainMenuModel::InitMenuItems(bool is_browser_mode,
bool should_open_button_options) {
// This gets called on initialization, so any changes should be reflected
@@ -758,68 +593,13 @@ void MainMenuModel::InitMenuItems(bool is_browser_mode,
}
}
-// MainMenuModel, views::MenuDelegate implementation ---------------------------
-
-const gfx::Font& MainMenuModel::GetLabelFont(int id) const {
- if (id >= kVPNCommandIndexOffset)
- return vpn_menu_model_->GetLabelFont(id - kVPNCommandIndexOffset);
- else
- return NetworkMenuModel::GetLabelFont(id);
-}
-
-bool MainMenuModel::IsItemChecked(int id) const {
- if (id >= kVPNCommandIndexOffset)
- return vpn_menu_model_->IsItemChecked(id - kVPNCommandIndexOffset);
- else
- return NetworkMenuModel::IsItemChecked(id);
-}
-
-bool MainMenuModel::IsCommandEnabled(int id) const {
- if (id >= kVPNCommandIndexOffset)
- return vpn_menu_model_->IsCommandEnabled(id - kVPNCommandIndexOffset);
- else
- return NetworkMenuModel::IsCommandEnabled(id);
-}
-
-void MainMenuModel::ExecuteCommand(int id) {
- if (id >= kVPNCommandIndexOffset)
- vpn_menu_model_->ExecuteCommand(id - kVPNCommandIndexOffset);
- else
- NetworkMenuModel::ExecuteCommand(id);
-}
-
-// MainMenuModel, NetworkMenuModel implementation: -----------------------------
-
-void MainMenuModel::PopulateMenuItem(
- views::MenuItemView* menu,
- int index,
- int command_id) {
- DCHECK_GT(static_cast<int>(menu_items_.size()), index);
- const MenuItem& item = menu_items_[index];
- if (item.type == ui::MenuModel::TYPE_SUBMENU) {
- views::MenuItemView* vpn_menu = NULL;
- if (item.icon.empty()) {
- vpn_menu = menu->AppendSubMenu(command_id, UTF16ToWide(item.label));
- } else {
- vpn_menu = menu->AppendSubMenuWithIcon(
- command_id,
- UTF16ToWide(item.label),
- item.icon);
- }
- vpn_menu_model_->PopulateMenu(vpn_menu);
- } else {
- NetworkMenuModel::PopulateMenuItem(menu, index, command_id);
- }
-}
-
-// VPNMenuModel ----------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+// VPNMenuModel
VPNMenuModel::VPNMenuModel(NetworkMenu* owner)
: NetworkMenuModel(owner) {
}
-// VPNMenuModel, NetworkMenuModel implementation: ------------------------------
-
void VPNMenuModel::InitMenuItems(bool is_browser_mode,
bool should_open_button_options) {
// This gets called on initialization, so any changes should be reflected
@@ -885,16 +665,6 @@ void VPNMenuModel::InitMenuItems(bool is_browser_mode,
}
}
-void VPNMenuModel::PopulateMenuItem(
- views::MenuItemView* menu,
- int index,
- int command_id) {
- NetworkMenuModel::PopulateMenuItem(
- menu,
- index,
- command_id + kVPNCommandIndexOffset);
-}
-
// static
SkBitmap VPNMenuModel::IconForDisplay(const Network* network) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
@@ -982,8 +752,7 @@ SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages];
NetworkMenu::NetworkMenu()
: min_width_(-1) {
main_menu_model_.reset(new MainMenuModel(this));
- network_menu_.reset(new views::MenuItemView(main_menu_model_.get()));
- network_menu_->set_has_icons(true);
+ network_menu_.reset(new views::Menu2(main_menu_model_.get()));
}
NetworkMenu::~NetworkMenu() {
@@ -991,16 +760,18 @@ NetworkMenu::~NetworkMenu() {
void NetworkMenu::SetFirstLevelMenuWidth(int width) {
min_width_ = width;
+ // This actually has no effect since menu is rebuilt before showing.
+ network_menu_->SetMinimumWidth(width);
}
void NetworkMenu::CancelMenu() {
- network_menu_->Cancel();
+ network_menu_->CancelMenu();
}
void NetworkMenu::UpdateMenu() {
refreshing_menu_ = true;
main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions());
- main_menu_model_->PopulateMenu(network_menu_.get());
+ network_menu_->Rebuild();
refreshing_menu_ = false;
}
@@ -1199,33 +970,26 @@ SkBitmap NetworkMenu::IconForDisplay(const SkBitmap* icon,
return canvas.ExtractBitmap();
}
-// NetworkMenu, views::ViewMenuDelegate implementation: ------------------------
+////////////////////////////////////////////////////////////////////////////////
+// NetworkMenu, views::ViewMenuDelegate implementation:
void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) {
- DCHECK_EQ(GetMenuButton(), source);
+ refreshing_menu_ = true;
NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
cros->RequestNetworkScan();
- UpdateMenu();
-
- // 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());
- network_menu_->RunMenuAt(
- window,
- GetMenuButton(),
- bounds,
- base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT,
- true);
+ // Build initial menu items. They will be updated when UpdateMenu is
+ // called from NetworkChanged.
+ main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions());
+ network_menu_->Rebuild();
+
+ // Restore menu width, if it was set up.
+ // NOTE: width isn't checked for correctness here since all width-related
+ // logic implemented inside |network_menu_|.
+ if (min_width_ != -1)
+ network_menu_->SetMinimumWidth(min_width_);
+ refreshing_menu_ = false;
+ network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
}
} // namespace chromeos
diff --git a/chrome/browser/chromeos/status/network_menu.h b/chrome/browser/chromeos/status/network_menu.h
index 9f14b27..62c1a8e 100644
--- a/chrome/browser/chromeos/status/network_menu.h
+++ b/chrome/browser/chromeos/status/network_menu.h
@@ -13,7 +13,6 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/native_widget_types.h"
-#include "views/controls/menu/menu_delegate.h"
#include "views/controls/menu/view_menu_delegate.h"
namespace gfx {
@@ -21,12 +20,142 @@ class Canvas;
}
namespace views {
-class MenuItemView;
+class Menu2;
}
namespace chromeos {
-class NetworkMenuModel;
+class NetworkMenu;
+
+class NetworkMenuModel : public ui::MenuModel {
+ public:
+ struct NetworkInfo {
+ NetworkInfo() :
+ need_passphrase(false), remembered(true), auto_connect(true) {}
+ // "ethernet" | "wifi" | "cellular" | "other".
+ std::string network_type;
+ // "connected" | "connecting" | "disconnected" | "error".
+ std::string status;
+ // status message or error message, empty if unknown status.
+ std::string message;
+ // IP address (if network is active, empty otherwise)
+ std::string ip_address;
+ // Remembered passphrase.
+ std::string passphrase;
+ // true if the network requires a passphrase.
+ bool need_passphrase;
+ // true if the network is currently remembered.
+ bool remembered;
+ // true if the network is auto connect (meaningful for Wifi only).
+ bool auto_connect;
+ };
+
+ explicit NetworkMenuModel(NetworkMenu* owner) : owner_(owner) {}
+ virtual ~NetworkMenuModel() {}
+
+ // Connect or reconnect to the network at |index|.
+ // If remember >= 0, set the favorite state of the network.
+ // Returns true if a connect occurred (e.g. menu should be closed).
+ bool ConnectToNetworkAt(int index,
+ const std::string& passphrase,
+ const std::string& ssid,
+ int remember) const;
+
+ // Called by NetworkMenu::RunMenu to initialize list of menu items.
+ virtual void InitMenuItems(bool is_browser_mode,
+ bool should_open_button_options) {}
+
+ // ui::MenuModel implementation.
+ virtual bool HasIcons() const { return true; }
+ virtual int GetItemCount() const;
+ virtual ui::MenuModel::ItemType GetTypeAt(int index) const;
+ virtual int GetCommandIdAt(int index) const { return index; }
+ virtual string16 GetLabelAt(int index) const;
+ virtual bool IsItemDynamicAt(int index) const { return true; }
+ virtual const gfx::Font* GetLabelFontAt(int index) const;
+ virtual bool GetAcceleratorAt(int index,
+ ui::Accelerator* accelerator) const { return false; }
+ virtual bool IsItemCheckedAt(int index) const;
+ virtual int GetGroupIdAt(int index) const { return 0; }
+ virtual bool GetIconAt(int index, SkBitmap* icon);
+ virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const {
+ return NULL;
+ }
+ virtual bool IsEnabledAt(int index) const;
+ virtual ui::MenuModel* GetSubmenuModelAt(int index) const;
+ virtual void HighlightChangedTo(int index) {}
+ virtual void ActivatedAt(int index);
+ virtual void MenuWillShow() {}
+ virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {}
+
+ protected:
+ enum MenuItemFlags {
+ FLAG_DISABLED = 1 << 0,
+ FLAG_TOGGLE_ETHERNET = 1 << 1,
+ FLAG_TOGGLE_WIFI = 1 << 2,
+ FLAG_TOGGLE_CELLULAR = 1 << 3,
+ FLAG_TOGGLE_OFFLINE = 1 << 4,
+ FLAG_ASSOCIATED = 1 << 5,
+ FLAG_ETHERNET = 1 << 6,
+ FLAG_WIFI = 1 << 7,
+ FLAG_CELLULAR = 1 << 8,
+ FLAG_PRIVATE_NETWORKS = 1 << 9,
+ FLAG_OPTIONS = 1 << 10,
+ FLAG_ADD_WIFI = 1 << 11,
+ FLAG_ADD_CELLULAR = 1 << 12,
+ FLAG_VPN = 1 << 13,
+ FLAG_ADD_VPN = 1 << 14,
+ FLAG_DISCONNECT_VPN = 1 << 15,
+ };
+
+ struct MenuItem {
+ MenuItem()
+ : type(ui::MenuModel::TYPE_SEPARATOR),
+ sub_menu_model(NULL),
+ flags(0) {}
+ MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon,
+ const std::string& service_path, int flags)
+ : type(type),
+ label(label),
+ icon(icon),
+ service_path(service_path),
+ sub_menu_model(NULL),
+ flags(flags) {}
+ MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon,
+ NetworkMenuModel* sub_menu_model, int flags)
+ : type(type),
+ label(label),
+ icon(icon),
+ sub_menu_model(sub_menu_model),
+ flags(flags) {}
+
+ ui::MenuModel::ItemType type;
+ string16 label;
+ SkBitmap icon;
+ std::string service_path;
+ NetworkMenuModel* sub_menu_model; // Weak.
+ int flags;
+ };
+ typedef std::vector<MenuItem> MenuItemVector;
+
+ // Our menu items.
+ MenuItemVector menu_items_;
+
+ NetworkMenu* owner_; // Weak pointer to NetworkMenu that owns this MenuModel.
+
+ private:
+ // Shows network details in Web UI options window.
+ void ShowTabbedNetworkSettings(const Network* network) const;
+
+ // Show a NetworkConfigView modal dialog instance.
+ void ShowNetworkConfigView(NetworkConfigView* view) const;
+
+ void ActivateCellular(const CellularNetwork* cellular) const;
+ void ShowOther(ConnectionType type) const;
+ void ShowOtherCellular() const;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel);
+};
// Menu for network menu button in the status area/welcome screen.
// This class will populating the menu with the list of networks.
@@ -124,7 +253,6 @@ class NetworkMenu : public views::ViewMenuDelegate {
const SkBitmap* bottom_left_badge);
protected:
- virtual views::MenuButton* GetMenuButton() = 0;
virtual gfx::NativeWindow GetNativeWindow() const = 0;
virtual void OpenButtonOptions() = 0;
virtual bool ShouldOpenButtonOptions() const = 0;
@@ -161,7 +289,7 @@ class NetworkMenu : public views::ViewMenuDelegate {
static SkBitmap kAnimatingImagesBlack[];
// The network menu.
- scoped_ptr<views::MenuItemView> network_menu_;
+ scoped_ptr<views::Menu2> network_menu_;
scoped_ptr<NetworkMenuModel> main_menu_model_;
diff --git a/chrome/browser/chromeos/status/network_menu_button.cc b/chrome/browser/chromeos/status/network_menu_button.cc
index 7c6cd9a..97682c5 100644
--- a/chrome/browser/chromeos/status/network_menu_button.cc
+++ b/chrome/browser/chromeos/status/network_menu_button.cc
@@ -113,10 +113,6 @@ bool NetworkMenuButton::IsBrowserMode() const {
return host_->GetScreenMode() == StatusAreaHost::kBrowserMode;
}
-views::MenuButton* NetworkMenuButton::GetMenuButton() {
- return this;
-}
-
gfx::NativeWindow NetworkMenuButton::GetNativeWindow() const {
return host_->GetNativeWindow();
}
diff --git a/chrome/browser/chromeos/status/network_menu_button.h b/chrome/browser/chromeos/status/network_menu_button.h
index 3f8ea48..61f024c 100644
--- a/chrome/browser/chromeos/status/network_menu_button.h
+++ b/chrome/browser/chromeos/status/network_menu_button.h
@@ -74,7 +74,6 @@ class NetworkMenuButton : public StatusAreaButton,
protected:
// NetworkMenu implementation:
- virtual views::MenuButton* GetMenuButton();
virtual gfx::NativeWindow GetNativeWindow() const;
virtual void OpenButtonOptions();
virtual bool ShouldOpenButtonOptions() const;
diff --git a/chrome/browser/chromeos/status/power_menu_button.cc b/chrome/browser/chromeos/status/power_menu_button.cc
index 59d9811..a076c25 100644
--- a/chrome/browser/chromeos/status/power_menu_button.cc
+++ b/chrome/browser/chromeos/status/power_menu_button.cc
@@ -13,26 +13,14 @@
#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 {
-
-// Menu item ids.
-enum {
- POWER_BATTERY_PERCENTAGE_ITEM,
- POWER_BATTERY_IS_CHARGED_ITEM,
-};
-
-const int kNumPowerImages = 16;
-
-} // namespace
namespace chromeos {
-// PowerMenuButton ------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+// PowerMenuButton
+
+// static
+const int PowerMenuButton::kNumPowerImages = 16;
PowerMenuButton::PowerMenuButton()
: StatusAreaButton(this),
@@ -40,7 +28,8 @@ PowerMenuButton::PowerMenuButton()
line_power_on_(false),
battery_fully_charged_(false),
battery_percentage_(0.0),
- icon_id_(-1) {
+ icon_id_(-1),
+ ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) {
UpdateIconAndLabelInfo();
CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
}
@@ -49,116 +38,82 @@ PowerMenuButton::~PowerMenuButton() {
CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
}
-// 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();
- }
+////////////////////////////////////////////////////////////////////////////////
+// PowerMenuButton, ui::MenuModel implementation:
- return UTF16ToWide(label);
+int PowerMenuButton::GetItemCount() const {
+ return 2;
}
-bool PowerMenuButton::IsCommandEnabled(int id) const {
- return false;
+ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const {
+ return ui::MenuModel::TYPE_COMMAND;
}
-// PowerMenuButton, views::View implementation: -------------------------------
+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:
void PowerMenuButton::OnLocaleChanged() {
UpdateIconAndLabelInfo();
}
-// PowerMenuButton, views::ViewMenuDelegate implementation: -------------------
+////////////////////////////////////////////////////////////////////////////////
+// PowerMenuButton, views::ViewMenuDelegate implementation:
void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
- // 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);
+ power_menu_.Rebuild();
+ power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
}
-// PowerMenuButton, PowerLibrary::Observer implementation: --------------------
+////////////////////////////////////////////////////////////////////////////////
+// PowerMenuButton, PowerLibrary::Observer implementation:
void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
UpdateIconAndLabelInfo();
}
-// 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);
- }
-}
+////////////////////////////////////////////////////////////////////////////////
+// PowerMenuButton, StatusAreaButton implementation:
void PowerMenuButton::UpdateIconAndLabelInfo() {
PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
@@ -235,7 +190,8 @@ void PowerMenuButton::UpdateIconAndLabelInfo() {
}
SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_));
- SetTooltipText(UTF16ToWide(GetBatteryPercentageText()));
+ SetTooltipText(UTF16ToWide(GetLabelAt(0)));
+ power_menu_.Rebuild();
SchedulePaint();
}
diff --git a/chrome/browser/chromeos/status/power_menu_button.h b/chrome/browser/chromeos/status/power_menu_button.h
index 782948e..216344e 100644
--- a/chrome/browser/chromeos/status/power_menu_button.h
+++ b/chrome/browser/chromeos/status/power_menu_button.h
@@ -6,10 +6,10 @@
#define CHROME_BROWSER_CHROMEOS_STATUS_POWER_MENU_BUTTON_H_
#pragma once
-#include "base/compiler_specific.h"
#include "chrome/browser/chromeos/cros/power_library.h"
#include "chrome/browser/chromeos/status/status_area_button.h"
-#include "views/controls/menu/menu_delegate.h"
+#include "ui/base/models/menu_model.h"
+#include "views/controls/menu/menu_2.h"
#include "views/controls/menu/view_menu_delegate.h"
namespace base {
@@ -23,16 +23,34 @@ namespace chromeos {
// The power menu button in the status area.
// This class will handle getting the power status and populating the menu.
class PowerMenuButton : public StatusAreaButton,
- public views::MenuDelegate,
public views::ViewMenuDelegate,
+ public ui::MenuModel,
public PowerLibrary::Observer {
public:
PowerMenuButton();
virtual ~PowerMenuButton();
- // views::MenuDelegate implementation
- virtual std::wstring GetLabel(int id) const OVERRIDE;
- virtual bool IsCommandEnabled(int id) const OVERRIDE;
+ // ui::MenuModel implementation.
+ virtual bool HasIcons() const { return false; }
+ virtual int GetItemCount() const;
+ virtual ui::MenuModel::ItemType GetTypeAt(int index) const;
+ virtual int GetCommandIdAt(int index) const { return index; }
+ virtual string16 GetLabelAt(int index) const;
+ virtual bool IsItemDynamicAt(int index) const { return true; }
+ virtual bool GetAcceleratorAt(int index,
+ ui::Accelerator* accelerator) const { return false; }
+ virtual bool IsItemCheckedAt(int index) const { return false; }
+ virtual int GetGroupIdAt(int index) const { return 0; }
+ virtual bool GetIconAt(int index, SkBitmap* icon) { return false; }
+ virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const {
+ return NULL;
+ }
+ virtual bool IsEnabledAt(int index) const { return false; }
+ virtual ui::MenuModel* GetSubmenuModelAt(int index) const { return NULL; }
+ virtual void HighlightChangedTo(int index) {}
+ virtual void ActivatedAt(int index) {}
+ virtual void MenuWillShow() {}
+ virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {}
// PowerLibrary::Observer implementation.
virtual void PowerChanged(PowerLibrary* obj);
@@ -50,13 +68,12 @@ class PowerMenuButton : public StatusAreaButton,
// views::ViewMenuDelegate implementation.
virtual void RunMenu(views::View* source, const gfx::Point& pt);
- // Format strings with power status
- string16 GetBatteryPercentageText() const;
- string16 GetBatteryIsChargedText() const;
-
// Update the power icon and menu label info depending on the power status.
void UpdateIconAndLabelInfo();
+ // The number of power images.
+ static const int kNumPowerImages;
+
// Stored data gathered from CrosLibrary::PowerLibrary.
bool battery_is_present_;
bool line_power_on_;
@@ -68,8 +85,9 @@ class PowerMenuButton : public StatusAreaButton,
// The currently showing icon bitmap id.
int icon_id_;
- // The power menu.
- scoped_ptr<views::MenuItemView> menu_;
+ // The power menu. This needs to be initialized last since it calls into
+ // GetLabelAt() during construction.
+ views::Menu2 power_menu_;
DISALLOW_COPY_AND_ASSIGN(PowerMenuButton);
};
diff --git a/views/controls/menu/menu_delegate.cc b/views/controls/menu/menu_delegate.cc
index 8bd21bf..e70fb17 100644
--- a/views/controls/menu/menu_delegate.cc
+++ b/views/controls/menu/menu_delegate.cc
@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "views/controls/menu/menu_config.h"
#include "views/controls/menu/menu_delegate.h"
namespace views {
@@ -15,10 +14,6 @@ std::wstring MenuDelegate::GetLabel(int id) const {
return std::wstring();
}
-const gfx::Font& MenuDelegate::GetLabelFont(int id) const {
- return MenuConfig::instance().font;
-}
-
std::wstring MenuDelegate::GetTooltipText(int id,
const gfx::Point& screen_loc) {
return std::wstring();
diff --git a/views/controls/menu/menu_delegate.h b/views/controls/menu/menu_delegate.h
index 578f679..6a5cc5b 100644
--- a/views/controls/menu/menu_delegate.h
+++ b/views/controls/menu/menu_delegate.h
@@ -17,12 +17,6 @@
using ui::OSExchangeData;
-namespace gfx {
-
-class Font;
-
-} // namespace gfx
-
namespace views {
class DropTargetEvent;
@@ -59,9 +53,6 @@ class MenuDelegate {
// added with an empty label.
virtual std::wstring GetLabel(int id) const;
- // The font for the menu item label.
- virtual const gfx::Font& GetLabelFont(int id) const;
-
// The tooltip shown for the menu item. This is invoked when the user
// hovers over the item, and no tooltip text has been set for that item.
virtual std::wstring GetTooltipText(int id, const gfx::Point& screen_loc);
diff --git a/views/controls/menu/menu_item_view.cc b/views/controls/menu/menu_item_view.cc
index 9d7b24b..3ff7f2b 100644
--- a/views/controls/menu/menu_item_view.cc
+++ b/views/controls/menu/menu_item_view.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 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.
@@ -456,7 +456,7 @@ void MenuItemView::Layout() {
int MenuItemView::GetAcceleratorTextWidth() {
string16 text = GetAcceleratorText();
- return text.empty() ? 0 : GetFont().GetStringWidth(text);
+ return text.empty() ? 0 : MenuConfig::instance().font.GetStringWidth(text);
}
MenuItemView::MenuItemView(MenuItemView* parent,
@@ -584,15 +584,6 @@ int MenuItemView::GetDrawStringFlags() {
return flags;
}
-const gfx::Font& MenuItemView::GetFont() {
- // Check for item-specific font.
- const MenuDelegate* delegate = GetDelegate();
- if (delegate)
- return delegate->GetLabelFont(GetCommand());
- else
- return MenuConfig::instance().font;
-}
-
void MenuItemView::AddEmptyMenus() {
DCHECK(HasSubmenu());
if (!submenu_->has_children()) {
@@ -632,7 +623,7 @@ void MenuItemView::PaintAccelerator(gfx::Canvas* canvas) {
if (accel_text.empty())
return;
- const gfx::Font& font = GetFont();
+ const gfx::Font& font = MenuConfig::instance().font;
int available_height = height() - GetTopMargin() - GetBottomMargin();
int max_accel_width =
parent_menu_item_->GetSubmenu()->max_accelerator_width();
diff --git a/views/controls/menu/menu_item_view.h b/views/controls/menu/menu_item_view.h
index d94d4ea..980f2d6 100644
--- a/views/controls/menu/menu_item_view.h
+++ b/views/controls/menu/menu_item_view.h
@@ -21,10 +21,6 @@
#include "ui/gfx/native_theme.h"
#endif
-namespace gfx {
-class Font;
-}
-
namespace ui {
class MenuModel;
}
@@ -320,9 +316,6 @@ class MenuItemView : public View {
// Returns the flags passed to DrawStringInt.
int GetDrawStringFlags();
- // Returns the font to use for menu text.
- const gfx::Font& GetFont();
-
// If this menu item has no children a child is added showing it has no
// children. Otherwise AddEmtpyMenus is recursively invoked on child menu
// items that have children.
diff --git a/views/controls/menu/menu_item_view_gtk.cc b/views/controls/menu/menu_item_view_gtk.cc
index aabe58b..17604e4 100644
--- a/views/controls/menu/menu_item_view_gtk.cc
+++ b/views/controls/menu/menu_item_view_gtk.cc
@@ -25,7 +25,7 @@ static const SkColor kSelectedBackgroundColor = SkColorSetRGB(246, 249, 253);
#endif
gfx::Size MenuItemView::CalculatePreferredSize() {
- const gfx::Font& font = GetFont();
+ const gfx::Font& font = MenuConfig::instance().font;
return gfx::Size(
font.GetStringWidth(title_) + label_start_ +
item_right_margin_ + GetChildPreferredWidth(),
@@ -83,7 +83,7 @@ void MenuItemView::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
SkColor fg_color =
IsEnabled() ? TextButton::kEnabledColor : TextButton::kDisabledColor;
#endif
- const gfx::Font& font = GetFont();
+ const gfx::Font& font = MenuConfig::instance().font;
int accel_width = parent_menu_item_->GetSubmenu()->max_accelerator_width();
int width = this->width() - item_right_margin_ - label_start_ - accel_width;
gfx::Rect text_bounds(label_start_, top_margin +
diff --git a/views/controls/menu/menu_item_view_win.cc b/views/controls/menu/menu_item_view_win.cc
index 82f1c0d..e71f8c1 100644
--- a/views/controls/menu/menu_item_view_win.cc
+++ b/views/controls/menu/menu_item_view_win.cc
@@ -18,7 +18,7 @@ using gfx::NativeTheme;
namespace views {
gfx::Size MenuItemView::CalculatePreferredSize() {
- const gfx::Font& font = GetFont();
+ const gfx::Font& font = MenuConfig::instance().font;
return gfx::Size(
font.GetStringWidth(title_) + label_start_ + item_right_margin_ +
GetChildPreferredWidth(),
@@ -82,7 +82,7 @@ void MenuItemView::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
SkColor fg_color = gfx::NativeThemeWin::instance()->GetThemeColorWithDefault(
gfx::NativeThemeWin::MENU, MENU_POPUPITEM, state, TMT_TEXTCOLOR,
default_sys_color);
- const gfx::Font& font = GetFont();
+ const gfx::Font& font = MenuConfig::instance().font;
int accel_width = parent_menu_item_->GetSubmenu()->max_accelerator_width();
int width = this->width() - item_right_margin_ - label_start_ - accel_width;
gfx::Rect text_bounds(label_start_, top_margin, width, font.GetHeight());