diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 4ad807b357c33ae0385c6e1f88d351ed9b6650ff..efb586fe038efa205bf6a05dad48cbc94f32defc 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -2368,6 +2368,12 @@ impl WindowHandle { { cx.read_window(self, |root_view, _cx| root_view.clone()) } + + pub fn is_active(&self, cx: &WindowContext) -> Option { + cx.windows + .get(self.id) + .and_then(|window| window.as_ref().map(|window| window.active)) + } } impl Copy for WindowHandle {} diff --git a/crates/zed2/src/zed2.rs b/crates/zed2/src/zed2.rs index 8f4a9c6ddf8b260f08aa03d4314b1a4c649bd9a6..73faeaaaf42a6aca435a61bb21c94c09a63e90f1 100644 --- a/crates/zed2/src/zed2.rs +++ b/crates/zed2/src/zed2.rs @@ -460,36 +460,45 @@ fn quit(_: &mut Workspace, _: &Quit, cx: &mut gpui::ViewContext) { .collect::>() })?; - // // If multiple windows have unsaved changes, and need a save prompt, - // // prompt in the active window before switching to a different window. - // workspace_windows.sort_by_key(|window| window.is_active(&cx) == Some(false)); - - // if let (true, Some(window)) = (should_confirm, workspace_windows.first().copied()) { - // let answer = window.prompt( - // PromptLevel::Info, - // "Are you sure you want to quit?", - // &["Quit", "Cancel"], - // &mut cx, - // ); - - // if let Some(mut answer) = answer { - // let answer = answer.next().await; - // if answer != Some(0) { - // return Ok(()); - // } - // } - // } - - // // If the user cancels any save prompt, then keep the app open. - // for window in workspace_windows { - // if let Some(should_close) = window.update_root(&mut cx, |workspace, cx| { - // workspace.prepare_to_close(true, cx) - // }) { - // if !should_close.await? { - // return Ok(()); - // } - // } - // } + // If multiple windows have unsaved changes, and need a save prompt, + // prompt in the active window before switching to a different window. + cx.update(|_, cx| { + workspace_windows.sort_by_key(|window| window.is_active(&cx) == Some(false)); + }) + .log_err(); + + if let (true, Some(window)) = (should_confirm, workspace_windows.first().copied()) { + let answer = cx + .update(|_, cx| { + cx.prompt( + PromptLevel::Info, + "Are you sure you want to quit?", + &["Quit", "Cancel"], + ) + }) + .log_err(); + + if let Some(mut answer) = answer { + let answer = answer.await.ok(); + if answer != Some(0) { + return Ok(()); + } + } + } + + // If the user cancels any save prompt, then keep the app open. + for window in workspace_windows { + if let Some(should_close) = window + .update(&mut cx, |workspace, cx| { + workspace.prepare_to_close(true, cx) + }) + .log_err() + { + if !should_close.await? { + return Ok(()); + } + } + } cx.update(|_, cx| { cx.quit(); })?;