From 711de5edcb80523f479bf6a8c70f5d5747f6c2ba Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Sat, 5 Mar 2022 10:52:55 +0100 Subject: [PATCH] Ensure `active_item_index` doesn't go off the end when closing items This fixes a bug introduced in #538, where closing the current tab would hide all the other tabs, if the current tab was the last one. Also, this commit manually sets the active item index instead of calling `Pane::activate_item`: even though this introduces a little bit of duplication, it prevents us from mistakenly calling `deactivate` on the wrong item. This would happen because `activate_item` looks at `self.active_item_index` to determine which item to deactivate before setting the new one. However, that index is potentially invalid because `::close_items` manipulates the `item_views` vector, so `activate_item` could end up calling `deactivate` on an item view that was not active in the first place. --- crates/workspace/src/pane.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index e9fb773a845500c4b9394ac706f238d1977e09c3..78f84d7da2d807a458b25b8ec4e3bf8b802ce61c 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -406,11 +406,13 @@ impl Pane { }); if self.item_views.is_empty() { - self.update_active_toolbar(cx); cx.emit(Event::Remove); } else { - self.activate_item(new_active_item_index, cx); + self.active_item_index = cmp::min(new_active_item_index, self.item_views.len() - 1); + self.focus_active_item(cx); + self.activate(cx); } + self.update_active_toolbar(cx); cx.notify(); }