From b9eb875bf40e3ffc781bc7da536df0ecb067323e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 10 Jun 2022 17:26:33 -0700 Subject: [PATCH] Prompt to save changes before quitting the app --- crates/workspace/src/workspace.rs | 2 +- crates/zed/src/zed.rs | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 1c4f703be9..851c3403d2 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -911,7 +911,7 @@ impl Workspace { })) } - fn prepare_to_close(&mut self, cx: &mut ViewContext) -> Task> { + pub fn prepare_to_close(&mut self, cx: &mut ViewContext) -> Task> { self.save_all_internal(true, cx) } diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 0f4598b1b7..8a56777df4 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -226,7 +226,29 @@ pub fn build_window_options() -> WindowOptions<'static> { } fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) { - cx.platform().quit(); + let mut workspaces = cx + .window_ids() + .filter_map(|window_id| cx.root_view::(window_id)) + .collect::>(); + + // 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())); + + cx.spawn(|mut cx| async move { + // If the user cancels any save prompt, then keep the app open. + for workspace in workspaces { + if !workspace + .update(&mut cx, |workspace, cx| workspace.prepare_to_close(cx)) + .await? + { + return Ok(()); + } + } + cx.platform().quit(); + anyhow::Ok(()) + }) + .detach_and_log_err(cx); } fn about(_: &mut Workspace, _: &About, cx: &mut gpui::ViewContext) {