diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-28 19:36:46 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-28 19:36:46 +0000 |
commit | d1ba5b511c94fb02b79ceae67a172d6bdb716e98 (patch) | |
tree | 23288e2b27f8513e76e5f22750f3166c92edaae5 /chrome/browser/views/bookmark_menu_controller_views.cc | |
parent | d3038bf0175f75df75385a17298134a2ee39b8a1 (diff) | |
download | chromium_src-d1ba5b511c94fb02b79ceae67a172d6bdb716e98.zip chromium_src-d1ba5b511c94fb02b79ceae67a172d6bdb716e98.tar.gz chromium_src-d1ba5b511c94fb02b79ceae67a172d6bdb716e98.tar.bz2 |
Makes it so deleting a bookmark from the context menu doesn't close
the bookmark menu.
BUG=2469
TEST=click on a bookmark folder on the bookmark bar, right click on an
item and chose delete. Make sure the folder stays up and still works
correctly (and the item you deleted isn't there).
Review URL: http://codereview.chromium.org/551178
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37424 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/views/bookmark_menu_controller_views.cc')
-rw-r--r-- | chrome/browser/views/bookmark_menu_controller_views.cc | 81 |
1 files changed, 79 insertions, 2 deletions
diff --git a/chrome/browser/views/bookmark_menu_controller_views.cc b/chrome/browser/views/bookmark_menu_controller_views.cc index 5a7256b..cb334ed 100644 --- a/chrome/browser/views/bookmark_menu_controller_views.cc +++ b/chrome/browser/views/bookmark_menu_controller_views.cc @@ -206,6 +206,7 @@ bool BookmarkMenuController::ShowContextMenu(MenuItemView* source, nodes[0]->GetParent(), nodes, BookmarkContextMenuController::BOOKMARK_BAR)); + context_menu_->set_observer(this); context_menu_->RunMenuAt(gfx::Point(x, y)); context_menu_.reset(NULL); return true; @@ -271,8 +272,7 @@ void BookmarkMenuController::BookmarkModelChanged() { void BookmarkMenuController::BookmarkNodeFavIconLoaded( BookmarkModel* model, const BookmarkNode* node) { - std::map<const BookmarkNode*, int>::iterator menu_pair = - node_to_menu_id_map_.find(node); + NodeToMenuIDMap::iterator menu_pair = node_to_menu_id_map_.find(node); if (menu_pair == node_to_menu_id_map_.end()) return; // We're not showing a menu item for the node. @@ -287,6 +287,19 @@ void BookmarkMenuController::BookmarkNodeFavIconLoaded( } } +void BookmarkMenuController::WillRemoveBookmarks( + const std::vector<const BookmarkNode*>& bookmarks) { + std::set<MenuItemView*> removed_menus; + + WillRemoveBookmarksImpl(bookmarks, &removed_menus); + + STLDeleteElements(&removed_menus); +} + +void BookmarkMenuController::DidRemoveBookmarks() { + profile_->GetBookmarkModel()->AddObserver(this); +} + MenuItemView* BookmarkMenuController::CreateMenu(const BookmarkNode* parent, int start_child_index) { MenuItemView* menu = new MenuItemView(this); @@ -337,6 +350,7 @@ void BookmarkMenuController::BuildMenu(const BookmarkNode* parent, GetBitmapNamed(IDR_BOOKMARK_BAR_FOLDER); MenuItemView* submenu = menu->AppendSubMenuWithIcon(id, node->GetTitle(), *folder_icon); + node_to_menu_id_map_[node] = id; BuildMenu(node, 0, submenu, next_menu_id); } else { NOTREACHED(); @@ -351,3 +365,66 @@ BookmarkMenuController::~BookmarkMenuController() { observer_->BookmarkMenuDeleted(this); STLDeleteValues(&node_to_menu_map_); } + +MenuItemView* BookmarkMenuController::GetMenuByID(int id) { + for (NodeToMenuMap::const_iterator i = node_to_menu_map_.begin(); + i != node_to_menu_map_.end(); ++i) { + MenuItemView* menu = i->second->GetMenuItemByID(id); + if (menu) + return menu; + } + return NULL; +} + +void BookmarkMenuController::WillRemoveBookmarksImpl( + const std::vector<const BookmarkNode*>& bookmarks, + std::set<views::MenuItemView*>* removed_menus) { + // Remove the observer so that when the remove happens we don't prematurely + // cancel the menu. + profile_->GetBookmarkModel()->RemoveObserver(this); + + // Remove the menu items. + std::set<MenuItemView*> changed_parent_menus; + for (std::vector<const BookmarkNode*>::const_iterator i = bookmarks.begin(); + i != bookmarks.end(); ++i) { + NodeToMenuIDMap::iterator node_to_menu = node_to_menu_id_map_.find(*i); + if (node_to_menu != node_to_menu_id_map_.end()) { + MenuItemView* menu = GetMenuByID(node_to_menu->second); + DCHECK(menu); // If there an entry in node_to_menu_id_map_, there should + // be a menu. + removed_menus->insert(menu); + changed_parent_menus.insert(menu->GetParentMenuItem()); + menu->GetParent()->RemoveChildView(menu); + node_to_menu_id_map_.erase(node_to_menu); + } + } + + // All the bookmarks in |bookmarks| should have the same parent. It's possible + // to support different parents, but this would need to prune any nodes whose + // parent has been removed. As all nodes currently have the same parent, there + // is the DCHECK. + DCHECK(changed_parent_menus.size() <= 1); + + for (std::set<MenuItemView*>::const_iterator i = changed_parent_menus.begin(); + i != changed_parent_menus.end(); ++i) { + (*i)->ChildrenChanged(); + } + + // Remove any descendants of the removed nodes in node_to_menu_id_map_. + for (NodeToMenuIDMap::iterator i = node_to_menu_id_map_.begin(); + i != node_to_menu_id_map_.end(); ) { + bool ancestor_removed = false; + for (std::vector<const BookmarkNode*>::const_iterator j = bookmarks.begin(); + j != bookmarks.end(); ++j) { + if (i->first->HasAncestor(*j)) { + ancestor_removed = true; + break; + } + } + if (ancestor_removed) { + node_to_menu_id_map_.erase(i++); + } else { + ++i; + } + } +} |