| Index: trunk/src/chrome/browser/ui/views/wrench_menu.cc
|
| ===================================================================
|
| --- trunk/src/chrome/browser/ui/views/wrench_menu.cc (revision 227696)
|
| +++ trunk/src/chrome/browser/ui/views/wrench_menu.cc (working copy)
|
| @@ -20,7 +20,6 @@
|
| #include "chrome/browser/ui/browser.h"
|
| #include "chrome/browser/ui/browser_window.h"
|
| #include "chrome/browser/ui/tabs/tab_strip_model.h"
|
| -#include "chrome/browser/ui/toolbar/wrench_menu_model.h"
|
| #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
|
| #include "chrome/browser/ui/views/wrench_menu_observer.h"
|
| #include "content/public/browser/host_zoom_map.h"
|
| @@ -89,18 +88,6 @@
|
| // Menu items which have embedded buttons should have this height in pixel.
|
| const int kMenuItemContainingButtonsHeight = 43;
|
|
|
| -// Returns true if |command_id| identifies a bookmark menu item.
|
| -bool IsBookmarkCommand(int command_id) {
|
| - return command_id >= WrenchMenuModel::kMinBookmarkCommandId &&
|
| - command_id <= WrenchMenuModel::kMaxBookmarkCommandId;
|
| -}
|
| -
|
| -// Returns true if |command_id| identifies a recent tabs menu item.
|
| -bool IsRecentTabsCommand(int command_id) {
|
| - return command_id >= WrenchMenuModel::kMinRecentTabsCommandId &&
|
| - command_id <= WrenchMenuModel::kMaxRecentTabsCommandId;
|
| -}
|
| -
|
| // Subclass of ImageButton whose preferred size includes the size of the border.
|
| class FullscreenButton : public ImageButton {
|
| public:
|
| @@ -424,8 +411,8 @@
|
| public:
|
| // Constructor for use with button containing menu items which have a
|
| // different height then normal items.
|
| - ButtonContainerMenuItemView(MenuItemView* parent, int command_id, int height)
|
| - : MenuItemView(parent, command_id, MenuItemView::NORMAL),
|
| + ButtonContainerMenuItemView(MenuItemView* parent, int id, int height)
|
| + : MenuItemView(parent, id, MenuItemView::NORMAL),
|
| height_(height) {
|
| };
|
|
|
| @@ -777,7 +764,10 @@
|
|
|
| // ui::MenuModelDelegate implementation:
|
| virtual void OnIconChanged(int index) OVERRIDE {
|
| - int command_id = model_->GetCommandIdAt(index);
|
| + // |index| specifies position in children items of |menu_item_| starting at
|
| + // 0, its corresponding command id as used in the children menu item views
|
| + // follows that of the parent menu item view |menu_item_|.
|
| + int command_id = menu_item_->GetCommand() + 1 + index;
|
| views::MenuItemView* item = menu_item_->GetMenuItemByID(command_id);
|
| DCHECK(item);
|
| gfx::Image icon;
|
| @@ -792,7 +782,9 @@
|
| if (!submenu)
|
| return -1;
|
| const int kMaxMenuItemWidth = 320;
|
| - return menu->GetCommand() == menu_item_->GetCommand() ?
|
| + return menu->GetCommand() >= menu_item_->GetCommand() &&
|
| + menu->GetCommand() <=
|
| + menu_item_->GetCommand() + submenu->GetMenuItemCount() ?
|
| kMaxMenuItemWidth : -1;
|
| }
|
|
|
| @@ -800,11 +792,11 @@
|
| return model_->GetLabelFontAt(index);
|
| }
|
|
|
| - bool GetForegroundColorAt(int index,
|
| - bool is_hovered,
|
| - SkColor* override_color) const {
|
| + bool GetForegroundColor(int command_id,
|
| + bool is_hovered,
|
| + SkColor* override_color) const {
|
| // The items for which we get a font, should be shown in black.
|
| - if (GetLabelFontAt(index)) {
|
| + if (GetLabelFontAt(command_id)) {
|
| *override_color = SK_ColorBLACK;
|
| return true;
|
| }
|
| @@ -829,6 +821,9 @@
|
| selected_index_(0),
|
| bookmark_menu_(NULL),
|
| feedback_menu_item_(NULL),
|
| + first_bookmark_command_id_(0),
|
| + first_recent_tabs_command_id_(-1),
|
| + last_recent_tabs_command_id_(-1),
|
| use_new_menu_(use_new_menu),
|
| supports_new_separators_(supports_new_separators) {
|
| registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
|
| @@ -850,15 +845,9 @@
|
| root_ = new MenuItemView(this);
|
| root_->set_has_icons(true); // We have checks, radios and icons, set this
|
| // so we get the taller menu style.
|
| - PopulateMenu(root_, model);
|
| -
|
| -#if defined(DEBUG)
|
| - // Verify that the reserved command ID's for bookmarks menu are not used.
|
| - for (int i = WrenchMenuModel:kMinBookmarkCommandId;
|
| - i <= WrenchMenuModel::kMaxBookmarkCommandId; ++i)
|
| - DCHECK(command_id_to_entry_.find(i) == command_id_to_entry_.end());
|
| -#endif // defined(DEBUG)
|
| -
|
| + int next_id = 1;
|
| + PopulateMenu(root_, model, &next_id);
|
| + first_bookmark_command_id_ = next_id + 1;
|
| menu_runner_.reset(new views::MenuRunner(root_));
|
| }
|
|
|
| @@ -905,10 +894,10 @@
|
| observer_list_.RemoveObserver(observer);
|
| }
|
|
|
| -const gfx::Font* WrenchMenu::GetLabelFont(int command_id) const {
|
| - if (IsRecentTabsCommand(command_id)) {
|
| +const gfx::Font* WrenchMenu::GetLabelFont(int index) const {
|
| + if (is_recent_tabs_command(index)) {
|
| return recent_tabs_menu_model_delegate_->GetLabelFontAt(
|
| - ModelIndexFromCommandId(command_id));
|
| + index - first_recent_tabs_command_id_);
|
| }
|
| return NULL;
|
| }
|
| @@ -916,22 +905,24 @@
|
| bool WrenchMenu::GetForegroundColor(int command_id,
|
| bool is_hovered,
|
| SkColor* override_color) const {
|
| - if (IsRecentTabsCommand(command_id)) {
|
| - return recent_tabs_menu_model_delegate_->GetForegroundColorAt(
|
| - ModelIndexFromCommandId(command_id), is_hovered, override_color);
|
| + if (is_recent_tabs_command(command_id)) {
|
| + return recent_tabs_menu_model_delegate_->GetForegroundColor(
|
| + command_id - first_recent_tabs_command_id_,
|
| + is_hovered,
|
| + override_color);
|
| }
|
| return false;
|
| }
|
|
|
| -string16 WrenchMenu::GetTooltipText(int command_id,
|
| +string16 WrenchMenu::GetTooltipText(int id,
|
| const gfx::Point& p) const {
|
| - return IsBookmarkCommand(command_id) ?
|
| - bookmark_menu_delegate_->GetTooltipText(command_id, p) : string16();
|
| + return is_bookmark_command(id) ?
|
| + bookmark_menu_delegate_->GetTooltipText(id, p) : string16();
|
| }
|
|
|
| bool WrenchMenu::IsTriggerableEvent(views::MenuItemView* menu,
|
| const ui::Event& e) {
|
| - return IsBookmarkCommand(menu->GetCommand()) ?
|
| + return is_bookmark_command(menu->GetCommand()) ?
|
| bookmark_menu_delegate_->IsTriggerableEvent(menu, e) :
|
| MenuDelegate::IsTriggerableEvent(menu, e);
|
| }
|
| @@ -962,7 +953,7 @@
|
| MenuItemView* item,
|
| const ui::DropTargetEvent& event,
|
| DropPosition* position) {
|
| - return IsBookmarkCommand(item->GetCommand()) ?
|
| + return is_bookmark_command(item->GetCommand()) ?
|
| bookmark_menu_delegate_->GetDropOperation(item, event, position) :
|
| ui::DragDropTypes::DRAG_NONE;
|
| }
|
| @@ -970,7 +961,7 @@
|
| int WrenchMenu::OnPerformDrop(MenuItemView* menu,
|
| DropPosition position,
|
| const ui::DropTargetEvent& event) {
|
| - if (!IsBookmarkCommand(menu->GetCommand()))
|
| + if (!is_bookmark_command(menu->GetCommand()))
|
| return ui::DragDropTypes::DRAG_NONE;
|
|
|
| int result = bookmark_menu_delegate_->OnPerformDrop(menu, position, event);
|
| @@ -978,34 +969,34 @@
|
| }
|
|
|
| bool WrenchMenu::ShowContextMenu(MenuItemView* source,
|
| - int command_id,
|
| + int id,
|
| const gfx::Point& p,
|
| ui::MenuSourceType source_type) {
|
| - return IsBookmarkCommand(command_id) ?
|
| - bookmark_menu_delegate_->ShowContextMenu(source, command_id, p,
|
| + return is_bookmark_command(id) ?
|
| + bookmark_menu_delegate_->ShowContextMenu(source, id, p,
|
| source_type) :
|
| false;
|
| }
|
|
|
| bool WrenchMenu::CanDrag(MenuItemView* menu) {
|
| - return IsBookmarkCommand(menu->GetCommand()) ?
|
| + return is_bookmark_command(menu->GetCommand()) ?
|
| bookmark_menu_delegate_->CanDrag(menu) : false;
|
| }
|
|
|
| void WrenchMenu::WriteDragData(MenuItemView* sender,
|
| ui::OSExchangeData* data) {
|
| - DCHECK(IsBookmarkCommand(sender->GetCommand()));
|
| + DCHECK(is_bookmark_command(sender->GetCommand()));
|
| return bookmark_menu_delegate_->WriteDragData(sender, data);
|
| }
|
|
|
| int WrenchMenu::GetDragOperations(MenuItemView* sender) {
|
| - return IsBookmarkCommand(sender->GetCommand()) ?
|
| + return is_bookmark_command(sender->GetCommand()) ?
|
| bookmark_menu_delegate_->GetDragOperations(sender) :
|
| MenuDelegate::GetDragOperations(sender);
|
| }
|
|
|
| int WrenchMenu::GetMaxWidthForMenu(MenuItemView* menu) {
|
| - if (IsBookmarkCommand(menu->GetCommand()))
|
| + if (is_bookmark_command(menu->GetCommand()))
|
| return bookmark_menu_delegate_->GetMaxWidthForMenu(menu);
|
| int max_width = -1;
|
| // If recent tabs menu is available, it will decide if |menu| is one of recent
|
| @@ -1018,37 +1009,40 @@
|
| return max_width;
|
| }
|
|
|
| -bool WrenchMenu::IsItemChecked(int command_id) const {
|
| - if (IsBookmarkCommand(command_id))
|
| +bool WrenchMenu::IsItemChecked(int id) const {
|
| + if (is_bookmark_command(id))
|
| return false;
|
|
|
| - const Entry& entry = command_id_to_entry_.find(command_id)->second;
|
| + const Entry& entry = id_to_entry_.find(id)->second;
|
| return entry.first->IsItemCheckedAt(entry.second);
|
| }
|
|
|
| -bool WrenchMenu::IsCommandEnabled(int command_id) const {
|
| - if (IsBookmarkCommand(command_id))
|
| +bool WrenchMenu::IsCommandEnabled(int id) const {
|
| + if (is_bookmark_command(id))
|
| return true;
|
|
|
| - if (command_id == 0)
|
| + if (id == 0)
|
| return false; // The root item.
|
|
|
| + const Entry& entry = id_to_entry_.find(id)->second;
|
| + int command_id = entry.first->GetCommandIdAt(entry.second);
|
| // The items representing the cut menu (cut/copy/paste) and zoom menu
|
| // (increment/decrement/reset) are always enabled. The child views of these
|
| // items enabled state updates appropriately.
|
| - if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS)
|
| - return true;
|
| -
|
| - const Entry& entry = command_id_to_entry_.find(command_id)->second;
|
| - return entry.first->IsEnabledAt(entry.second);
|
| + return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
|
| + entry.first->IsEnabledAt(entry.second);
|
| }
|
|
|
| -void WrenchMenu::ExecuteCommand(int command_id, int mouse_event_flags) {
|
| - if (IsBookmarkCommand(command_id)) {
|
| - bookmark_menu_delegate_->ExecuteCommand(command_id, mouse_event_flags);
|
| +void WrenchMenu::ExecuteCommand(int id, int mouse_event_flags) {
|
| + if (is_bookmark_command(id)) {
|
| + bookmark_menu_delegate_->ExecuteCommand(id, mouse_event_flags);
|
| return;
|
| }
|
|
|
| + // Not a bookmark
|
| + const Entry& entry = id_to_entry_.find(id)->second;
|
| + int command_id = entry.first->GetCommandIdAt(entry.second);
|
| +
|
| if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
|
| // These items are represented by child views. If ExecuteCommand is invoked
|
| // it means the user clicked on the area around the buttons and we should
|
| @@ -1056,21 +1050,25 @@
|
| return;
|
| }
|
|
|
| - const Entry& entry = command_id_to_entry_.find(command_id)->second;
|
| return entry.first->ActivatedAt(entry.second, mouse_event_flags);
|
| }
|
|
|
| -bool WrenchMenu::GetAccelerator(int command_id, ui::Accelerator* accelerator) {
|
| - if (IsBookmarkCommand(command_id))
|
| +bool WrenchMenu::GetAccelerator(int id, ui::Accelerator* accelerator) {
|
| + if (is_bookmark_command(id))
|
| return false;
|
| + IDToEntry::iterator ix = id_to_entry_.find(id);
|
| + if (ix == id_to_entry_.end()) {
|
| + // There is no entry for this id.
|
| + return false;
|
| + }
|
|
|
| + const Entry& entry = ix->second;
|
| + int command_id = entry.first->GetCommandIdAt(entry.second);
|
| if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
|
| // These have special child views; don't show the accelerator for them.
|
| return false;
|
| }
|
|
|
| - CommandIDToEntry::iterator ix = command_id_to_entry_.find(command_id);
|
| - const Entry& entry = ix->second;
|
| ui::Accelerator menu_accelerator;
|
| if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
|
| return false;
|
| @@ -1120,7 +1118,8 @@
|
| }
|
|
|
| void WrenchMenu::PopulateMenu(MenuItemView* parent,
|
| - MenuModel* model) {
|
| + MenuModel* model,
|
| + int* next_id) {
|
| for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
|
| // The button container menu items have a special height which we have to
|
| // use instead of the normal height.
|
| @@ -1131,10 +1130,17 @@
|
| height = kMenuItemContainingButtonsHeight;
|
|
|
| MenuItemView* item = AppendMenuItem(
|
| - parent, model, i, model->GetTypeAt(i), height);
|
| + parent, model, i, model->GetTypeAt(i), next_id, height);
|
|
|
| - if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU)
|
| - PopulateMenu(item, model->GetSubmenuModelAt(i));
|
| + if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) {
|
| + bool is_recent_tabs_menu =
|
| + model->GetCommandIdAt(i) == IDC_RECENT_TABS_MENU;
|
| + if (is_recent_tabs_menu)
|
| + first_recent_tabs_command_id_ = *next_id;
|
| + PopulateMenu(item, model->GetSubmenuModelAt(i), next_id);
|
| + if (is_recent_tabs_menu)
|
| + last_recent_tabs_command_id_ = *next_id - 1;
|
| + }
|
|
|
| const ui::NativeTheme* native_theme = GetNativeTheme();
|
|
|
| @@ -1189,34 +1195,23 @@
|
| MenuModel* model,
|
| int index,
|
| MenuModel::ItemType menu_type,
|
| + int* next_id,
|
| int height) {
|
| - int command_id = model->GetCommandIdAt(index);
|
| - DCHECK(command_id > -1 ||
|
| - (command_id == -1 &&
|
| - model->GetTypeAt(index) == MenuModel::TYPE_SEPARATOR));
|
| + int id = (*next_id)++;
|
|
|
| - if (command_id > -1) { // Don't add separators to |command_id_to_entry_|.
|
| - // All command ID's should be unique except for IDC_SHOW_HISTORY which is
|
| - // in both wrench menu and RecentTabs submenu,
|
| - if (command_id != IDC_SHOW_HISTORY) {
|
| - DCHECK(command_id_to_entry_.find(command_id) ==
|
| - command_id_to_entry_.end())
|
| - << "command ID " << command_id << " already exists!";
|
| - }
|
| - command_id_to_entry_[command_id].first = model;
|
| - command_id_to_entry_[command_id].second = index;
|
| - }
|
| + id_to_entry_[id].first = model;
|
| + id_to_entry_[id].second = index;
|
|
|
| MenuItemView* menu_item = NULL;
|
| if (height > 0) {
|
| // For menu items with a special menu height we use our special class to be
|
| // able to modify the item height.
|
| - menu_item = new ButtonContainerMenuItemView(parent, command_id, height);
|
| + menu_item = new ButtonContainerMenuItemView(parent, id, height);
|
| parent->GetSubmenu()->AddChildView(menu_item);
|
| } else {
|
| // For all other cases we use the more generic way to add menu items.
|
| menu_item = views::MenuModelAdapter::AppendMenuItemFromModel(
|
| - model, index, parent, command_id);
|
| + model, index, parent, id);
|
| }
|
|
|
| if (menu_item) {
|
| @@ -1258,8 +1253,7 @@
|
| new BookmarkMenuDelegate(browser_,
|
| browser_,
|
| parent,
|
| - WrenchMenuModel::kMinBookmarkCommandId,
|
| - WrenchMenuModel::kMaxBookmarkCommandId));
|
| + first_bookmark_command_id_));
|
| bookmark_menu_delegate_->Init(this,
|
| bookmark_menu_,
|
| model->bookmark_bar_node(),
|
| @@ -1267,9 +1261,3 @@
|
| BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS,
|
| BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU);
|
| }
|
| -
|
| -int WrenchMenu::ModelIndexFromCommandId(int command_id) const {
|
| - CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id);
|
| - DCHECK(ix != command_id_to_entry_.end());
|
| - return ix->second.second;
|
| -}
|
|
|