From c8cb1140b90bdcc9d75bd0191a23f4384a2c5a69 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Wed, 6 Dec 2023 19:10:15 -0500 Subject: [PATCH] Clean up how we open the recent projects picker (#3526) This PR performs some light cleanup of how we open the recent projects picker, to bring it more in-line with our other picker code. Release Notes: - N/A --- .../recent_projects2/src/recent_projects.rs | 76 +++++++++---------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/crates/recent_projects2/src/recent_projects.rs b/crates/recent_projects2/src/recent_projects.rs index 03cd042f82..f6c2e0a2a2 100644 --- a/crates/recent_projects2/src/recent_projects.rs +++ b/crates/recent_projects2/src/recent_projects.rs @@ -22,44 +22,6 @@ pub fn init(cx: &mut AppContext) { cx.observe_new_views(RecentProjects::register).detach(); } -fn toggle( - _: &mut Workspace, - _: &OpenRecent, - cx: &mut ViewContext, -) -> Option>> { - Some(cx.spawn(|workspace, mut cx| async move { - let workspace_locations: Vec<_> = cx - .background_executor() - .spawn(async { - WORKSPACE_DB - .recent_workspaces_on_disk() - .await - .unwrap_or_default() - .into_iter() - .map(|(_, location)| location) - .collect() - }) - .await; - - workspace.update(&mut cx, |workspace, cx| { - if !workspace_locations.is_empty() { - let weak_workspace = cx.view().downgrade(); - workspace.toggle_modal(cx, |cx| { - let delegate = - RecentProjectsDelegate::new(weak_workspace, workspace_locations, true); - - RecentProjects::new(delegate, cx) - }); - } else { - workspace.show_notification(0, cx, |cx| { - cx.build_view(|_| MessageNotification::new("No recent projects to open.")) - }) - } - })?; - Ok(()) - })) -} - pub struct RecentProjects { picker: View>, } @@ -74,9 +36,7 @@ impl RecentProjects { fn register(workspace: &mut Workspace, _: &mut ViewContext) { workspace.register_action(|workspace, _: &OpenRecent, cx| { let Some(recent_projects) = workspace.active_modal::(cx) else { - // TODO(Marshall): Is this how we should be handling this? - // The previous code was using `cx.add_async_action` to invoke `toggle`. - if let Some(handler) = toggle(workspace, &OpenRecent, cx) { + if let Some(handler) = Self::open(workspace, cx) { handler.detach_and_log_err(cx); } return; @@ -89,6 +49,40 @@ impl RecentProjects { }); }); } + + fn open(_: &mut Workspace, cx: &mut ViewContext) -> Option>> { + Some(cx.spawn(|workspace, mut cx| async move { + let workspace_locations: Vec<_> = cx + .background_executor() + .spawn(async { + WORKSPACE_DB + .recent_workspaces_on_disk() + .await + .unwrap_or_default() + .into_iter() + .map(|(_, location)| location) + .collect() + }) + .await; + + workspace.update(&mut cx, |workspace, cx| { + if !workspace_locations.is_empty() { + let weak_workspace = cx.view().downgrade(); + workspace.toggle_modal(cx, |cx| { + let delegate = + RecentProjectsDelegate::new(weak_workspace, workspace_locations, true); + + RecentProjects::new(delegate, cx) + }); + } else { + workspace.show_notification(0, cx, |cx| { + cx.build_view(|_| MessageNotification::new("No recent projects to open.")) + }) + } + })?; + Ok(()) + })) + } } impl EventEmitter for RecentProjects {}