Move methods querying window state into AsyncAppContext

This commit is contained in:
Antonio Scandurra 2023-05-02 19:28:58 +02:00
parent 4f6939732e
commit 70f8cf4cf6
9 changed files with 116 additions and 113 deletions

View file

@ -918,12 +918,18 @@ impl Workspace {
}
pub fn close_global(_: &CloseWindow, cx: &mut AppContext) {
let id = cx.window_ids().find(|&id| cx.window_is_active(id));
if let Some(id) = id {
//This can only get called when the window's project connection has been lost
//so we don't need to prompt the user for anything and instead just close the window
cx.remove_window(id);
}
cx.spawn(|mut cx| async move {
let id = cx
.window_ids()
.into_iter()
.find(|&id| cx.window_is_active(id));
if let Some(id) = id {
//This can only get called when the window's project connection has been lost
//so we don't need to prompt the user for anything and instead just close the window
cx.remove_window(id);
}
})
.detach();
}
pub fn close(
@ -948,19 +954,14 @@ impl Workspace {
) -> Task<Result<bool>> {
let active_call = self.active_call().cloned();
let window_id = cx.window_id();
let workspace_count = cx
.window_ids()
.collect::<Vec<_>>()
.into_iter()
.filter_map(|window_id| {
cx.app_context()
.root_view(window_id)?
.clone()
.downcast::<Workspace>()
})
.count();
cx.spawn(|this, mut cx| async move {
let workspace_count = cx
.window_ids()
.into_iter()
.filter_map(|window_id| cx.root_view(window_id)?.clone().downcast::<Workspace>())
.count();
if let Some(active_call) = active_call {
if !quitting
&& workspace_count == 1
@ -2849,10 +2850,10 @@ impl std::fmt::Debug for OpenPaths {
pub struct WorkspaceCreated(WeakViewHandle<Workspace>);
pub fn activate_workspace_for_project(
cx: &mut AppContext,
cx: &mut AsyncAppContext,
predicate: impl Fn(&mut Project, &mut ModelContext<Project>) -> bool,
) -> Option<WeakViewHandle<Workspace>> {
for window_id in cx.window_ids().collect::<Vec<_>>() {
for window_id in cx.window_ids() {
let handle = cx
.update_window(window_id, |cx| {
if let Some(workspace_handle) = cx.root_view().clone().downcast::<Workspace>() {
@ -2891,13 +2892,14 @@ pub fn open_paths(
> {
log::info!("open paths {:?}", abs_paths);
// Open paths in existing workspace if possible
let existing =
activate_workspace_for_project(cx, |project, cx| project.contains_paths(abs_paths, cx));
let app_state = app_state.clone();
let abs_paths = abs_paths.to_vec();
cx.spawn(|mut cx| async move {
// Open paths in existing workspace if possible
let existing = activate_workspace_for_project(&mut cx, |project, cx| {
project.contains_paths(&abs_paths, cx)
});
if let Some(existing) = existing {
Ok((
existing.clone(),
@ -2960,13 +2962,16 @@ pub fn join_remote_project(
cx: &mut AppContext,
) -> Task<Result<()>> {
cx.spawn(|mut cx| async move {
let existing_workspace = cx.update(|cx| {
cx.window_ids()
.filter_map(|window_id| cx.root_view(window_id)?.clone().downcast::<Workspace>())
.find(|workspace| {
let existing_workspace = cx
.window_ids()
.into_iter()
.filter_map(|window_id| cx.root_view(window_id)?.clone().downcast::<Workspace>())
.find(|workspace| {
cx.read_window(workspace.window_id(), |cx| {
workspace.read(cx).project().read(cx).remote_id() == Some(project_id)
})
});
.unwrap_or(false)
});
let workspace = if let Some(existing_workspace) = existing_workspace {
existing_workspace.downgrade()
@ -3035,24 +3040,25 @@ pub fn join_remote_project(
}
pub fn restart(_: &Restart, cx: &mut AppContext) {
let mut workspaces = cx
.window_ids()
.filter_map(|window_id| {
Some(
cx.root_view(window_id)?
.clone()
.downcast::<Workspace>()?
.downgrade(),
)
})
.collect::<Vec<_>>();
// If multiple windows have unsaved changes, and need a save prompt,
// prompt in the active window before switching to a different window.
workspaces.sort_by_key(|workspace| !cx.window_is_active(workspace.window_id()));
let should_confirm = cx.global::<Settings>().confirm_quit;
cx.spawn(|mut cx| async move {
let mut workspaces = cx
.window_ids()
.into_iter()
.filter_map(|window_id| {
Some(
cx.root_view(window_id)?
.clone()
.downcast::<Workspace>()?
.downgrade(),
)
})
.collect::<Vec<_>>();
// If multiple windows have unsaved changes, and need a save prompt,
// prompt in the active window before switching to a different window.
workspaces.sort_by_key(|workspace| !cx.window_is_active(workspace.window_id()));
if let (true, Some(workspace)) = (should_confirm, workspaces.first()) {
let answer = cx.prompt(
workspace.window_id(),