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

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_actions_bar.cc

Issue 2606293002: Remove ScopedVector from chrome/browser/ui. (Closed)
Patch Set: view Created 3 years, 11 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/toolbar/toolbar_actions_bar.h" 5 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 // Takes a reference vector |reference| of length n, where n is less than or 47 // Takes a reference vector |reference| of length n, where n is less than or
48 // equal to the length of |to_sort|, and rearranges |to_sort| so that 48 // equal to the length of |to_sort|, and rearranges |to_sort| so that
49 // |to_sort|'s first n elements match the n elements of |reference| (the order 49 // |to_sort|'s first n elements match the n elements of |reference| (the order
50 // of any remaining elements in |to_sort| is unspecified). 50 // of any remaining elements in |to_sort| is unspecified).
51 // |equal| is used to compare the elements of |to_sort| and |reference|. 51 // |equal| is used to compare the elements of |to_sort| and |reference|.
52 // This allows us to sort a vector to match another vector of two different 52 // This allows us to sort a vector to match another vector of two different
53 // types without needing to construct a more cumbersome comparator class. 53 // types without needing to construct a more cumbersome comparator class.
54 // |FunctionType| should equate to (something similar to) 54 // |FunctionType| should equate to (something similar to)
55 // bool Equal(const Type1&, const Type2&), but we can't enforce this 55 // bool Equal(const Type1&, const Type2&), but we can't enforce this
56 // because of MSVC compilation limitations. 56 // because of MSVC compilation limitations.
57 template<typename Type1, typename Type2, typename FunctionType> 57 template <typename Type1, typename Type2, typename FunctionType>
58 void SortContainer(std::vector<Type1>* to_sort, 58 void SortContainer(std::vector<std::unique_ptr<Type1>>* to_sort,
59 const std::vector<Type2>& reference, 59 const std::vector<Type2>& reference,
60 FunctionType equal) { 60 FunctionType equal) {
61 CHECK_GE(to_sort->size(), reference.size()) << 61 CHECK_GE(to_sort->size(), reference.size()) <<
62 "|to_sort| must contain all elements in |reference|."; 62 "|to_sort| must contain all elements in |reference|.";
63 if (reference.empty()) 63 if (reference.empty())
64 return; 64 return;
65 // Run through the each element and compare it to the reference. If something 65 // Run through the each element and compare it to the reference. If something
66 // is out of place, find the correct spot for it. 66 // is out of place, find the correct spot for it.
67 for (size_t i = 0; i < reference.size() - 1; ++i) { 67 for (size_t i = 0; i < reference.size() - 1; ++i) {
68 if (!equal(to_sort->at(i), reference[i])) { 68 if (!equal(to_sort->at(i).get(), reference[i])) {
69 // Find the correct index (it's guaranteed to be after our current 69 // Find the correct index (it's guaranteed to be after our current
70 // index, since everything up to this point is correct), and swap. 70 // index, since everything up to this point is correct), and swap.
71 size_t j = i + 1; 71 size_t j = i + 1;
72 while (!equal(to_sort->at(j), reference[i])) { 72 while (!equal(to_sort->at(j).get(), reference[i])) {
73 ++j; 73 ++j;
74 DCHECK_LT(j, to_sort->size()) << 74 DCHECK_LT(j, to_sort->size()) <<
75 "Item in |reference| not found in |to_sort|."; 75 "Item in |reference| not found in |to_sort|.";
76 } 76 }
77 std::swap(to_sort->at(i), to_sort->at(j)); 77 std::swap(to_sort->at(i), to_sort->at(j));
78 } 78 }
79 } 79 }
80 } 80 }
81 81
82 // How long to wait until showing an extension message bubble. 82 // How long to wait until showing an extension message bubble.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 203 }
204 204
205 size_t ToolbarActionsBar::GetIconCount() const { 205 size_t ToolbarActionsBar::GetIconCount() const {
206 if (!model_) 206 if (!model_)
207 return 0; 207 return 0;
208 208
209 int pop_out_modifier = 0; 209 int pop_out_modifier = 0;
210 // If there is a popped out action, it could affect the number of visible 210 // If there is a popped out action, it could affect the number of visible
211 // icons - but only if it wouldn't otherwise be visible. 211 // icons - but only if it wouldn't otherwise be visible.
212 if (popped_out_action_) { 212 if (popped_out_action_) {
213 size_t popped_out_index = 213 auto popped_out_action = popped_out_action_;
214 std::find(toolbar_actions_.begin(), 214 auto popped_out_it = std::find_if(
215 toolbar_actions_.end(), 215 toolbar_actions_.begin(), toolbar_actions_.end(),
216 popped_out_action_) - toolbar_actions_.begin(); 216 [popped_out_action](
217 const std::unique_ptr<ToolbarActionViewController>& ptr) {
218 return ptr.get() == popped_out_action;
219 });
220 size_t popped_out_index = popped_out_it - toolbar_actions_.begin();
Nico 2017/01/03 18:12:35 again, size_t popped_out_index = 0; for (; po
Avi (use Gerrit) 2017/01/03 22:54:53 Done.
217 pop_out_modifier = popped_out_index >= model_->visible_icon_count() ? 1 : 0; 221 pop_out_modifier = popped_out_index >= model_->visible_icon_count() ? 1 : 0;
218 } 222 }
219 223
220 // We purposefully do not account for any "popped out" actions in overflow 224 // We purposefully do not account for any "popped out" actions in overflow
221 // mode. This is because the popup cannot be showing while the overflow menu 225 // mode. This is because the popup cannot be showing while the overflow menu
222 // is open, so there's no concern there. Also, if the user has a popped out 226 // is open, so there's no concern there. Also, if the user has a popped out
223 // action, and immediately opens the overflow menu, we *want* the action there 227 // action, and immediately opens the overflow menu, we *want* the action there
224 // (since it will close the popup, but do so asynchronously, and we don't 228 // (since it will close the popup, but do so asynchronously, and we don't
225 // want to "slide" the action back in. 229 // want to "slide" the action back in.
226 size_t visible_icons = in_overflow_mode() ? 230 size_t visible_icons = in_overflow_mode() ?
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 291
288 return gfx::Rect(platform_settings().item_spacing + 292 return gfx::Rect(platform_settings().item_spacing +
289 index_in_row * IconWidth(true), 293 index_in_row * IconWidth(true),
290 row_index * IconHeight(), 294 row_index * IconHeight(),
291 IconWidth(false), 295 IconWidth(false),
292 IconHeight()); 296 IconHeight());
293 } 297 }
294 298
295 std::vector<ToolbarActionViewController*> 299 std::vector<ToolbarActionViewController*>
296 ToolbarActionsBar::GetActions() const { 300 ToolbarActionsBar::GetActions() const {
297 std::vector<ToolbarActionViewController*> actions = toolbar_actions_.get(); 301 std::vector<ToolbarActionViewController*> actions;
302 for (const auto& action : toolbar_actions_)
303 actions.push_back(action.get());
298 304
299 // If there is an action that should be popped out, and it's not visible by 305 // If there is an action that should be popped out, and it's not visible by
300 // default, make it the final action in the list. 306 // default, make it the final action in the list.
301 if (popped_out_action_) { 307 if (popped_out_action_) {
302 size_t index = 308 size_t index =
303 std::find(actions.begin(), actions.end(), popped_out_action_) - 309 std::find(actions.begin(), actions.end(), popped_out_action_) -
304 actions.begin(); 310 actions.begin();
305 DCHECK_NE(actions.size(), index); 311 DCHECK_NE(actions.size(), index);
306 size_t visible = GetIconCount(); 312 size_t visible = GetIconCount();
307 if (index >= visible) { 313 if (index >= visible) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 tracked_objects::ScopedTracker tracking_profile3( 351 tracked_objects::ScopedTracker tracking_profile3(
346 FROM_HERE_WITH_EXPLICIT_FUNCTION( 352 FROM_HERE_WITH_EXPLICIT_FUNCTION(
347 "ToolbarActionsBar::CreateActions3")); 353 "ToolbarActionsBar::CreateActions3"));
348 ReorderActions(); 354 ReorderActions();
349 } 355 }
350 356
351 tracked_objects::ScopedTracker tracking_profile4( 357 tracked_objects::ScopedTracker tracking_profile4(
352 FROM_HERE_WITH_EXPLICIT_FUNCTION("ToolbarActionsBar::CreateActions4")); 358 FROM_HERE_WITH_EXPLICIT_FUNCTION("ToolbarActionsBar::CreateActions4"));
353 359
354 for (size_t i = 0; i < toolbar_actions_.size(); ++i) 360 for (size_t i = 0; i < toolbar_actions_.size(); ++i)
355 delegate_->AddViewForAction(toolbar_actions_[i], i); 361 delegate_->AddViewForAction(toolbar_actions_[i].get(), i);
356 } 362 }
357 363
358 // Once the actions are created, we should animate the changes. 364 // Once the actions are created, we should animate the changes.
359 suppress_animation_ = false; 365 suppress_animation_ = false;
360 366
361 // CreateActions() can be called multiple times, so we need to make sure we 367 // CreateActions() can be called multiple times, so we need to make sure we
362 // haven't already shown the bubble. 368 // haven't already shown the bubble.
363 // Extension bubbles can also highlight a subset of actions, so don't show the 369 // Extension bubbles can also highlight a subset of actions, so don't show the
364 // bubble if the toolbar is already highlighting a different set. 370 // bubble if the toolbar is already highlighting a different set.
365 if (should_check_extension_bubble_ && !is_highlighting()) { 371 if (should_check_extension_bubble_ && !is_highlighting()) {
(...skipping 12 matching lines...) Expand all
378 toolbar_actions_.clear(); 384 toolbar_actions_.clear();
379 } 385 }
380 386
381 void ToolbarActionsBar::Update() { 387 void ToolbarActionsBar::Update() {
382 if (toolbar_actions_.empty()) 388 if (toolbar_actions_.empty())
383 return; // Nothing to do. 389 return; // Nothing to do.
384 390
385 { 391 {
386 // Don't layout until the end. 392 // Don't layout until the end.
387 base::AutoReset<bool> layout_resetter(&suppress_layout_, true); 393 base::AutoReset<bool> layout_resetter(&suppress_layout_, true);
388 for (ToolbarActionViewController* action : toolbar_actions_) 394 for (const auto& action : toolbar_actions_)
389 action->UpdateState(); 395 action->UpdateState();
390 } 396 }
391 397
392 ReorderActions(); // Also triggers a draw. 398 ReorderActions(); // Also triggers a draw.
393 } 399 }
394 400
395 bool ToolbarActionsBar::ShowToolbarActionPopup(const std::string& action_id, 401 bool ToolbarActionsBar::ShowToolbarActionPopup(const std::string& action_id,
396 bool grant_active_tab) { 402 bool grant_active_tab) {
397 // Don't override another popup, and only show in the active window. 403 // Don't override another popup, and only show in the active window.
398 if (popup_owner() || !browser_->window()->IsActive()) 404 if (popup_owner() || !browser_->window()->IsActive())
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 482
477 void ToolbarActionsBar::OnBubbleClosed() { 483 void ToolbarActionsBar::OnBubbleClosed() {
478 is_showing_bubble_ = false; 484 is_showing_bubble_ = false;
479 } 485 }
480 486
481 bool ToolbarActionsBar::IsActionVisibleOnMainBar( 487 bool ToolbarActionsBar::IsActionVisibleOnMainBar(
482 const ToolbarActionViewController* action) const { 488 const ToolbarActionViewController* action) const {
483 if (in_overflow_mode()) 489 if (in_overflow_mode())
484 return main_bar_->IsActionVisibleOnMainBar(action); 490 return main_bar_->IsActionVisibleOnMainBar(action);
485 491
486 size_t index = std::find(toolbar_actions_.begin(), 492 auto it = std::find_if(
487 toolbar_actions_.end(), 493 toolbar_actions_.begin(), toolbar_actions_.end(),
488 action) - toolbar_actions_.begin(); 494 [action](const std::unique_ptr<ToolbarActionViewController>& ptr) {
495 return ptr.get() == action;
496 });
497 size_t index = it - toolbar_actions_.begin();
489 return index < GetIconCount() || action == popped_out_action_; 498 return index < GetIconCount() || action == popped_out_action_;
Nico 2017/01/03 18:12:35 nit: I'd compare action to popped_out_action_ firs
Avi (use Gerrit) 2017/01/03 22:54:53 Done.
490 } 499 }
491 500
492 void ToolbarActionsBar::PopOutAction(ToolbarActionViewController* controller, 501 void ToolbarActionsBar::PopOutAction(ToolbarActionViewController* controller,
493 bool is_sticky, 502 bool is_sticky,
494 const base::Closure& closure) { 503 const base::Closure& closure) {
495 DCHECK(!in_overflow_mode()) << "Only the main bar can pop out actions."; 504 DCHECK(!in_overflow_mode()) << "Only the main bar can pop out actions.";
496 DCHECK(!popped_out_action_) << "Only one action can be popped out at a time!"; 505 DCHECK(!popped_out_action_) << "Only one action can be popped out at a time!";
497 bool needs_redraw = !IsActionVisibleOnMainBar(controller); 506 bool needs_redraw = !IsActionVisibleOnMainBar(controller);
498 popped_out_action_ = controller; 507 popped_out_action_ = controller;
499 is_popped_out_sticky_ = is_sticky; 508 is_popped_out_sticky_ = is_sticky;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 void ToolbarActionsBar::OnToolbarActionAdded( 622 void ToolbarActionsBar::OnToolbarActionAdded(
614 const ToolbarActionsModel::ToolbarItem& item, 623 const ToolbarActionsModel::ToolbarItem& item,
615 int index) { 624 int index) {
616 CHECK(model_->actions_initialized()); 625 CHECK(model_->actions_initialized());
617 CHECK(GetActionForId(item.id) == nullptr) 626 CHECK(GetActionForId(item.id) == nullptr)
618 << "Asked to add a toolbar action view for an action that already " 627 << "Asked to add a toolbar action view for an action that already "
619 "exists"; 628 "exists";
620 629
621 toolbar_actions_.insert(toolbar_actions_.begin() + index, 630 toolbar_actions_.insert(toolbar_actions_.begin() + index,
622 model_->CreateActionForItem(browser_, this, item)); 631 model_->CreateActionForItem(browser_, this, item));
623 delegate_->AddViewForAction(toolbar_actions_[index], index); 632 delegate_->AddViewForAction(toolbar_actions_[index].get(), index);
624 633
625 // We may need to resize (e.g. to show the new icon, or the chevron). We don't 634 // We may need to resize (e.g. to show the new icon, or the chevron). We don't
626 // need to check if an extension is upgrading here, because ResizeDelegate() 635 // need to check if an extension is upgrading here, because ResizeDelegate()
627 // checks to see if the container is already the proper size, and because 636 // checks to see if the container is already the proper size, and because
628 // if the action is newly incognito enabled, even though it's a reload, it's 637 // if the action is newly incognito enabled, even though it's a reload, it's
629 // a new extension to this toolbar. 638 // a new extension to this toolbar.
630 // We suppress the chevron during animation because, if we're expanding to 639 // We suppress the chevron during animation because, if we're expanding to
631 // show a new icon, we don't want to have the chevron visible only for the 640 // show a new icon, we don't want to have the chevron visible only for the
632 // duration of the animation. 641 // duration of the animation.
633 ResizeDelegate(gfx::Tween::LINEAR, true); 642 ResizeDelegate(gfx::Tween::LINEAR, true);
634 } 643 }
635 644
636 void ToolbarActionsBar::OnToolbarActionRemoved(const std::string& action_id) { 645 void ToolbarActionsBar::OnToolbarActionRemoved(const std::string& action_id) {
637 ToolbarActions::iterator iter = toolbar_actions_.begin(); 646 ToolbarActions::iterator iter = toolbar_actions_.begin();
638 while (iter != toolbar_actions_.end() && (*iter)->GetId() != action_id) 647 while (iter != toolbar_actions_.end() && (*iter)->GetId() != action_id)
639 ++iter; 648 ++iter;
640 649
641 if (iter == toolbar_actions_.end()) 650 if (iter == toolbar_actions_.end())
642 return; 651 return;
643 652
644 // The action should outlive the UI element (which is owned by the delegate), 653 // The action should outlive the UI element (which is owned by the delegate),
645 // so we can't delete it just yet. But we should remove it from the list of 654 // so we can't delete it just yet. But we should remove it from the list of
646 // actions so that any width calculations are correct. 655 // actions so that any width calculations are correct.
647 std::unique_ptr<ToolbarActionViewController> removed_action(*iter); 656 std::unique_ptr<ToolbarActionViewController> removed_action =
648 toolbar_actions_.weak_erase(iter); 657 std::move(*iter);
658 toolbar_actions_.erase(iter);
649 delegate_->RemoveViewForAction(removed_action.get()); 659 delegate_->RemoveViewForAction(removed_action.get());
650 if (popped_out_action_ == removed_action.get()) 660 if (popped_out_action_ == removed_action.get())
651 UndoPopOut(); 661 UndoPopOut();
652 removed_action.reset(); 662 removed_action.reset();
653 663
654 // If the extension is being upgraded we don't want the bar to shrink 664 // If the extension is being upgraded we don't want the bar to shrink
655 // because the icon is just going to get re-added to the same location. 665 // because the icon is just going to get re-added to the same location.
656 // There is an exception if this is an off-the-record profile, and the 666 // There is an exception if this is an off-the-record profile, and the
657 // extension is no longer incognito-enabled. 667 // extension is no longer incognito-enabled.
658 if (!extensions::ExtensionSystem::Get(browser_->profile()) 668 if (!extensions::ExtensionSystem::Get(browser_->profile())
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 } 730 }
721 731
722 void ToolbarActionsBar::OnToolbarHighlightModeChanged(bool is_highlighting) { 732 void ToolbarActionsBar::OnToolbarHighlightModeChanged(bool is_highlighting) {
723 if (!model_->actions_initialized()) 733 if (!model_->actions_initialized())
724 return; 734 return;
725 735
726 { 736 {
727 base::AutoReset<bool> layout_resetter(&suppress_layout_, true); 737 base::AutoReset<bool> layout_resetter(&suppress_layout_, true);
728 base::AutoReset<bool> animation_resetter(&suppress_animation_, true); 738 base::AutoReset<bool> animation_resetter(&suppress_animation_, true);
729 std::set<std::string> seen; 739 std::set<std::string> seen;
730 for (const ToolbarActionsModel::ToolbarItem item : 740 for (const auto& item : model_->toolbar_items()) {
731 model_->toolbar_items()) { 741 auto current_pos = std::find_if(
732 auto current_pos = 742 toolbar_actions_.begin(), toolbar_actions_.end(),
733 std::find_if(toolbar_actions_.begin(), toolbar_actions_.end(), 743 [&item](const std::unique_ptr<ToolbarActionViewController>& action) {
734 [&item](const ToolbarActionViewController* action) { 744 return action->GetId() == item.id;
735 return action->GetId() == item.id; 745 });
Nico 2017/01/03 18:12:35 guess :-)
Avi (use Gerrit) 2017/01/03 22:54:53 Done.
736 });
737 if (current_pos == toolbar_actions_.end()) { 746 if (current_pos == toolbar_actions_.end()) {
738 toolbar_actions_.push_back( 747 toolbar_actions_.push_back(
739 model_->CreateActionForItem(browser_, this, item).release()); 748 model_->CreateActionForItem(browser_, this, item));
740 delegate_->AddViewForAction(toolbar_actions_.back(), 749 delegate_->AddViewForAction(toolbar_actions_.back().get(),
741 toolbar_actions_.size() - 1); 750 toolbar_actions_.size() - 1);
742 } 751 }
743 seen.insert(item.id); 752 seen.insert(item.id);
744 } 753 }
745 754
746 for (ToolbarActions::iterator iter = toolbar_actions_.begin(); 755 for (auto iter = toolbar_actions_.begin();
747 iter != toolbar_actions_.end();) { 756 iter != toolbar_actions_.end();) {
748 if (seen.count((*iter)->GetId()) == 0) { 757 if (seen.count((*iter)->GetId()) == 0) {
749 delegate_->RemoveViewForAction(*iter); 758 delegate_->RemoveViewForAction(iter->get());
750 iter = toolbar_actions_.erase(iter); 759 iter = toolbar_actions_.erase(iter);
751 } else { 760 } else {
752 ++iter; 761 ++iter;
753 } 762 }
754 } 763 }
755 } 764 }
756 765
757 ReorderActions(); 766 ReorderActions();
758 } 767 }
759 768
(...skipping 20 matching lines...) Expand all
780 789
781 void ToolbarActionsBar::ReorderActions() { 790 void ToolbarActionsBar::ReorderActions() {
782 if (toolbar_actions_.empty()) 791 if (toolbar_actions_.empty())
783 return; 792 return;
784 793
785 // First, reset the order to that of the model. 794 // First, reset the order to that of the model.
786 auto compare = [](ToolbarActionViewController* const& action, 795 auto compare = [](ToolbarActionViewController* const& action,
787 const ToolbarActionsModel::ToolbarItem& item) { 796 const ToolbarActionsModel::ToolbarItem& item) {
788 return action->GetId() == item.id; 797 return action->GetId() == item.id;
789 }; 798 };
790 SortContainer(&toolbar_actions_.get(), model_->toolbar_items(), compare); 799 SortContainer(&toolbar_actions_, model_->toolbar_items(), compare);
791 800
792 // Our visible browser actions may have changed - re-Layout() and check the 801 // Our visible browser actions may have changed - re-Layout() and check the
793 // size (if we aren't suppressing the layout). 802 // size (if we aren't suppressing the layout).
794 if (!suppress_layout_) { 803 if (!suppress_layout_) {
795 ResizeDelegate(gfx::Tween::EASE_OUT, false); 804 ResizeDelegate(gfx::Tween::EASE_OUT, false);
796 delegate_->Redraw(true); 805 delegate_->Redraw(true);
797 } 806 }
798 } 807 }
799 808
800 ToolbarActionViewController* ToolbarActionsBar::GetActionForId( 809 ToolbarActionViewController* ToolbarActionsBar::GetActionForId(
801 const std::string& action_id) { 810 const std::string& action_id) {
802 for (ToolbarActionViewController* action : toolbar_actions_) { 811 for (const auto& action : toolbar_actions_) {
803 if (action->GetId() == action_id) 812 if (action->GetId() == action_id)
804 return action; 813 return action.get();
805 } 814 }
806 return nullptr; 815 return nullptr;
807 } 816 }
808 817
809 content::WebContents* ToolbarActionsBar::GetCurrentWebContents() { 818 content::WebContents* ToolbarActionsBar::GetCurrentWebContents() {
810 return browser_->tab_strip_model()->GetActiveWebContents(); 819 return browser_->tab_strip_model()->GetActiveWebContents();
811 } 820 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698