From 1ba2ce55bb0d492804745fb828378ce7014acf8e Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 21:43:58 +0800 Subject: [PATCH] fix(macos): keep project tab close confirmations visible (#7501) Closing a macOS project tab group could cover the running-agent confirmation when a peer returned to Welcome and took focus. Confirmations and the Welcome transition now take turns, and background web views stay attached while their close checks are pending. --- src-tauri/src/commands/window.rs | 5 ++- src-tauri/src/lib.rs | 6 +++ src-tauri/src/macos.rs | 6 +++ src-tauri/src/window_groups.rs | 7 --- .../app/__tests__/windowClose.test.ts | 44 +++++++++++++++++++ src/features/app/closeGuard.ts | 22 ++++++---- 6 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/commands/window.rs b/src-tauri/src/commands/window.rs index c5ea422e0..dd97844e0 100644 --- a/src-tauri/src/commands/window.rs +++ b/src-tauri/src/commands/window.rs @@ -393,8 +393,9 @@ pub fn window_close_context(app: AppHandle, window: Window, windows: State<'_, W }; let quitting = windows.quitting(); let has_document_windows = !windows::document_windows(&app, window.label()).is_empty(); - let hidden_tab = windows.groups.lock().group(window.label()).is_some_and(|g| g.selected != window.label()); - let batch = quitting || pending.len() > 1 || windows.in_tab_close_batch(window.label()) || hidden_tab; + // AppKit can deliver the selected tab's close before its siblings have registered theirs. + let grouped = windows.groups.lock().group(window.label()).is_some(); + let batch = quitting || pending.len() > 1 || windows.in_tab_close_batch(window.label()) || grouped; CloseContext { quitting, windows: count as u32, has_document_windows, batch } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 39f5cd29e..ea6fa2f31 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -291,6 +291,12 @@ pub fn run() { } #[cfg(target_os = "linux")] linux_window_state::save_before_close(window.app_handle()); + // A background tab's detached page is suspended: it would never answer the close, and a + // prompt it shows would stay off screen while the rest of the group closes around it. + #[cfg(target_os = "macos")] + if let Some(webview) = window.app_handle().get_webview_window(window.label()) { + macos::attach(&webview); + } if let Some(w) = window.try_state::() { w.close_requested(window.label()); } diff --git a/src-tauri/src/macos.rs b/src-tauri/src/macos.rs index 0fb0ce1bb..a3daf5d28 100644 --- a/src-tauri/src/macos.rs +++ b/src-tauri/src/macos.rs @@ -189,6 +189,12 @@ fn detach_if_background(window: &WebviewWindow, generation: u64) { if !BACKGROUND.with_borrow(|tabs| tabs.due(window.label(), generation)) { return; } + // Selecting a peer for its close dialog can schedule another detach. Keep this + // page attached until its own close guard has answered. + if window.state::().is_closing(window.label()) { + BACKGROUND.with_borrow_mut(|tabs| tabs.cancel(window.label())); + return; + } let Some(native) = native(window) else { return }; // A visible window is never a background tab. if native.occlusionState().contains(NSWindowOcclusionState::Visible) { diff --git a/src-tauri/src/window_groups.rs b/src-tauri/src/window_groups.rs index 40b3dc1d7..a027ba49e 100644 --- a/src-tauri/src/window_groups.rs +++ b/src-tauri/src/window_groups.rs @@ -365,13 +365,6 @@ pub fn cancel_close(app: &AppHandle, caller: &str) { return; } state.close_settled(caller); - // For a red-button group close where this tab stayed open (e.g. returned to Welcome), - // select it so closing siblings disappear behind it without flashing. - if state.groups.lock().group(caller).is_some() - && let Some(window) = app.get_webview_window(caller) - { - windows::focus(&window); - } if !state.has_other_pending_closes(caller) && !state.quitting() { let app = app.clone(); tauri::async_runtime::spawn(async move { diff --git a/src/features/app/__tests__/windowClose.test.ts b/src/features/app/__tests__/windowClose.test.ts index e2fc98c04..f97067896 100644 --- a/src/features/app/__tests__/windowClose.test.ts +++ b/src/features/app/__tests__/windowClose.test.ts @@ -125,6 +125,50 @@ describe("native window close", () => { expect(forget).not.toHaveBeenCalled(); }); + it("keeps a tab returning to Welcome from covering a peer's close dialog", async () => { + vi.mocked(ipc.commands.windowCloseContext).mockResolvedValue({ + quitting: false, + windows: 1, + hasDocumentWindows: false, + batch: true, + }); + let releasePeer!: (proceed: boolean) => void; + vi.mocked(ipc.commands.windowBeginPrompt).mockReturnValue(new Promise((resolve) => (releasePeer = resolve))); + + const closing = close({}); + await vi.waitFor(() => expect(ipc.commands.windowBeginPrompt).toHaveBeenCalledOnce()); + expect(ipc.commands.closeProject).not.toHaveBeenCalled(); + + releasePeer(true); + expect(await closing).toHaveBeenCalledOnce(); + expect(ipc.commands.closeProject).toHaveBeenCalledOnce(); + expect(ipc.commands.closeProject).toHaveBeenCalledBefore(vi.mocked(ipc.commands.windowGroupCancelClose)); + }); + + it("keeps the agent task's tab visible through confirmation and the transition to Welcome", async () => { + vi.mocked(ipc.commands.windowCloseContext).mockResolvedValue({ + quitting: false, + windows: 1, + hasDocumentWindows: false, + batch: true, + }); + deps.runningAgentTasks = async () => [{ id: "t1", title: "Refactor auth" }]; + let answer!: (result: { button: string; checked: boolean }) => void; + deps.ask = vi.fn(() => new Promise<{ button: string; checked: boolean }>((resolve) => (answer = resolve))); + + const closing = close({}); + await vi.waitFor(() => expect(deps.ask).toHaveBeenCalledOnce()); + expect(ipc.commands.windowBeginPrompt).toHaveBeenCalledBefore(vi.mocked(deps.ask)); + expect(ipc.commands.closeProject).not.toHaveBeenCalled(); + expect(ipc.commands.windowReadyToClose).not.toHaveBeenCalled(); + expect(ipc.commands.windowEndPrompt).not.toHaveBeenCalled(); + + answer({ button: "pause", checked: false }); + expect(await closing).toHaveBeenCalledOnce(); + expect(ipc.commands.windowBeginPrompt).toHaveBeenCalledOnce(); + expect(ipc.commands.closeProject).toHaveBeenCalledOnce(); + }); + it("closes the Welcome window without the exit prompt when confirm exit is on", async () => { const prevented = await close({ confirmExit: true }, false, true); expect(prevented).not.toHaveBeenCalled(); diff --git a/src/features/app/closeGuard.ts b/src/features/app/closeGuard.ts index a3a73a3d6..1636559c3 100644 --- a/src/features/app/closeGuard.ts +++ b/src/features/app/closeGuard.ts @@ -338,6 +338,13 @@ async function onCloseRequested(deps: CloseDeps): Promise { let promptHeld = false; let batchAborted = false; let frozen: (() => void) | undefined; + const acquirePromptTurn = async () => { + if (!promptHeld) { + promptHeld = await commands.windowBeginPrompt(); + batchAborted = !promptHeld; + } + return promptHeld; + }; const cancel = async () => { if (!batchAborted) { await (quitting ? commands.appCancelQuit() : commands.windowGroupCancelClose()); @@ -359,14 +366,7 @@ async function onCloseRequested(deps: CloseDeps): Promise { deps = { ...deps, ask: async (request) => { - if (!promptHeld) { - const proceed = await commands.windowBeginPrompt(); - if (!proceed) { - batchAborted = true; - return { button: "cancel", checked: false }; - } - promptHeld = true; - } + if (!(await acquirePromptTurn())) return { button: "cancel", checked: false }; await raiseWindowForPrompts(); return baseAsk(request); }, @@ -422,6 +422,12 @@ async function onCloseRequested(deps: CloseDeps): Promise { return await cancel(); } } + // Returning to Welcome detaches and focuses this window. Wait for any peer's dialog + // before doing that, and hold the turn until the native transition is complete. + if (welcome && ctx.batch && !(await acquirePromptTurn())) { + maybeEditorService()?.cancelClose?.(); + return false; + } forgot = forgetTabs(); if (!forgot && (!welcome || remote.session)) maybeEditorService()?.flushSession?.(); if (welcome) {