From 13212d274e86c5b0127d011288cc88496b859f86 Mon Sep 17 00:00:00 2001 From: Julia Date: Fri, 3 Mar 2023 16:06:03 -0800 Subject: [PATCH] Confirm restart if prompt-quit is enabled or there are unsaved changes --- crates/zed/src/zed.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 19721547bda187bb3be53083cf52edfacf37e0cb..2fbac3613ed3a6242c41db026e7b46b0d7f1aa50 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -381,7 +381,47 @@ pub fn build_window_options( } fn restart(_: &Restart, cx: &mut gpui::MutableAppContext) { - cx.platform().restart(); + 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())); + + let should_confirm = cx.global::().confirm_quit; + cx.spawn(|mut cx| async move { + if let (true, Some(workspace)) = (should_confirm, workspaces.first()) { + let answer = cx + .prompt( + workspace.window_id(), + PromptLevel::Info, + "Are you sure you want to restart?", + &["Restart", "Cancel"], + ) + .next() + .await; + if answer != Some(0) { + return Ok(()); + } + } + + // 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(true, cx) + }) + .await? + { + return Ok(()); + } + } + cx.platform().restart(); + anyhow::Ok(()) + }) + .detach_and_log_err(cx); } fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) {