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

Unified Diff: chrome/browser/chromeos/status/network_menu.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: Fixes for merge with trunk. 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/network_menu.cc
diff --git a/chrome/browser/chromeos/status/network_menu.cc b/chrome/browser/chromeos/status/network_menu.cc
index ea7faa7ee087ca38d23f55b7f5f63f4e2d30498c..e57dad18a50b247dad93bcbbad8710ef4bb0063d 100644
--- a/chrome/browser/chromeos/status/network_menu.cc
+++ b/chrome/browser/chromeos/status/network_menu.cc
@@ -22,9 +22,13 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/skbitmap_operations.h"
-#include "views/controls/menu/menu_2.h"
+#include "views/controls/menu/menu_item_view.h"
+#include "views/controls/menu/submenu_view.h"
+#include "views/widget/widget.h"
#include "views/window/window.h"
+using views::MenuItemView;
+
namespace chromeos {
////////////////////////////////////////////////////////////////////////////////
@@ -77,7 +81,8 @@ SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages];
NetworkMenu::NetworkMenu()
: min_width_(-1) {
- network_menu_.reset(new views::Menu2(this));
+ network_menu_.reset(new views::MenuItemView(this));
+ network_menu_->set_has_icons(true);
}
NetworkMenu::~NetworkMenu() {
@@ -147,50 +152,43 @@ bool NetworkMenu::ConnectToNetworkAt(int index,
}
////////////////////////////////////////////////////////////////////////////////
-// NetworkMenu, ui::MenuModel implementation:
-
-int NetworkMenu::GetItemCount() const {
- return static_cast<int>(menu_items_.size());
-}
-
-ui::MenuModel::ItemType NetworkMenu::GetTypeAt(int index) const {
- return menu_items_[index].type;
-}
-
-string16 NetworkMenu::GetLabelAt(int index) const {
- return menu_items_[index].label;
+// NetworkMenu, views::MenuItemView implementation:
+std::wstring NetworkMenu::GetLabel(int id) const {
+ DCHECK_LE(0,id);
oshima 2011/04/15 17:40:12 space after , . same for the rest
rhashimoto 2011/04/15 21:20:27 Done.
+ DCHECK_GT(static_cast<int>(menu_items_.size()),id);
+ return UTF16ToWide(menu_items_[id].label);
}
-const gfx::Font* NetworkMenu::GetLabelFontAt(int index) const {
- return (menu_items_[index].flags & FLAG_ASSOCIATED) ?
+const gfx::Font* NetworkMenu::GetLabelFont(int id) const
+{
+ return (menu_items_[id].flags & FLAG_ASSOCIATED) ?
&ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) :
- NULL;
+ &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
}
-bool NetworkMenu::IsItemCheckedAt(int index) const {
- // All ui::MenuModel::TYPE_CHECK menu items are checked.
- return true;
+bool NetworkMenu::IsCommandEnabled(int id) const {
+ if (id < static_cast<int>(menu_items_.size()))
+ return !(menu_items_[id].flags & FLAG_DISABLED);
+ else
+ return false;
}
-bool NetworkMenu::GetIconAt(int index, SkBitmap* icon) {
- if (!menu_items_[index].icon.empty()) {
- *icon = menu_items_[index].icon;
- return true;
- }
- return false;
+bool NetworkMenu::IsItemChecked(int id) const {
+ DCHECK_LE(0,id);
+ DCHECK_GT(static_cast<int>(menu_items_.size()),id);
+ return menu_items_[id].type == ui::MenuModel::TYPE_CHECK;
}
-bool NetworkMenu::IsEnabledAt(int index) const {
- return !(menu_items_[index].flags & FLAG_DISABLED);
-}
+void NetworkMenu::ExecuteCommand(int id) {
+ DCHECK_LE(0,id);
+ DCHECK_GT(static_cast<int>(menu_items_.size()),id);
-void NetworkMenu::ActivatedAt(int index) {
// When we are refreshing the menu, ignore menu item activation.
if (refreshing_menu_)
return;
NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
- int flags = menu_items_[index].flags;
+ int flags = menu_items_[id].flags;
if (flags & FLAG_OPTIONS) {
OpenButtonOptions();
} else if (flags & FLAG_TOGGLE_ETHERNET) {
@@ -208,30 +206,44 @@ void NetworkMenu::ActivatedAt(int index) {
ShowTabbedNetworkSettings(cros->ethernet_network());
}
} else if (flags & FLAG_WIFI) {
- ConnectToNetworkAt(index, std::string(), std::string(), -1);
+ ConnectToNetworkAt(id, std::string(), std::string(), -1);
} else if (flags & FLAG_OTHER_WIFI_NETWORK) {
- ConnectToNetworkAt(index, std::string(), std::string(), -1);
+ ConnectToNetworkAt(id, std::string(), std::string(), -1);
} else if (flags & FLAG_CELLULAR) {
- ConnectToNetworkAt(index, std::string(), std::string(), -1);
+ ConnectToNetworkAt(id, std::string(), std::string(), -1);
} else if (flags & FLAG_OTHER_CELLULAR_NETWORK) {
- ConnectToNetworkAt(index, std::string(), std::string(), -1);
+ ConnectToNetworkAt(id, std::string(), std::string(), -1);
}
}
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_->CancelMenu();
+ network_menu_->Cancel();
}
void NetworkMenu::UpdateMenu() {
refreshing_menu_ = true;
+
+ // Rebuild list of available networks.
InitMenuItems();
- network_menu_->Rebuild();
+
+ // Clear and repopulate MenuItemView.
+ network_menu_->CreateSubmenu()->RemoveAllChildViews(true);
+ for (int i = 0, n = menu_items_.size(); i < n; ++i) {
+ if (menu_items_[i].type == ui::MenuModel::TYPE_SEPARATOR)
+ network_menu_->AppendSeparator();
+ else
+ network_menu_->AppendMenuItemWithIcon(
+ i,
+ UTF16ToWide(menu_items_[i].label),
+ menu_items_[i].icon);
+ }
+
+ network_menu_->ChildrenChanged();
refreshing_menu_ = false;
}
@@ -414,22 +426,41 @@ SkBitmap NetworkMenu::IconForDisplay(const SkBitmap* icon,
// NetworkMenu, views::ViewMenuDelegate implementation:
void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) {
- refreshing_menu_ = true;
+ DCHECK_EQ(GetMenuButton(),source);
+
NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
cros->RequestNetworkScan();
- // Build initial menu items. They will be updated when UpdateMenu is
- // called from NetworkChanged.
- InitMenuItems();
- network_menu_->Rebuild();
+ 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;
+ 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
+ }
- // 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);
+ 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);
}
void NetworkMenu::InitMenuItems() {

Powered by Google App Engine
This is Rietveld 408576698