vcs_menu: Fix a circular view handle in modal picker.

Co-authored-by: Julia Risley <julia@zed.dev>
This commit is contained in:
Piotr Osiewicz 2023-10-25 18:34:14 +02:00
parent 3a369bc207
commit 39480364bd

View file

@ -16,7 +16,7 @@ actions!(branches, [OpenRecent]);
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
Picker::<BranchListDelegate>::init(cx); Picker::<BranchListDelegate>::init(cx);
cx.add_async_action(toggle); cx.add_action(toggle);
} }
pub type BranchList = Picker<BranchListDelegate>; pub type BranchList = Picker<BranchListDelegate>;
@ -24,30 +24,29 @@ pub fn build_branch_list(
workspace: ViewHandle<Workspace>, workspace: ViewHandle<Workspace>,
cx: &mut ViewContext<BranchList>, cx: &mut ViewContext<BranchList>,
) -> Result<BranchList> { ) -> Result<BranchList> {
Ok(Picker::new(BranchListDelegate::new(workspace, 29, cx)?, cx) let delegate = workspace.read_with(cx, |workspace, cx| {
.with_theme(|theme| theme.picker.clone())) BranchListDelegate::new(workspace, cx.handle(), 29, cx)
})?;
Ok(Picker::new(delegate, cx).with_theme(|theme| theme.picker.clone()))
} }
fn toggle( fn toggle(
_: &mut Workspace, workspace: &mut Workspace,
_: &OpenRecent, _: &OpenRecent,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> { ) -> Result<()> {
Some(cx.spawn(|workspace, mut cx| async move { // Modal branch picker has a longer trailoff than a popover one.
workspace.update(&mut cx, |workspace, cx| { let delegate = BranchListDelegate::new(workspace, cx.handle(), 70, cx)?;
// Modal branch picker has a longer trailoff than a popover one. workspace.toggle_modal(cx, |_, cx| {
let delegate = BranchListDelegate::new(cx.handle(), 70, cx)?; cx.add_view(|cx| {
workspace.toggle_modal(cx, |_, cx| { Picker::new(delegate, cx)
cx.add_view(|cx| { .with_theme(|theme| theme.picker.clone())
Picker::new(delegate, cx) .with_max_size(800., 1200.)
.with_theme(|theme| theme.picker.clone()) })
.with_max_size(800., 1200.) });
})
}); Ok(())
Ok::<_, anyhow::Error>(())
})??;
Ok(())
}))
} }
pub struct BranchListDelegate { pub struct BranchListDelegate {
@ -62,15 +61,16 @@ pub struct BranchListDelegate {
impl BranchListDelegate { impl BranchListDelegate {
fn new( fn new(
workspace: ViewHandle<Workspace>, workspace: &Workspace,
handle: ViewHandle<Workspace>,
branch_name_trailoff_after: usize, branch_name_trailoff_after: usize,
cx: &AppContext, cx: &AppContext,
) -> Result<Self> { ) -> Result<Self> {
let project = workspace.read(cx).project().read(&cx); let project = workspace.project().read(&cx);
let Some(worktree) = project.visible_worktrees(cx).next() else { let Some(worktree) = project.visible_worktrees(cx).next() else {
bail!("Cannot update branch list as there are no visible worktrees") bail!("Cannot update branch list as there are no visible worktrees")
}; };
let mut cwd = worktree.read(cx).abs_path().to_path_buf(); let mut cwd = worktree.read(cx).abs_path().to_path_buf();
cwd.push(".git"); cwd.push(".git");
let Some(repo) = project.fs().open_repo(&cwd) else { let Some(repo) = project.fs().open_repo(&cwd) else {
@ -79,13 +79,14 @@ impl BranchListDelegate {
let all_branches = repo.lock().branches()?; let all_branches = repo.lock().branches()?;
Ok(Self { Ok(Self {
matches: vec![], matches: vec![],
workspace, workspace: handle,
all_branches, all_branches,
selected_index: 0, selected_index: 0,
last_query: Default::default(), last_query: Default::default(),
branch_name_trailoff_after, branch_name_trailoff_after,
}) })
} }
fn display_error_toast(&self, message: String, cx: &mut ViewContext<BranchList>) { fn display_error_toast(&self, message: String, cx: &mut ViewContext<BranchList>) {
const GIT_CHECKOUT_FAILURE_ID: usize = 2048; const GIT_CHECKOUT_FAILURE_ID: usize = 2048;
self.workspace.update(cx, |model, ctx| { self.workspace.update(cx, |model, ctx| {