From 3057b38fad5f04793bc8039c4b7b4219a0ac756f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 4 Mar 2022 10:36:51 -0700 Subject: [PATCH] Implement close inactive items action Co-Authored-By: Antonio Scandurra Co-Authored-By: Max Brunsfeld --- crates/workspace/src/pane.rs | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index a4eb6c008e..d827ec6f5a 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -25,6 +25,7 @@ action!(ActivateItem, usize); action!(ActivatePrevItem); action!(ActivateNextItem); action!(CloseActiveItem); +action!(CloseInactiveItems); action!(CloseItem, usize); action!(GoBack); action!(GoForward); @@ -44,6 +45,9 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(|pane: &mut Pane, _: &CloseActiveItem, cx| { pane.close_active_item(cx); }); + cx.add_action(|pane: &mut Pane, _: &CloseInactiveItems, cx| { + pane.close_inactive_items(cx); + }); cx.add_action(|pane: &mut Pane, action: &CloseItem, cx| { pane.close_item(action.0, cx); }); @@ -61,6 +65,7 @@ pub fn init(cx: &mut MutableAppContext) { Binding::new("shift-cmd-{", ActivatePrevItem, Some("Pane")), Binding::new("shift-cmd-}", ActivateNextItem, Some("Pane")), Binding::new("cmd-w", CloseActiveItem, Some("Pane")), + Binding::new("alt-cmd-t", CloseInactiveItems, Some("Pane")), Binding::new("cmd-k up", Split(SplitDirection::Up), Some("Pane")), Binding::new("cmd-k down", Split(SplitDirection::Down), Some("Pane")), Binding::new("cmd-k left", Split(SplitDirection::Left), Some("Pane")), @@ -357,14 +362,34 @@ impl Pane { } } - pub fn close_item(&mut self, item_view_id: usize, cx: &mut ViewContext) { + pub fn close_inactive_items(&mut self, cx: &mut ViewContext) { + if !self.item_views.is_empty() { + let active_item_id = self.item_views[self.active_item_index].1.id(); + self.close_items(cx, |id| id != active_item_id); + } + } + + pub fn close_item(&mut self, view_id_to_close: usize, cx: &mut ViewContext) { + self.close_items(cx, |view_id| view_id == view_id_to_close); + } + + pub fn close_items( + &mut self, + cx: &mut ViewContext, + should_close: impl Fn(usize) -> bool, + ) { let mut item_ix = 0; + let mut new_active_item_index = self.active_item_index; self.item_views.retain(|(_, item_view)| { - if item_view.id() == item_view_id { + if should_close(item_view.id()) { if item_ix == self.active_item_index { item_view.deactivated(cx); } + if item_ix < self.active_item_index { + new_active_item_index -= 1; + } + let mut nav_history = self.nav_history.borrow_mut(); if let Some(path) = item_view.project_path(cx) { nav_history.paths_by_item.insert(item_view.id(), path); @@ -379,17 +404,12 @@ impl Pane { true } }); - self.activate_item( - cmp::min( - self.active_item_index, - self.item_views.len().saturating_sub(1), - ), - cx, - ); if self.item_views.is_empty() { self.update_active_toolbar(cx); cx.emit(Event::Remove); + } else { + self.activate_item(new_active_item_index, cx); } cx.notify();