From 20eeb78251a866f8ccd86b0ced795e5f8a24cc02 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 4 Nov 2024 18:17:11 +0800 Subject: [PATCH] chore: Update BranchListDelegate to use WeakView (#20157) --- crates/vcs_menu/src/lib.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/vcs_menu/src/lib.rs b/crates/vcs_menu/src/lib.rs index 8f73153dd8..f165c91bfe 100644 --- a/crates/vcs_menu/src/lib.rs +++ b/crates/vcs_menu/src/lib.rs @@ -1,10 +1,11 @@ -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use fuzzy::{StringMatch, StringMatchCandidate}; use git::repository::Branch; use gpui::{ actions, rems, AnyElement, AppContext, AsyncAppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, InteractiveElement, IntoElement, ParentElement, Render, - SharedString, Styled, Subscription, Task, View, ViewContext, VisualContext, WindowContext, + SharedString, Styled, Subscription, Task, View, ViewContext, VisualContext, WeakView, + WindowContext, }; use picker::{Picker, PickerDelegate}; use project::ProjectPath; @@ -95,7 +96,7 @@ impl BranchEntry { pub struct BranchListDelegate { matches: Vec, all_branches: Vec, - workspace: View, + workspace: WeakView, selected_index: usize, last_query: String, /// Max length of branch name before we truncate it and add a trailing `...`. @@ -122,7 +123,7 @@ impl BranchListDelegate { Ok(Self { matches: vec![], - workspace, + workspace: workspace.downgrade(), all_branches, selected_index: 0, last_query: Default::default(), @@ -235,8 +236,13 @@ impl PickerDelegate for BranchListDelegate { let branch = branch.clone(); |picker, mut cx| async move { let branch_change_task = picker.update(&mut cx, |this, cx| { - let project = this.delegate.workspace.read(cx).project().read(cx); + let workspace = this + .delegate + .workspace + .upgrade() + .ok_or_else(|| anyhow!("workspace was dropped"))?; + let project = workspace.read(cx).project().read(cx); let branch_to_checkout = match branch { BranchEntry::Branch(branch) => branch.string, BranchEntry::NewBranch { name: branch_name } => branch_name,