diff --git a/src/features/app/__tests__/windowClose.test.ts b/src/features/app/__tests__/windowClose.test.ts index eac8fd2c38..e2fc98c04a 100644 --- a/src/features/app/__tests__/windowClose.test.ts +++ b/src/features/app/__tests__/windowClose.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import * as ipc from "@/platform/ipc"; +import * as keymap from "@/platform/keymap"; import { useProject } from "@/platform/project"; import { disableRemote, enableRemote } from "@/platform/remote"; import * as services from "@/workbench/contributions"; @@ -66,6 +67,8 @@ beforeEach(() => { }); afterEach(() => { + // A window that closes stays frozen until it is destroyed. + document.body.removeAttribute("inert"); uninstall?.(); uninstall = undefined; native.close = undefined; @@ -227,6 +230,73 @@ describe("native window close", () => { // A quit can start outside the window (Dock, logout, another window): its prompts are drawn // inside the window, so it has to come forward before asking. describe("quitting from outside the window", () => { + // A window that passed its checks waits at the barrier while a peer asks; an edit made then + // would close without being asked about, so the page takes no input until the batch decides. + // jsdom does not implement inert, so these check the attribute; WebKit/Chromium block input with it. + describe("waiting at the close barrier", () => { + const resumeKeys = vi.fn(); + const frozen = () => + document.body.hasAttribute("inert") && + vi.mocked(keymap.suspendKeyboardDispatch).mock.calls.length === 1 && + resumeKeys.mock.calls.length === 0; + let frozenWhileWaiting: boolean | undefined; + beforeEach(() => { + frozenWhileWaiting = undefined; + vi.spyOn(keymap, "suspendKeyboardDispatch").mockReturnValue(resumeKeys); + vi.mocked(ipc.commands.windowCloseContext).mockResolvedValue({ + quitting: true, + windows: 2, + hasDocumentWindows: false, + batch: true, + }); + }); + function waitAt(result: () => Promise) { + vi.mocked(ipc.commands.windowReadyToClose).mockImplementationOnce(async () => { + frozenWhileWaiting = frozen(); + return result(); + }); + } + + it("freezes the page and leaves it frozen as the window closes", async () => { + waitAt(async () => true); + const prevented = await close({}); + expect(frozenWhileWaiting).toBe(true); + expect(prevented).not.toHaveBeenCalled(); + expect(document.body.hasAttribute("inert")).toBe(true); + expect(resumeKeys).not.toHaveBeenCalled(); + }); + + it("unfreezes and refocuses when a peer cancels the batch", async () => { + const input = document.createElement("textarea"); + document.body.append(input); + input.focus(); + waitAt(async () => { + expect(document.activeElement).not.toBe(input); + return false; + }); + const prevented = await close({}); + expect(frozenWhileWaiting).toBe(true); + expect(prevented).toHaveBeenCalledOnce(); + expect(document.body.hasAttribute("inert")).toBe(false); + expect(resumeKeys).toHaveBeenCalledOnce(); + expect(document.activeElement).toBe(input); + expect(ipc.commands.appCancelQuit).not.toHaveBeenCalled(); + input.remove(); + }); + + it("unfreezes when waiting fails", async () => { + waitAt(async () => { + throw new Error("ipc down"); + }); + const prevented = await close({}); + expect(frozenWhileWaiting).toBe(true); + expect(prevented).toHaveBeenCalledOnce(); + expect(document.body.hasAttribute("inert")).toBe(false); + expect(resumeKeys).toHaveBeenCalledOnce(); + expect(ipc.commands.appCancelQuit).toHaveBeenCalledOnce(); + }); + }); + function recordingDeps(button: string) { const asked: PromptRequest[] = []; const d: CloseDeps = { diff --git a/src/features/app/closeGuard.ts b/src/features/app/closeGuard.ts index 094592a86b..a3a73a3d6e 100644 --- a/src/features/app/closeGuard.ts +++ b/src/features/app/closeGuard.ts @@ -24,6 +24,7 @@ import { type Settings, } from "@/platform/ipc"; import { t, tx } from "@/platform/i18n"; +import { suspendKeyboardDispatch } from "@/platform/keymap"; import { closeProject, provideLeaveProjectGuard, useProject } from "@/platform/project"; import { isRemoteWindow, shutdownRemoteBackend, useRemote } from "@/platform/remote"; import { workflowSettings } from "@/platform/workflowSettings"; @@ -313,6 +314,22 @@ function forgetTabs(): boolean { return true; } +/** + * Takes the page out of keyboard and pointer input; the result puts it back and refocuses what had + * focus. `inert` alone leaves the focused editor typing, so focus moves off it first. + */ +function freezeInput(): () => void { + const focused = document.activeElement; + if (focused instanceof HTMLElement) focused.blur(); + document.body.toggleAttribute("inert", true); + const resumeKeys = suspendKeyboardDispatch(); + return () => { + resumeKeys(); + document.body.toggleAttribute("inert", false); + if (focused instanceof HTMLElement && focused.isConnected) focused.focus(); + }; +} + async function onCloseRequested(deps: CloseDeps): Promise { if (closing) return false; closing = true; @@ -320,6 +337,7 @@ async function onCloseRequested(deps: CloseDeps): Promise { let quitting = false; let promptHeld = false; let batchAborted = false; + let frozen: (() => void) | undefined; const cancel = async () => { if (!batchAborted) { await (quitting ? commands.appCancelQuit() : commands.windowGroupCancelClose()); @@ -411,13 +429,16 @@ async function onCloseRequested(deps: CloseDeps): Promise { forgot = false; return await cancel(); } - if (!(await commands.windowReadyToClose())) { - batchAborted = true; - undoForget(); - maybeEditorService()?.cancelClose?.(); - return false; + // Every unsaved change was settled above; an edit made while peers still ask would close unasked. + frozen = freezeInput(); + if (await commands.windowReadyToClose()) { + frozen = undefined; + return true; } - return true; + batchAborted = true; + undoForget(); + maybeEditorService()?.cancelClose?.(); + return false; } catch (e) { undoForget(); maybeEditorService()?.cancelClose?.(); @@ -425,6 +446,7 @@ async function onCloseRequested(deps: CloseDeps): Promise { deps.onSaveFailed(e); return false; } finally { + frozen?.(); if (promptHeld) { promptHeld = false; void commands.windowEndPrompt().catch(() => {});