fix(desktop): hold-to-quit no longer gets stuck (#9141)

This commit is contained in:
Alex Southwell authored and GitHub committed 2026-09-01 21:19:00 -07:00
1 parent cde12790d7
commit b8262b4122
4 files changed
+159 -8

No files matched your search

@@ -105,6 +105,8 @@ function makeFakeBrowserWindow() {
restore: vi.fn(),
setBackgroundColor: vi.fn(),
setAutoHideCursor: vi.fn(),
setFullScreen: vi.fn(),
setOpacity: vi.fn(),
setTitle: vi.fn(),
setTitleBarOverlay: vi.fn(),
show: vi.fn(),
@@ -127,6 +129,8 @@ function makeFakeBrowserWindow() {
setZoomLevel: webContents.setZoomLevel,
setBackgroundThrottling: webContents.setBackgroundThrottling,
setAutoHideCursor: window.setAutoHideCursor,
setFullScreen: window.setFullScreen,
setOpacity: window.setOpacity,
webContentsListeners,
windowListeners,
};
@@ -394,6 +398,25 @@ const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | n
});
describe("DesktopWindow", () => {
it("leaves fullscreen before concealing a pending quit", () => {
const fakeWindow = makeFakeBrowserWindow();
DesktopWindow.concealPendingQuitWindow(fakeWindow.window);
assert.deepEqual(fakeWindow.setOpacity.mock.calls, [[0]]);
fakeWindow.setOpacity.mockClear();
fakeWindow.isFullScreen.mockReturnValue(true);
DesktopWindow.concealPendingQuitWindow(fakeWindow.window);
assert.deepEqual(fakeWindow.setFullScreen.mock.calls, [[false]]);
assert.deepEqual(fakeWindow.setOpacity.mock.calls, [[0]]);
fakeWindow.setOpacity.mockClear();
fakeWindow.isFullScreen.mockReturnValue(false);
fakeWindow.isDestroyed.mockReturnValue(true);
DesktopWindow.concealPendingQuitWindow(fakeWindow.window);
assert.equal(fakeWindow.setOpacity.mock.calls.length, 0);
});
it("restores bounds only when the window fits within a connected display", () => {
const persistedBounds = { x: 2040, y: 80, width: 1320, height: 880 };
const displays = [
+18
View File
@@ -207,6 +207,21 @@ export function isRetryableDevelopmentRendererLoadFailure(input: {
);
}
export function concealPendingQuitWindow(
window: Pick<
Electron.BrowserWindow,
"isDestroyed" | "isFullScreen" | "setFullScreen" | "setOpacity"
>,
): void {
if (window.isDestroyed()) return;
if (window.isFullScreen()) {
window.setFullScreen(false);
}
// Electron implements window opacity on macOS and Windows. Linux keeps the
// release-gated quit behavior but cannot make the pending window disappear.
window.setOpacity(0);
}
function getWindowTitleBarOptions(
shouldUseDarkColors: boolean,
platform: NodeJS.Platform,
@@ -570,6 +585,9 @@ export const make = Effect.gen(function* () {
window.webContents.send(QUIT_SHORTCUT_CHANNEL, hint);
}
},
// Keep the transparent window focused until the physical shortcut is
// released so its remaining repeats cannot reach the next app.
concealWindow: () => concealPendingQuitWindow(window),
quit: () => {
void runPromise(electronApp.quit);
},
+83 -2
View File
@@ -32,11 +32,13 @@ function makeHarness(options?: {
getMode?: () => Promise<QuitConfirmationMode>;
}) {
const notifications: Array<QuitShortcutHintEvent> = [];
const concealWindow = vi.fn();
const quit = vi.fn();
const handler = makeQuitShortcutHandler({
platform: options?.platform ?? "darwin",
getMode: options?.getMode ?? (() => Promise.resolve(options?.mode ?? "hold")),
notify: (event) => notifications.push(event),
concealWindow,
quit,
});
const preventDefault = vi.fn();
@@ -57,7 +59,7 @@ function makeHarness(options?: {
await send(makeInput({ isAutoRepeat: true, ...repeatOverrides }));
}
};
return { notifications, quit, preventDefault, send, holdFor };
return { notifications, concealWindow, quit, preventDefault, send, holdFor };
}
describe("makeQuitShortcutHandler", () => {
@@ -82,10 +84,11 @@ describe("makeQuitShortcutHandler", () => {
expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
});
it("quits after a completed hold is released", async () => {
it("conceals a completed hold, then quits after release", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
expect(harness.concealWindow).toHaveBeenCalledTimes(1);
expect(harness.quit).not.toHaveBeenCalled();
await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
expect(harness.quit).not.toHaveBeenCalled();
@@ -94,6 +97,81 @@ describe("makeQuitShortcutHandler", () => {
expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
});
it("keeps a concealed hold committed when another key is pressed", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
await harness.holdFor(QUIT_HOLD_DURATION_MS);
await harness.send(makeInput({ key: "Shift", shift: true }));
expect(harness.concealWindow).toHaveBeenCalledTimes(1);
expect(harness.quit).not.toHaveBeenCalled();
vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
expect(harness.quit).toHaveBeenCalledTimes(1);
});
it("keeps a concealed hold committed through a fresh Cmd+Q press", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
await harness.holdFor(QUIT_HOLD_DURATION_MS);
await harness.send(makeInput({}));
expect(harness.concealWindow).toHaveBeenCalledTimes(1);
expect(harness.quit).not.toHaveBeenCalled();
await harness.send(makeInput({ type: "keyUp" }));
expect(harness.quit).toHaveBeenCalledTimes(1);
});
it("quits when a completed hold goes quiet without release events", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
await harness.holdFor(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS * 2);
// If neither keyUp reaches the handler, continued repeats must keep the
// app alive. Once they stop, the quiet period is the release signal.
expect(harness.quit).not.toHaveBeenCalled();
vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
expect(harness.quit).toHaveBeenCalledTimes(1);
expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
});
it("waits for slow repeats to stop before quitting", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
vi.advanceTimersByTime(300);
await harness.send(makeInput({ isAutoRepeat: true }));
vi.advanceTimersByTime(900);
await harness.send(makeInput({ isAutoRepeat: true }));
vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
expect(harness.quit).not.toHaveBeenCalled();
vi.advanceTimersByTime(300);
await harness.send(makeInput({ isAutoRepeat: true }));
vi.advanceTimersByTime(1_799);
expect(harness.quit).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(harness.quit).toHaveBeenCalledTimes(1);
});
it("uses the initial repeat delay when the first repeat completes the hold", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + 100);
await harness.send(makeInput({ isAutoRepeat: true }));
vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
expect(harness.quit).not.toHaveBeenCalled();
vi.advanceTimersByTime(1_999);
expect(harness.quit).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(harness.quit).toHaveBeenCalledTimes(1);
});
it("waits for Q release when Cmd is released first", async () => {
const harness = makeHarness();
await harness.send(makeInput({}));
@@ -115,6 +193,7 @@ describe("makeQuitShortcutHandler", () => {
await harness.send(makeInput({ type: "keyUp" }));
expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
expect(harness.concealWindow).not.toHaveBeenCalled();
expect(harness.quit).not.toHaveBeenCalled();
});
@@ -130,6 +209,7 @@ describe("makeQuitShortcutHandler", () => {
it("quits without showing a hint in direct mode", async () => {
const harness = makeHarness({ mode: "direct" });
await harness.send(makeInput({}));
expect(harness.concealWindow).not.toHaveBeenCalled();
expect(harness.quit).toHaveBeenCalledTimes(1);
expect(harness.notifications).toEqual([]);
});
@@ -223,6 +303,7 @@ describe("makeQuitShortcutHandler", () => {
await harness.send(makeInput({}));
vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
await harness.send(makeInput({}));
expect(harness.concealWindow).not.toHaveBeenCalled();
expect(harness.quit).toHaveBeenCalledTimes(1);
expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
});
+35 -6
View File
@@ -11,9 +11,12 @@ export const QUIT_DOUBLE_PRESS_MS = 500;
// release: macOS suppresses a letter keyUp while the command key is down, so a
// tap release can go completely unseen and a release-based timer would quit
// anyway. Once held, quitting waits for Q keyUp or a quiet grace period after
// modifier keyUp so repeats cannot reach the next app. Keyboards with
// repeats stop so they cannot reach the next app. Keyboards with
// auto-repeat disabled fall back to the application menu Quit action.
export const QUIT_HOLD_RELEASE_GRACE_MS = 600;
// A slow repeat rate can exceed the fixed grace. Waiting for two observed
// cadences keeps the timer behind the next repeat without slowing normal rates.
const QUIT_HOLD_REPEAT_CADENCE_MULTIPLIER = 2;
export interface QuitHoldKeyInput {
readonly type: string;
@@ -29,6 +32,7 @@ export interface QuitShortcutOptions {
readonly platform: NodeJS.Platform;
readonly getMode: () => Promise<QuitConfirmationMode>;
readonly notify: (event: QuitShortcutHintEvent) => void;
readonly concealWindow: () => void;
readonly quit: () => void;
}
@@ -45,6 +49,8 @@ export function makeQuitShortcutHandler(
let quitOnRelease = false;
let heldSince = 0;
let lastPressAt = 0;
let lastRepeatAt = 0;
let repeatCadenceMs = 0;
// Incremented when a press is superseded or explicitly cancelled. A plain
// key release does not invalidate its pending mode read: direct mode and a
// completed second press must still be honored after that read settles.
@@ -64,6 +70,8 @@ export function makeQuitShortcutHandler(
holding = false;
armed = false;
quitOnRelease = false;
lastRepeatAt = 0;
repeatCadenceMs = 0;
if (keepHint) return;
mode = undefined;
@@ -80,6 +88,15 @@ export function makeQuitShortcutHandler(
options.quit();
};
const quitAfterQuietPeriod = () => {
clearWatchdog();
const quietPeriodMs = Math.max(
QUIT_HOLD_RELEASE_GRACE_MS,
repeatCadenceMs * QUIT_HOLD_REPEAT_CADENCE_MULTIPLIER,
);
watchdog = setTimeout(quitNow, quietPeriodMs);
};
return (event, input) => {
const key = input.key.toLowerCase();
if (input.type === "keyUp") {
@@ -91,20 +108,31 @@ export function makeQuitShortcutHandler(
if (!quitOnRelease) {
release(false, true);
} else {
watchdog = setTimeout(quitNow, QUIT_HOLD_RELEASE_GRACE_MS);
quitAfterQuietPeriod();
}
}
return;
}
if (input.type !== "keyDown") return;
if (quitOnRelease && input.isAutoRepeat && key === "q") {
const modifierDown = options.platform === "darwin" ? input.meta : input.control;
if (input.isAutoRepeat && modifierDown && key === "q") {
const now = Date.now();
repeatCadenceMs = now - (lastRepeatAt === 0 ? heldSince : lastRepeatAt);
lastRepeatAt = now;
}
if (quitOnRelease) {
event.preventDefault();
clearWatchdog();
if (key === "q") {
if (modifierDown) {
quitAfterQuietPeriod();
} else {
clearWatchdog();
}
}
return;
}
const modifierDown = options.platform === "darwin" ? input.meta : input.control;
if (!modifierDown || input.alt || input.shift || key !== "q") {
// Re-pressing the platform modifier is the first half of a second full
// quit shortcut, so it must not cancel an active double-press window.
@@ -129,7 +157,8 @@ export function makeQuitShortcutHandler(
if (mode === "hold" && armed && Date.now() - heldSince >= QUIT_HOLD_DURATION_MS) {
armed = false;
quitOnRelease = true;
clearWatchdog();
options.concealWindow();
quitAfterQuietPeriod();
}
return;
}