diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-08 18:07:17 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-08 18:07:17 +0000 |
commit | 6777a5795196c21e69169695685191e2df0eb5dd (patch) | |
tree | 866e21158cba150ef6d010ec7dc557ed14451bf9 /chrome/browser/chromeos/views | |
parent | 81f85b378c9d8c7c3e23233a78d28da785c97a9a (diff) | |
download | chromium_src-6777a5795196c21e69169695685191e2df0eb5dd.zip chromium_src-6777a5795196c21e69169695685191e2df0eb5dd.tar.gz chromium_src-6777a5795196c21e69169695685191e2df0eb5dd.tar.bz2 |
Refactor domui menu to make extending easier
* Moved the code to build JS model to MenuUI.
* Added ModelUpdated/CreateMenuItem/AddCustomConfigValues for subclass to extends behavior.
* Uses the actual icon size to compute left padding
* Adjusted css padding parameters to match the mock (http://www.corp.google.com/~roma/design/chrome/native-widgets/menus/menus-spec.png)
wrech_menu.{cc,h} not used yet, but added for Steven to look at it.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/3613015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@61981 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/views')
-rw-r--r-- | chrome/browser/chromeos/views/domui_menu_widget.cc | 2 | ||||
-rw-r--r-- | chrome/browser/chromeos/views/native_menu_domui.cc | 122 | ||||
-rw-r--r-- | chrome/browser/chromeos/views/native_menu_domui.h | 19 |
3 files changed, 33 insertions, 110 deletions
diff --git a/chrome/browser/chromeos/views/domui_menu_widget.cc b/chrome/browser/chromeos/views/domui_menu_widget.cc index 50b720c..82a66a6 100644 --- a/chrome/browser/chromeos/views/domui_menu_widget.cc +++ b/chrome/browser/chromeos/views/domui_menu_widget.cc @@ -243,7 +243,7 @@ void DOMUIMenuWidget::ShowAt(chromeos::MenuLocator* locator) { container->set_border(new RoundedBorder(locator)); container->SetLayoutManager(new InsetsLayout()); SetContentsView(container); - dom_view_->LoadURL(GURL("chrome://menu")); + dom_view_->LoadURL(domui_menu_->menu_url()); } else { domui_menu_->UpdateStates(); dom_view_->GetParent()->set_border(new RoundedBorder(locator)); diff --git a/chrome/browser/chromeos/views/native_menu_domui.cc b/chrome/browser/chromeos/views/native_menu_domui.cc index 4477bf6..42c6482 100644 --- a/chrome/browser/chromeos/views/native_menu_domui.cc +++ b/chrome/browser/chromeos/views/native_menu_domui.cc @@ -7,24 +7,16 @@ #include <string> #include "app/menus/menu_model.h" -#include "base/json/json_writer.h" #include "base/message_loop.h" -#include "base/string_number_conversions.h" #include "base/string_util.h" -#include "base/utf_string_conversions.h" -#include "base/values.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/chromeos/views/domui_menu_widget.h" #include "chrome/browser/chromeos/views/menu_locator.h" -#include "chrome/browser/dom_ui/dom_ui_util.h" #include "chrome/browser/profile_manager.h" -#include "gfx/favicon_size.h" -#include "gfx/font.h" +#include "chrome/common/url_constants.h" #include "gfx/rect.h" -#include "third_party/skia/include/core/SkBitmap.h" #include "views/controls/menu/menu_2.h" -#include "views/controls/menu/menu_config.h" namespace { @@ -35,34 +27,6 @@ bool MenuTypeCanExecute(menus::MenuModel::ItemType type) { type == menus::MenuModel::TYPE_RADIO; } -// A utility function that generates css font property from gfx::Font. -std::wstring GetFontShorthand(const gfx::Font* font) { - std::wstring out; - if (font == NULL) { - font = &(views::MenuConfig::instance().font); - } - if (font->GetStyle() & gfx::Font::BOLD) { - out.append(L"bold "); - } - if (font->GetStyle() & gfx::Font::ITALIC) { - out.append(L"italic "); - } - if (font->GetStyle() & gfx::Font::UNDERLINED) { - out.append(L"underline "); - } - - // TODO(oshima): The font size from gfx::Font is too small when - // used in webkit. Figure out the reason. - out.append(ASCIIToWide(base::IntToString(font->GetFontSize() + 4))); - out.append(L"px/"); - out.append(ASCIIToWide(base::IntToString( - std::max(kFavIconSize, font->GetHeight())))); - out.append(L"px \""); - out.append(font->GetFontName()); - out.append(L"\",sans-serif"); - return out; -} - // Currently opened menu. See RunMenuAt for reason why we need this. chromeos::NativeMenuDOMUI* current_ = NULL; @@ -70,6 +34,15 @@ chromeos::NativeMenuDOMUI* current_ = NULL; namespace chromeos { +// static +void NativeMenuDOMUI::SetMenuURL(views::Menu2* menu2, const GURL& url) { + gfx::NativeView native = menu2->GetNativeMenu(); + DCHECK(native); + DOMUIMenuWidget* widget = DOMUIMenuWidget::FindDOMUIMenuWidget(native); + DCHECK(widget); + widget->domui_menu()->set_menu_url(url); +} + //////////////////////////////////////////////////////////////////////////////// // NativeMenuDOMUI, public: @@ -81,7 +54,8 @@ NativeMenuDOMUI::NativeMenuDOMUI(menus::MenuModel* menu_model, bool root) menu_shown_(false), activated_menu_(NULL), activated_index_(-1), - menu_action_(MENU_ACTION_NONE) { + menu_action_(MENU_ACTION_NONE), + menu_url_(StringPrintf("chrome://%s", chrome::kChromeUIMenu)) { menu_widget_ = new DOMUIMenuWidget(this, root); // Set the initial location off the screen not to show small // window with dropshadow. @@ -153,47 +127,7 @@ void NativeMenuDOMUI::CancelMenu() { void NativeMenuDOMUI::Rebuild() { activated_menu_ = NULL; - DictionaryValue model; - ListValue* items = new ListValue(); - model.Set("items", items); - bool has_icon = false; - for (int index = 0; index < model_->GetItemCount(); ++index) { - menus::MenuModel::ItemType type = model_->GetTypeAt(index); - DictionaryValue* item; - switch (type) { - case menus::MenuModel::TYPE_SEPARATOR: - item = CreateMenuItem(index, "separator", &has_icon); - break; - case menus::MenuModel::TYPE_RADIO: - has_icon = true; // all radio buttons has indicator icon. - item = CreateMenuItem(index, "radio", &has_icon); - break; - case menus::MenuModel::TYPE_SUBMENU: - item = CreateMenuItem(index, "submenu", &has_icon); - break; - case menus::MenuModel::TYPE_COMMAND: - item = CreateMenuItem(index, "command", &has_icon); - break; - case menus::MenuModel::TYPE_CHECK: - item = CreateMenuItem(index, "check", &has_icon); - break; - default: - // TODO(oshima): We don't support BUTTOM_ITEM for now. - // I haven't decided how to implement zoom/cut&paste - // stuff, but may do somethign similar to what linux_views - // does. - NOTREACHED(); - continue; - } - items->Set(index, item); - } - model.SetBoolean("hasIcon", has_icon); - model.SetBoolean("isRoot", menu_widget_->is_root()); - - std::string json_model; - base::JSONWriter::Write(&model, false, &json_model); - std::wstring script = UTF8ToWide("updateModel(" + json_model + ")"); - menu_widget_->ExecuteJavascript(script); + menu_widget_->ExecuteJavascript(L"modelUpdated()"); } void NativeMenuDOMUI::UpdateStates() { @@ -202,8 +136,7 @@ void NativeMenuDOMUI::UpdateStates() { } gfx::NativeMenu NativeMenuDOMUI::GetNativeMenu() const { - NOTREACHED(); - return NULL; + return menu_widget_->GetNativeView(); } NativeMenuDOMUI::MenuAction NativeMenuDOMUI::GetMenuAction() const { @@ -277,6 +210,7 @@ void NativeMenuDOMUI::OpenSubmenu(int index, int y) { // Returns the model for the submenu at the specified index. menus::MenuModel* submenu = model_->GetSubmenuModelAt(index); submenu_.reset(new chromeos::NativeMenuDOMUI(submenu, false)); + submenu_->set_menu_url(menu_url_); // y in menu_widget_ coordinate. submenu_->set_parent(this); submenu_->ShowAt( @@ -310,7 +244,9 @@ void NativeMenuDOMUI::MoveInputToParent() { } void NativeMenuDOMUI::OnLoad() { - Rebuild(); + // TODO(oshima): OnLoad is no longer used, but kept in case + // we may need it. Delete this if this is not necessary to + // implement wrench/network/bookmark menus. } void NativeMenuDOMUI::SetSize(const gfx::Size& size) { @@ -363,28 +299,6 @@ void NativeMenuDOMUI::ProcessActivate() { } } -DictionaryValue* NativeMenuDOMUI::CreateMenuItem( - int index, const char* type, bool* has_icon_out) { - // Note: DOM UI uses '&' as mnemonic. - string16 label16 = model_->GetLabelAt(index); - DictionaryValue* item = new DictionaryValue(); - - item->SetString("type", type); - item->SetString("label", label16); - item->SetBoolean("enabled", model_->IsEnabledAt(index)); - item->SetBoolean("visible", model_->IsVisibleAt(index)); - item->SetBoolean("checked", model_->IsItemCheckedAt(index)); - item->SetInteger("command_id", model_->GetCommandIdAt(index)); - item->SetString( - "font", WideToUTF16(GetFontShorthand(model_->GetLabelFontAt(index)))); - SkBitmap icon; - if (model_->GetIconAt(index, &icon) && !icon.isNull() && !icon.empty()) { - item->SetString("icon", dom_ui_util::GetImageDataUrl(icon)); - *has_icon_out = true; - } - return item; -} - void NativeMenuDOMUI::ShowAt(MenuLocator* locator) { model_->MenuWillShow(); menu_widget_->ShowAt(locator); diff --git a/chrome/browser/chromeos/views/native_menu_domui.h b/chrome/browser/chromeos/views/native_menu_domui.h index cf4786e..d000985 100644 --- a/chrome/browser/chromeos/views/native_menu_domui.h +++ b/chrome/browser/chromeos/views/native_menu_domui.h @@ -11,6 +11,7 @@ #include "base/message_loop.h" #include "base/observer_list.h" #include "chrome/browser/chromeos/dom_ui/domui_menu_control.h" +#include "googleurl/src/gurl.h" #include "views/controls/menu/menu_wrapper.h" class SkBitmap; @@ -77,16 +78,19 @@ class NativeMenuDOMUI : public views::MenuWrapper, // Returns the profile to create DOMView. Profile* GetProfile(); + // Sets/Gets the url for the domui menu. + void set_menu_url(const GURL& url) { menu_url_ = url; } + const GURL& menu_url() const { return menu_url_; } + + // Sets the menu url of menu2. This has to be called before + // RunMenuAt/RunContextMenuAt is called. + static void SetMenuURL(views::Menu2* menu2, const GURL& url); + private: // Callback that we should really process the menu activation. // See description above class for why we delay processing activation. void ProcessActivate(); - // Creates a menu item for the menu item at index. - DictionaryValue* CreateMenuItem(int index, - const char* type, - bool* has_icon_out); - // Show the menu using given |locator|. void ShowAt(MenuLocator* locator); @@ -124,6 +128,11 @@ class NativeMenuDOMUI : public views::MenuWrapper, // Vector of listeners to receive callbacks when the menu opens. ObserverList<views::MenuListener> listeners_; + // URL to invoke Menu DOMUI. Default menu is chrome://menu, but + // custom menu can use different url using SetMenuURL method + // (e.g. chrome://wrench-menu for wrench menu). + GURL menu_url_; + DISALLOW_COPY_AND_ASSIGN(NativeMenuDOMUI); }; |