From 1a53d5b7ba61bc020f3f5ad624b5327391a13dca Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 10 Jan 2022 10:10:11 +0100 Subject: [PATCH] Use a new `Workspace::activate_item` API in project diagnostics Previously, we would only activate the pane without switching the pane's *active item*. --- crates/diagnostics/src/diagnostics.rs | 2 +- crates/workspace/src/pane.rs | 8 ++++++-- crates/workspace/src/workspace.rs | 19 ++++++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 3d14aea1a6e903ebbb3b0f3805655e8f905180a2..f9cbb68aab6ba2e70da4121bfeee4c2828c29f83 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -167,7 +167,7 @@ impl ProjectDiagnosticsEditor { fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext) { if let Some(existing) = workspace.item_of_type::(cx) { - workspace.activate_pane_for_item(&existing, cx); + workspace.activate_item(&existing, cx); } else { let diagnostics = cx.add_model(|_| ProjectDiagnostics::new(workspace.project().clone())); diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index e7e1383e17c1dbbf2202a26d245d007d065ceedd..731db29d634049ea6cebeec5b2369c4a98fa63fa 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -139,10 +139,14 @@ impl Pane { .map(|(_, view)| view.clone()) } - pub fn item_index(&self, item: &dyn ItemViewHandle) -> Option { + pub fn index_for_item_view(&self, item_view: &dyn ItemViewHandle) -> Option { self.item_views .iter() - .position(|(_, i)| i.id() == item.id()) + .position(|(_, i)| i.id() == item_view.id()) + } + + pub fn index_for_item(&self, item: &dyn ItemHandle) -> Option { + self.item_views.iter().position(|(id, _)| *id == item.id()) } pub fn activate_item(&mut self, index: usize, cx: &mut ViewContext) { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 55538c09dd691413817fbe3ec4115d2151c317c3..ea6eb6603140108238ff916e605fb787451e12f3 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -334,7 +334,7 @@ impl ItemViewHandle for ViewHandle { return; } if T::should_activate_item_on_event(event) { - if let Some(ix) = pane.item_index(&item) { + if let Some(ix) = pane.index_for_item_view(&item) { pane.activate_item(ix, cx); pane.activate(cx); } @@ -962,6 +962,23 @@ impl Workspace { } } + pub fn activate_item(&mut self, item: &dyn ItemHandle, cx: &mut ViewContext) -> bool { + let result = self.panes.iter().find_map(|pane| { + if let Some(ix) = pane.read(cx).index_for_item(item) { + Some((pane.clone(), ix)) + } else { + None + } + }); + if let Some((pane, ix)) = result { + self.activate_pane(pane.clone(), cx); + pane.update(cx, |pane, cx| pane.activate_item(ix, cx)); + true + } else { + false + } + } + fn activate_pane(&mut self, pane: ViewHandle, cx: &mut ViewContext) { self.active_pane = pane; self.status_bar.update(cx, |status_bar, cx| {