[go: up one dir, main page]

[Extensions Toolbar Mac] Properly adjust width after icon removal

The max width for the toolbar is set when an action is removed, but the action
was still counted in the calculations (since it was still in the underlying
vector). Fix this.

BUG=454338

Review URL: https://codereview.chromium.org/901303004

Cr-Commit-Position: refs/heads/master@{#315587}
(cherry picked from commit 2b26eb3ff127a64bcd1c8cf9afb2898fac0616e3)

TBR=avi@chromium.org

Review URL: https://codereview.chromium.org/936863002

Cr-Commit-Position: refs/branch-heads/2272@{#328}
Cr-Branched-From: 827a380cfdb31aa54c8d56e63ce2c3fd8c3ba4d4-refs/heads/master@{#310958}
diff --git a/chrome/browser/ui/cocoa/extensions/browser_actions_controller.mm b/chrome/browser/ui/cocoa/extensions/browser_actions_controller.mm
index 57525e9..3d39f52 100644
--- a/chrome/browser/ui/cocoa/extensions/browser_actions_controller.mm
+++ b/chrome/browser/ui/cocoa/extensions/browser_actions_controller.mm
@@ -465,8 +465,7 @@
              name:kBrowserActionButtonDraggingNotification
            object:newButton];
 
-  [containerView_ setMaxWidth:
-      toolbarActionsBar_->IconCountToWidth([self buttonCount])];
+  [containerView_ setMaxWidth:toolbarActionsBar_->GetMaximumWidth()];
 }
 
 - (void)redraw {
@@ -521,8 +520,7 @@
   [button onRemoved];
   [buttons_ removeObject:button];
 
-  [containerView_ setMaxWidth:
-      toolbarActionsBar_->IconCountToWidth([self buttonCount])];
+  [containerView_ setMaxWidth:toolbarActionsBar_->GetMaximumWidth()];
 }
 
 - (void)removeAllViews {
diff --git a/chrome/browser/ui/toolbar/toolbar_actions_bar.cc b/chrome/browser/ui/toolbar/toolbar_actions_bar.cc
index fc749dd..ed4b060 100644
--- a/chrome/browser/ui/toolbar/toolbar_actions_bar.cc
+++ b/chrome/browser/ui/toolbar/toolbar_actions_bar.cc
@@ -714,10 +714,15 @@
   if (iter == toolbar_actions_.end())
     return;
 
-  delegate_->RemoveViewForAction(*iter);
+  // 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.
+  scoped_ptr<ToolbarActionViewController> removed_action(*iter);
+  toolbar_actions_.weak_erase(iter);
+  delegate_->RemoveViewForAction(removed_action.get());
   if (tab_order_helper_)
-    tab_order_helper_->ActionRemoved(*iter);
-  toolbar_actions_.erase(iter);
+    tab_order_helper_->ActionRemoved(removed_action.get());
+  removed_action.reset();
 
   // If the extension is being upgraded we don't want the bar to shrink
   // because the icon is just going to get re-added to the same location.