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
This commit is contained in:
Marshall Bowers 2023-12-06 19:10:15 -05:00 committed by GitHub
parent a4b271e063
commit c8cb1140b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,11 +22,35 @@ pub fn init(cx: &mut AppContext) {
cx.observe_new_views(RecentProjects::register).detach(); cx.observe_new_views(RecentProjects::register).detach();
} }
fn toggle( pub struct RecentProjects {
_: &mut Workspace, picker: View<Picker<RecentProjectsDelegate>>,
_: &OpenRecent, }
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> { impl RecentProjects {
fn new(delegate: RecentProjectsDelegate, cx: &mut ViewContext<Self>) -> Self {
Self {
picker: cx.build_view(|cx| Picker::new(delegate, cx)),
}
}
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|workspace, _: &OpenRecent, cx| {
let Some(recent_projects) = workspace.active_modal::<Self>(cx) else {
if let Some(handler) = Self::open(workspace, cx) {
handler.detach_and_log_err(cx);
}
return;
};
recent_projects.update(cx, |recent_projects, cx| {
recent_projects
.picker
.update(cx, |picker, cx| picker.cycle_selection(cx))
});
});
}
fn open(_: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<Task<Result<()>>> {
Some(cx.spawn(|workspace, mut cx| async move { Some(cx.spawn(|workspace, mut cx| async move {
let workspace_locations: Vec<_> = cx let workspace_locations: Vec<_> = cx
.background_executor() .background_executor()
@ -58,36 +82,6 @@ fn toggle(
})?; })?;
Ok(()) Ok(())
})) }))
}
pub struct RecentProjects {
picker: View<Picker<RecentProjectsDelegate>>,
}
impl RecentProjects {
fn new(delegate: RecentProjectsDelegate, cx: &mut ViewContext<Self>) -> Self {
Self {
picker: cx.build_view(|cx| Picker::new(delegate, cx)),
}
}
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|workspace, _: &OpenRecent, cx| {
let Some(recent_projects) = workspace.active_modal::<Self>(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) {
handler.detach_and_log_err(cx);
}
return;
};
recent_projects.update(cx, |recent_projects, cx| {
recent_projects
.picker
.update(cx, |picker, cx| picker.cycle_selection(cx))
});
});
} }
} }