From f453928b4495adb745b26ca56ce350967d4e01dc Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 26 Nov 2021 10:21:56 -0700 Subject: [PATCH] Associate the project with an active worktree This is similar to the active entry, but it can remain assigned even if there is no active entry and we can potentially manipulate it in other scenarios such as interaction with the project browser. This prepares the ground to show the collaborators for the active worktree. --- crates/project/src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 3e129c8fb8..d1d48f49ca 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -19,6 +19,7 @@ pub use worktree::*; pub struct Project { worktrees: Vec>, + active_worktree: Option, active_entry: Option, languages: Arc, client: Arc, @@ -46,6 +47,7 @@ impl Project { pub fn new(languages: Arc, rpc: Arc, fs: Arc) -> Self { Self { worktrees: Default::default(), + active_worktree: None, active_entry: None, languages, client: rpc, @@ -109,10 +111,25 @@ impl Project { fn add_worktree(&mut self, worktree: ModelHandle, cx: &mut ModelContext) { cx.observe(&worktree, |_, _, cx| cx.notify()).detach(); + if self.active_worktree.is_none() { + self.set_active_worktree(Some(worktree.id()), cx); + } self.worktrees.push(worktree); cx.notify(); } + fn set_active_worktree(&mut self, worktree_id: Option, cx: &mut ModelContext) { + if self.active_worktree != worktree_id { + self.active_worktree = worktree_id; + cx.notify(); + } + } + + pub fn active_worktree(&self) -> Option> { + self.active_worktree + .and_then(|worktree_id| self.worktree_for_id(worktree_id)) + } + pub fn set_active_path(&mut self, entry: Option, cx: &mut ModelContext) { let new_active_entry = entry.and_then(|project_path| { let worktree = self.worktree_for_id(project_path.worktree_id)?; @@ -123,6 +140,9 @@ impl Project { }) }); if new_active_entry != self.active_entry { + if let Some(worktree_id) = new_active_entry.map(|e| e.worktree_id) { + self.set_active_worktree(Some(worktree_id), cx); + } self.active_entry = new_active_entry; cx.emit(Event::ActiveEntryChanged(new_active_entry)); } @@ -184,6 +204,7 @@ impl Project { } pub fn close_remote_worktree(&mut self, id: u64, cx: &mut ModelContext) { + let mut reset_active = None; self.worktrees.retain(|worktree| { let keep = worktree.update(cx, |worktree, cx| { if let Some(worktree) = worktree.as_remote_mut() { @@ -196,9 +217,15 @@ impl Project { }); if !keep { cx.emit(Event::WorktreeRemoved(worktree.id())); + reset_active = Some(worktree.id()); } keep }); + + if self.active_worktree == reset_active { + self.active_worktree = self.worktrees.first().map(|w| w.id()); + cx.notify(); + } } pub fn match_paths<'a>(