fix(app): keep a window waiting for its peers to close from taking edits it would lose (#7428)

When Quit or closing several project tabs closes windows in parallel, a window with nothing to ask about waits at windowReadyToClose for the others; while a peer showed its Unsaved Changes prompt, the waiting window still took input, and an edit made there closed unasked. The waiting window now blurs its focused element, goes inert and suspends keyboard dispatch while it waits; it is given back with focus restored when the close is cancelled or the wait fails.

Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-12-02 17:12:10 +08:00
1 джерело 04006f2ea7
коміт ae4a90b23a
2 files changed
+98 -6

No files matched your search

+70
Переглянути файл
@@ -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<boolean>) {
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 = {
+28 -6
Переглянути файл
@@ -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<boolean> {
if (closing) return false;
closing = true;
@@ -320,6 +337,7 @@ async function onCloseRequested(deps: CloseDeps): Promise<boolean> {
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<boolean> {
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<boolean> {
deps.onSaveFailed(e);
return false;
} finally {
frozen?.();
if (promptHeld) {
promptHeld = false;
void commands.windowEndPrompt().catch(() => {});