| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" | 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 menu_ = CreateMenu(node, start_child_index, show_options); | 110 menu_ = CreateMenu(node, start_child_index, show_options); |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { | 114 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { |
| 115 page_navigator_ = navigator; | 115 page_navigator_ = navigator; |
| 116 if (context_menu_.get()) | 116 if (context_menu_.get()) |
| 117 context_menu_->SetPageNavigator(navigator); | 117 context_menu_->SetPageNavigator(navigator); |
| 118 } | 118 } |
| 119 | 119 |
| 120 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { | 120 const BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() const { |
| 121 return BookmarkModelFactory::GetForProfile(profile_); | 121 return BookmarkModelFactory::GetForProfile(profile_); |
| 122 } | 122 } |
| 123 | 123 |
| 124 bookmarks::ManagedBookmarkService* | 124 bookmarks::ManagedBookmarkService* |
| 125 BookmarkMenuDelegate::GetManagedBookmarkService() { | 125 BookmarkMenuDelegate::GetManagedBookmarkService() { |
| 126 return ManagedBookmarkServiceFactory::GetForProfile(profile_); | 126 return ManagedBookmarkServiceFactory::GetForProfile(profile_); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, | 129 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, |
| 130 int start_index) { | 130 int start_index) { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 bool copy = event.source_operations() == ui::DragDropTypes::DRAG_COPY; | 306 bool copy = event.source_operations() == ui::DragDropTypes::DRAG_COPY; |
| 307 return chrome::DropBookmarks(profile_, drop_data_, | 307 return chrome::DropBookmarks(profile_, drop_data_, |
| 308 drop_parent, index_to_drop_at, copy); | 308 drop_parent, index_to_drop_at, copy); |
| 309 } | 309 } |
| 310 | 310 |
| 311 bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source, | 311 bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source, |
| 312 int id, | 312 int id, |
| 313 const gfx::Point& p, | 313 const gfx::Point& p, |
| 314 ui::MenuSourceType source_type) { | 314 ui::MenuSourceType source_type) { |
| 315 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); | 315 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); |
| 316 std::vector<const BookmarkNode*> nodes; | 316 const BookmarkNode* node = menu_id_to_node_map_[id]; |
| 317 nodes.push_back(menu_id_to_node_map_[id]); | 317 std::vector<const BookmarkNode*> nodes(1, node); |
| 318 bool close_on_delete = !parent_menu_item_ && | 318 context_menu_.reset(new BookmarkContextMenu( |
| 319 (nodes[0]->parent() == GetBookmarkModel()->other_node() && | 319 parent_, browser_, profile_, page_navigator_, node->parent(), nodes, |
| 320 nodes[0]->parent()->child_count() == 1); | 320 ShouldCloseOnRemove(node))); |
| 321 context_menu_.reset( | |
| 322 new BookmarkContextMenu( | |
| 323 parent_, | |
| 324 browser_, | |
| 325 profile_, | |
| 326 page_navigator_, | |
| 327 nodes[0]->parent(), | |
| 328 nodes, | |
| 329 close_on_delete)); | |
| 330 context_menu_->set_observer(this); | 321 context_menu_->set_observer(this); |
| 331 context_menu_->RunMenuAt(p, source_type); | 322 context_menu_->RunMenuAt(p, source_type); |
| 332 context_menu_.reset(NULL); | 323 context_menu_.reset(nullptr); |
| 333 return true; | 324 return true; |
| 334 } | 325 } |
| 335 | 326 |
| 336 bool BookmarkMenuDelegate::CanDrag(MenuItemView* menu) { | 327 bool BookmarkMenuDelegate::CanDrag(MenuItemView* menu) { |
| 337 const BookmarkNode* node = menu_id_to_node_map_[menu->GetCommand()]; | 328 const BookmarkNode* node = menu_id_to_node_map_[menu->GetCommand()]; |
| 338 // Don't let users drag the other folder. | 329 // Don't let users drag the other folder. |
| 339 return node->parent() != GetBookmarkModel()->root_node(); | 330 return node->parent() != GetBookmarkModel()->root_node(); |
| 340 } | 331 } |
| 341 | 332 |
| 342 void BookmarkMenuDelegate::WriteDragData(MenuItemView* sender, | 333 void BookmarkMenuDelegate::WriteDragData(MenuItemView* sender, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 (*i)->ChildrenChanged(); | 427 (*i)->ChildrenChanged(); |
| 437 } | 428 } |
| 438 | 429 |
| 439 void BookmarkMenuDelegate::DidRemoveBookmarks() { | 430 void BookmarkMenuDelegate::DidRemoveBookmarks() { |
| 440 // Balances remove in WillRemoveBookmarksImpl. | 431 // Balances remove in WillRemoveBookmarksImpl. |
| 441 GetBookmarkModel()->AddObserver(this); | 432 GetBookmarkModel()->AddObserver(this); |
| 442 DCHECK(is_mutating_model_); | 433 DCHECK(is_mutating_model_); |
| 443 is_mutating_model_ = false; | 434 is_mutating_model_ = false; |
| 444 } | 435 } |
| 445 | 436 |
| 437 bool BookmarkMenuDelegate::ShouldCloseOnRemove(const BookmarkNode* node) const { |
| 438 // We never need to close when embedded in the app menu. |
| 439 const bool is_shown_from_app_menu = parent_menu_item_ != nullptr; |
| 440 if (is_shown_from_app_menu) |
| 441 return false; |
| 442 |
| 443 const bool is_only_child_of_other_folder = |
| 444 (node->parent() == GetBookmarkModel()->other_node() && |
| 445 node->parent()->child_count() == 1); |
| 446 const bool is_child_of_bookmark_bar = |
| 447 node->parent() == GetBookmarkModel()->bookmark_bar_node(); |
| 448 // The 'other' bookmarks folder hides when it has no more items, so we need |
| 449 // to exit the menu when the last node is removed. |
| 450 // If the parent is the bookmark bar, then the menu is showing for an item on |
| 451 // the bookmark bar. When removing this item we need to close the menu (as |
| 452 // there is no longer anything to anchor the menu to). |
| 453 return is_only_child_of_other_folder || is_child_of_bookmark_bar; |
| 454 } |
| 455 |
| 446 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, | 456 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, |
| 447 int start_child_index, | 457 int start_child_index, |
| 448 ShowOptions show_options) { | 458 ShowOptions show_options) { |
| 449 MenuItemView* menu = new MenuItemView(real_delegate_); | 459 MenuItemView* menu = new MenuItemView(real_delegate_); |
| 450 menu->SetCommand(next_menu_id_++); | 460 menu->SetCommand(next_menu_id_++); |
| 451 AddMenuToMaps(menu, parent); | 461 AddMenuToMaps(menu, parent); |
| 452 menu->set_has_icons(true); | 462 menu->set_has_icons(true); |
| 453 bool show_permanent = show_options == SHOW_PERMANENT_FOLDERS; | 463 bool show_permanent = show_options == SHOW_PERMANENT_FOLDERS; |
| 454 if (show_permanent && parent == GetBookmarkModel()->bookmark_bar_node()) { | 464 if (show_permanent && parent == GetBookmarkModel()->bookmark_bar_node()) { |
| 455 BuildMenuForManagedNode(menu); | 465 BuildMenuForManagedNode(menu); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 } | 541 } |
| 532 AddMenuToMaps(child_menu_item, node); | 542 AddMenuToMaps(child_menu_item, node); |
| 533 } | 543 } |
| 534 } | 544 } |
| 535 | 545 |
| 536 void BookmarkMenuDelegate::AddMenuToMaps(MenuItemView* menu, | 546 void BookmarkMenuDelegate::AddMenuToMaps(MenuItemView* menu, |
| 537 const BookmarkNode* node) { | 547 const BookmarkNode* node) { |
| 538 menu_id_to_node_map_[menu->GetCommand()] = node; | 548 menu_id_to_node_map_[menu->GetCommand()] = node; |
| 539 node_to_menu_map_[node] = menu; | 549 node_to_menu_map_[node] = menu; |
| 540 } | 550 } |
| OLD | NEW |