| Index: chrome/browser/ui/toolbar/toolbar_actions_bar.cc
|
| diff --git a/chrome/browser/ui/toolbar/toolbar_actions_bar.cc b/chrome/browser/ui/toolbar/toolbar_actions_bar.cc
|
| index 72f85cc804889a821b1695b5c2608ed721da1e22..5ced529c819ef736defa4183c46451c7e7895200 100644
|
| --- a/chrome/browser/ui/toolbar/toolbar_actions_bar.cc
|
| +++ b/chrome/browser/ui/toolbar/toolbar_actions_bar.cc
|
| @@ -4,6 +4,7 @@
|
|
|
| #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
|
|
|
| +#include <set>
|
| #include <utility>
|
|
|
| #include "base/auto_reset.h"
|
| @@ -54,8 +55,8 @@ enum DimensionType { WIDTH, HEIGHT };
|
| // |FunctionType| should equate to (something similar to)
|
| // bool Equal(const Type1&, const Type2&), but we can't enforce this
|
| // because of MSVC compilation limitations.
|
| -template<typename Type1, typename Type2, typename FunctionType>
|
| -void SortContainer(std::vector<Type1>* to_sort,
|
| +template <typename Type1, typename Type2, typename FunctionType>
|
| +void SortContainer(std::vector<std::unique_ptr<Type1>>* to_sort,
|
| const std::vector<Type2>& reference,
|
| FunctionType equal) {
|
| CHECK_GE(to_sort->size(), reference.size()) <<
|
| @@ -65,11 +66,11 @@ void SortContainer(std::vector<Type1>* to_sort,
|
| // Run through the each element and compare it to the reference. If something
|
| // is out of place, find the correct spot for it.
|
| for (size_t i = 0; i < reference.size() - 1; ++i) {
|
| - if (!equal(to_sort->at(i), reference[i])) {
|
| + if (!equal(to_sort->at(i).get(), reference[i])) {
|
| // Find the correct index (it's guaranteed to be after our current
|
| // index, since everything up to this point is correct), and swap.
|
| size_t j = i + 1;
|
| - while (!equal(to_sort->at(j), reference[i])) {
|
| + while (!equal(to_sort->at(j).get(), reference[i])) {
|
| ++j;
|
| DCHECK_LT(j, to_sort->size()) <<
|
| "Item in |reference| not found in |to_sort|.";
|
| @@ -210,10 +211,12 @@ size_t ToolbarActionsBar::GetIconCount() const {
|
| // If there is a popped out action, it could affect the number of visible
|
| // icons - but only if it wouldn't otherwise be visible.
|
| if (popped_out_action_) {
|
| - size_t popped_out_index =
|
| - std::find(toolbar_actions_.begin(),
|
| - toolbar_actions_.end(),
|
| - popped_out_action_) - toolbar_actions_.begin();
|
| + size_t popped_out_index = 0;
|
| + for (; popped_out_index < toolbar_actions_.size(); ++popped_out_index) {
|
| + if (toolbar_actions_[popped_out_index].get() == popped_out_action_)
|
| + break;
|
| + }
|
| +
|
| pop_out_modifier = popped_out_index >= model_->visible_icon_count() ? 1 : 0;
|
| }
|
|
|
| @@ -294,7 +297,9 @@ gfx::Rect ToolbarActionsBar::GetFrameForIndex(
|
|
|
| std::vector<ToolbarActionViewController*>
|
| ToolbarActionsBar::GetActions() const {
|
| - std::vector<ToolbarActionViewController*> actions = toolbar_actions_.get();
|
| + std::vector<ToolbarActionViewController*> actions;
|
| + for (const auto& action : toolbar_actions_)
|
| + actions.push_back(action.get());
|
|
|
| // If there is an action that should be popped out, and it's not visible by
|
| // default, make it the final action in the list.
|
| @@ -352,7 +357,7 @@ void ToolbarActionsBar::CreateActions() {
|
| FROM_HERE_WITH_EXPLICIT_FUNCTION("ToolbarActionsBar::CreateActions4"));
|
|
|
| for (size_t i = 0; i < toolbar_actions_.size(); ++i)
|
| - delegate_->AddViewForAction(toolbar_actions_[i], i);
|
| + delegate_->AddViewForAction(toolbar_actions_[i].get(), i);
|
| }
|
|
|
| // Once the actions are created, we should animate the changes.
|
| @@ -385,7 +390,7 @@ void ToolbarActionsBar::Update() {
|
| {
|
| // Don't layout until the end.
|
| base::AutoReset<bool> layout_resetter(&suppress_layout_, true);
|
| - for (ToolbarActionViewController* action : toolbar_actions_)
|
| + for (const auto& action : toolbar_actions_)
|
| action->UpdateState();
|
| }
|
|
|
| @@ -483,10 +488,15 @@ bool ToolbarActionsBar::IsActionVisibleOnMainBar(
|
| if (in_overflow_mode())
|
| return main_bar_->IsActionVisibleOnMainBar(action);
|
|
|
| - size_t index = std::find(toolbar_actions_.begin(),
|
| - toolbar_actions_.end(),
|
| - action) - toolbar_actions_.begin();
|
| - return index < GetIconCount() || action == popped_out_action_;
|
| + if (action == popped_out_action_)
|
| + return true;
|
| +
|
| + size_t visible_icon_count = std::min(toolbar_actions_.size(), GetIconCount());
|
| + for (size_t index = 0; index < visible_icon_count; ++index)
|
| + if (toolbar_actions_[index].get() == action)
|
| + return true;
|
| +
|
| + return false;
|
| }
|
|
|
| void ToolbarActionsBar::PopOutAction(ToolbarActionViewController* controller,
|
| @@ -620,7 +630,7 @@ void ToolbarActionsBar::OnToolbarActionAdded(
|
|
|
| toolbar_actions_.insert(toolbar_actions_.begin() + index,
|
| model_->CreateActionForItem(browser_, this, item));
|
| - delegate_->AddViewForAction(toolbar_actions_[index], index);
|
| + delegate_->AddViewForAction(toolbar_actions_[index].get(), index);
|
|
|
| // We may need to resize (e.g. to show the new icon, or the chevron). We don't
|
| // need to check if an extension is upgrading here, because ResizeDelegate()
|
| @@ -644,8 +654,9 @@ void ToolbarActionsBar::OnToolbarActionRemoved(const std::string& action_id) {
|
| // The action should outlive the UI element (which is owned by the delegate),
|
| // so we can't delete it just yet. But we should remove it from the list of
|
| // actions so that any width calculations are correct.
|
| - std::unique_ptr<ToolbarActionViewController> removed_action(*iter);
|
| - toolbar_actions_.weak_erase(iter);
|
| + std::unique_ptr<ToolbarActionViewController> removed_action =
|
| + std::move(*iter);
|
| + toolbar_actions_.erase(iter);
|
| delegate_->RemoveViewForAction(removed_action.get());
|
| if (popped_out_action_ == removed_action.get())
|
| UndoPopOut();
|
| @@ -726,27 +737,30 @@ void ToolbarActionsBar::OnToolbarHighlightModeChanged(bool is_highlighting) {
|
| {
|
| base::AutoReset<bool> layout_resetter(&suppress_layout_, true);
|
| base::AutoReset<bool> animation_resetter(&suppress_animation_, true);
|
| - std::set<std::string> seen;
|
| - for (const ToolbarActionsModel::ToolbarItem item :
|
| - model_->toolbar_items()) {
|
| - auto current_pos =
|
| - std::find_if(toolbar_actions_.begin(), toolbar_actions_.end(),
|
| - [&item](const ToolbarActionViewController* action) {
|
| - return action->GetId() == item.id;
|
| - });
|
| - if (current_pos == toolbar_actions_.end()) {
|
| + std::set<std::string> model_item_ids;
|
| + for (const auto& model_item : model_->toolbar_items()) {
|
| + model_item_ids.insert(model_item.id);
|
| +
|
| + bool found = false;
|
| + for (size_t i = 0; i < toolbar_actions_.size(); ++i) {
|
| + if (toolbar_actions_[i]->GetId() == model_item.id) {
|
| + found = true;
|
| + break;
|
| + }
|
| + }
|
| +
|
| + if (!found) {
|
| toolbar_actions_.push_back(
|
| - model_->CreateActionForItem(browser_, this, item).release());
|
| - delegate_->AddViewForAction(toolbar_actions_.back(),
|
| + model_->CreateActionForItem(browser_, this, model_item));
|
| + delegate_->AddViewForAction(toolbar_actions_.back().get(),
|
| toolbar_actions_.size() - 1);
|
| }
|
| - seen.insert(item.id);
|
| }
|
|
|
| - for (ToolbarActions::iterator iter = toolbar_actions_.begin();
|
| + for (auto iter = toolbar_actions_.begin();
|
| iter != toolbar_actions_.end();) {
|
| - if (seen.count((*iter)->GetId()) == 0) {
|
| - delegate_->RemoveViewForAction(*iter);
|
| + if (model_item_ids.count((*iter)->GetId()) == 0) {
|
| + delegate_->RemoveViewForAction(iter->get());
|
| iter = toolbar_actions_.erase(iter);
|
| } else {
|
| ++iter;
|
| @@ -787,7 +801,7 @@ void ToolbarActionsBar::ReorderActions() {
|
| const ToolbarActionsModel::ToolbarItem& item) {
|
| return action->GetId() == item.id;
|
| };
|
| - SortContainer(&toolbar_actions_.get(), model_->toolbar_items(), compare);
|
| + SortContainer(&toolbar_actions_, model_->toolbar_items(), compare);
|
|
|
| // Our visible browser actions may have changed - re-Layout() and check the
|
| // size (if we aren't suppressing the layout).
|
| @@ -799,9 +813,9 @@ void ToolbarActionsBar::ReorderActions() {
|
|
|
| ToolbarActionViewController* ToolbarActionsBar::GetActionForId(
|
| const std::string& action_id) {
|
| - for (ToolbarActionViewController* action : toolbar_actions_) {
|
| + for (const auto& action : toolbar_actions_) {
|
| if (action->GetId() == action_id)
|
| - return action;
|
| + return action.get();
|
| }
|
| return nullptr;
|
| }
|
|
|