From 39480364bd22ae55acf58a7f1b9cab242d261f07 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:34:14 +0200 Subject: [PATCH] vcs_menu: Fix a circular view handle in modal picker. Co-authored-by: Julia Risley --- crates/vcs_menu/src/lib.rs | 49 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/crates/vcs_menu/src/lib.rs b/crates/vcs_menu/src/lib.rs index dce3724ccd..7e89e489ff 100644 --- a/crates/vcs_menu/src/lib.rs +++ b/crates/vcs_menu/src/lib.rs @@ -16,7 +16,7 @@ actions!(branches, [OpenRecent]); pub fn init(cx: &mut AppContext) { Picker::::init(cx); - cx.add_async_action(toggle); + cx.add_action(toggle); } pub type BranchList = Picker; @@ -24,30 +24,29 @@ pub fn build_branch_list( workspace: ViewHandle, cx: &mut ViewContext, ) -> Result { - Ok(Picker::new(BranchListDelegate::new(workspace, 29, cx)?, cx) - .with_theme(|theme| theme.picker.clone())) + let delegate = workspace.read_with(cx, |workspace, cx| { + BranchListDelegate::new(workspace, cx.handle(), 29, cx) + })?; + + Ok(Picker::new(delegate, cx).with_theme(|theme| theme.picker.clone())) } fn toggle( - _: &mut Workspace, + workspace: &mut Workspace, _: &OpenRecent, cx: &mut ViewContext, -) -> Option>> { - Some(cx.spawn(|workspace, mut cx| async move { - workspace.update(&mut cx, |workspace, cx| { - // Modal branch picker has a longer trailoff than a popover one. - let delegate = BranchListDelegate::new(cx.handle(), 70, cx)?; - workspace.toggle_modal(cx, |_, cx| { - cx.add_view(|cx| { - Picker::new(delegate, cx) - .with_theme(|theme| theme.picker.clone()) - .with_max_size(800., 1200.) - }) - }); - Ok::<_, anyhow::Error>(()) - })??; - Ok(()) - })) +) -> Result<()> { + // Modal branch picker has a longer trailoff than a popover one. + let delegate = BranchListDelegate::new(workspace, cx.handle(), 70, cx)?; + workspace.toggle_modal(cx, |_, cx| { + cx.add_view(|cx| { + Picker::new(delegate, cx) + .with_theme(|theme| theme.picker.clone()) + .with_max_size(800., 1200.) + }) + }); + + Ok(()) } pub struct BranchListDelegate { @@ -62,15 +61,16 @@ pub struct BranchListDelegate { impl BranchListDelegate { fn new( - workspace: ViewHandle, + workspace: &Workspace, + handle: ViewHandle, branch_name_trailoff_after: usize, cx: &AppContext, ) -> Result { - let project = workspace.read(cx).project().read(&cx); - + let project = workspace.project().read(&cx); let Some(worktree) = project.visible_worktrees(cx).next() else { bail!("Cannot update branch list as there are no visible worktrees") }; + let mut cwd = worktree.read(cx).abs_path().to_path_buf(); cwd.push(".git"); let Some(repo) = project.fs().open_repo(&cwd) else { @@ -79,13 +79,14 @@ impl BranchListDelegate { let all_branches = repo.lock().branches()?; Ok(Self { matches: vec![], - workspace, + workspace: handle, all_branches, selected_index: 0, last_query: Default::default(), branch_name_trailoff_after, }) } + fn display_error_toast(&self, message: String, cx: &mut ViewContext) { const GIT_CHECKOUT_FAILURE_ID: usize = 2048; self.workspace.update(cx, |model, ctx| {