From 6659aea13be405c99eef4b8e32ec29401fb6cdaa Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 4 Feb 2025 11:09:46 +0200 Subject: [PATCH] Disallow multiple quit confirmations (#24180) Closes https://github.com/zed-industries/zed/issues/10192 , again. Release Notes: - Fixed multiple save modals appearing when app is being closed multiple times --- crates/zed/src/zed.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index c8196b4527..220623dfe2 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -44,6 +44,7 @@ use settings::{ }; use std::any::TypeId; use std::path::PathBuf; +use std::sync::atomic::{self, AtomicBool}; use std::time::Duration; use std::{borrow::Cow, ops::Deref, path::Path, sync::Arc}; use terminal_view::terminal_panel::{self, TerminalPanel}; @@ -937,7 +938,12 @@ fn install_cli( .detach_and_prompt_err("Error installing zed cli", window, cx, |_, _, _| None); } +static WAITING_QUIT_CONFIRMATION: AtomicBool = AtomicBool::new(false); fn quit(_: &Quit, cx: &mut App) { + if WAITING_QUIT_CONFIRMATION.load(atomic::Ordering::Acquire) { + return; + } + let should_confirm = WorkspaceSettings::get_global(cx).confirm_quit; cx.spawn(|mut cx| async move { let mut workspace_windows = cx.update(|cx| { @@ -954,23 +960,27 @@ fn quit(_: &Quit, cx: &mut App) { }) .log_err(); - if let (true, Some(workspace)) = (should_confirm, workspace_windows.first().copied()) { - let answer = workspace - .update(&mut cx, |_, window, cx| { - window.prompt( - PromptLevel::Info, - "Are you sure you want to quit?", - None, - &["Quit", "Cancel"], - cx, - ) - }) - .log_err(); + if should_confirm { + if let Some(workspace) = workspace_windows.first() { + let answer = workspace + .update(&mut cx, |_, window, cx| { + window.prompt( + PromptLevel::Info, + "Are you sure you want to quit?", + None, + &["Quit", "Cancel"], + cx, + ) + }) + .log_err(); - if let Some(answer) = answer { - let answer = answer.await.ok(); - if answer != Some(0) { - return Ok(()); + if let Some(answer) = answer { + WAITING_QUIT_CONFIRMATION.store(true, atomic::Ordering::Release); + let answer = answer.await.ok(); + WAITING_QUIT_CONFIRMATION.store(false, atomic::Ordering::Release); + if answer != Some(0) { + return Ok(()); + } } } }