Skip to content

Commit b7dbbba

Browse files
authored
feat(desktop): Chrome-style hold-to-quit (pingdotgg#5508)
1 parent 178da6b commit b7dbbba

16 files changed

Lines changed: 539 additions & 3 deletions

File tree

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
55
export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
66
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
77
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
8+
export const QUIT_SHORTCUT_CHANNEL = "desktop:quit-shortcut";
89
export const GET_WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:get-window-fullscreen-state";
910
export const WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:window-fullscreen-state";
1011
export const UPDATE_STATE_CHANNEL = "desktop:update-state";

‎apps/desktop/src/ipc/methods/wsl.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
1010
import * as DesktopShutdown from "../../app/DesktopShutdown.ts";
1111
import * as DesktopState from "../../app/DesktopState.ts";
1212
import * as ElectronApp from "../../electron/ElectronApp.ts";
13+
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
1314
import * as ElectronTheme from "../../electron/ElectronTheme.ts";
15+
import * as ElectronWindow from "../../electron/ElectronWindow.ts";
1416
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
17+
import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
1518
import * as DesktopWindow from "../../window/DesktopWindow.ts";
1619
import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
1720
import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
@@ -70,6 +73,15 @@ const unusedLifecycleRuntimeLayer = Layer.mergeAll(
7073
ElectronTheme.ElectronTheme,
7174
ElectronTheme.ElectronTheme.of({} as ElectronTheme.ElectronTheme["Service"]),
7275
),
76+
Layer.succeed(
77+
ElectronDialog.ElectronDialog,
78+
ElectronDialog.ElectronDialog.of({} as ElectronDialog.ElectronDialog["Service"]),
79+
),
80+
Layer.succeed(
81+
ElectronWindow.ElectronWindow,
82+
ElectronWindow.ElectronWindow.of({} as ElectronWindow.ElectronWindow["Service"]),
83+
),
84+
DesktopClientSettings.layerTest(),
7385
);
7486

7587
describe("WSL IPC", () => {

‎apps/desktop/src/preload.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ contextBridge.exposeInMainWorld("desktopBridge", {
117117
ipcRenderer.removeListener(IpcChannels.MENU_ACTION_CHANNEL, wrappedListener);
118118
};
119119
},
120+
onQuitShortcut: (listener) => {
121+
const wrappedListener = (_event: Electron.IpcRendererEvent, state: unknown) => {
122+
if (state !== "down" && state !== "up") return;
123+
listener(state);
124+
};
125+
126+
ipcRenderer.on(IpcChannels.QUIT_SHORTCUT_CHANNEL, wrappedListener);
127+
return () => {
128+
ipcRenderer.removeListener(IpcChannels.QUIT_SHORTCUT_CHANNEL, wrappedListener);
129+
};
130+
},
120131
getWindowFullscreenState: () =>
121132
ipcRenderer.sendSync(IpcChannels.GET_WINDOW_FULLSCREEN_STATE_CHANNEL) === true,
122133
onWindowFullscreenStateChange: (listener) => {

‎apps/desktop/src/settings/DesktopClientSettings.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
1313
import * as DesktopClientSettings from "./DesktopClientSettings.ts";
1414

1515
const clientSettings: ClientSettings = {
16+
confirmQuit: true,
1617
confirmThreadArchive: true,
1718
confirmThreadDelete: false,
1819
dismissedProviderUpdateNotificationKeys: [],

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import * as DesktopConfig from "../app/DesktopConfig.ts";
3737
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
3838
import * as DesktopState from "../app/DesktopState.ts";
3939
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
40+
import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts";
41+
import * as ElectronApp from "../electron/ElectronApp.ts";
4042
import * as ElectronMenu from "../electron/ElectronMenu.ts";
4143
import * as ElectronShell from "../electron/ElectronShell.ts";
4244
import * as ElectronTheme from "../electron/ElectronTheme.ts";
@@ -128,6 +130,14 @@ function makeFakeBrowserWindow() {
128130
};
129131
}
130132

133+
const desktopClientSettingsLayer = Layer.mock(DesktopClientSettings.DesktopClientSettings)({
134+
get: Effect.succeed(Option.none()),
135+
});
136+
137+
const electronAppLayer = Layer.mock(ElectronApp.ElectronApp)({
138+
quit: Effect.void,
139+
});
140+
131141
const desktopAssetsLayer = Layer.succeed(DesktopAssets.DesktopAssets, {
132142
iconPaths: Effect.succeed({
133143
ico: Option.none<string>(),
@@ -253,8 +263,10 @@ function makeTestLayer(input: {
253263
desktopAssetsLayer,
254264
desktopEnvironmentLayer,
255265
desktopAppSettingsLayer,
266+
desktopClientSettingsLayer,
256267
desktopServerExposureLayer,
257268
DesktopState.layer,
269+
electronAppLayer,
258270
electronMenuLayer,
259271
Layer.succeed(ElectronShell.ElectronShell, {
260272
openExternal: (url) =>
@@ -356,7 +368,9 @@ const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | n
356368
desktopAssetsLayer,
357369
desktopEnvironmentLayer,
358370
DesktopAppSettings.layerTest(),
371+
desktopClientSettingsLayer,
359372
desktopServerExposureLayer,
373+
electronAppLayer,
360374
electronMenuLayer,
361375
Layer.succeed(ElectronShell.ElectronShell, {
362376
openExternal: () => Effect.succeed(true),

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as Ref from "effect/Ref";
88

99
import * as Electron from "electron";
1010

11+
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts";
12+
1113
import * as DesktopAssets from "../app/DesktopAssets.ts";
1214
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
1315
import { makeComponentLogger } from "../app/DesktopObservability.ts";
@@ -16,9 +18,16 @@ import { getDesktopUrl } from "../electron/ElectronProtocol.ts";
1618
import * as ElectronShell from "../electron/ElectronShell.ts";
1719
import * as ElectronTheme from "../electron/ElectronTheme.ts";
1820
import * as ElectronWindow from "../electron/ElectronWindow.ts";
19-
import { MENU_ACTION_CHANNEL, WINDOW_FULLSCREEN_STATE_CHANNEL } from "../ipc/channels.ts";
21+
import {
22+
MENU_ACTION_CHANNEL,
23+
QUIT_SHORTCUT_CHANNEL,
24+
WINDOW_FULLSCREEN_STATE_CHANNEL,
25+
} from "../ipc/channels.ts";
2026
import * as PreviewManager from "../preview/Manager.ts";
2127
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
28+
import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts";
29+
import * as ElectronApp from "../electron/ElectronApp.ts";
30+
import { makeQuitHoldHandler } from "./QuitHold.ts";
2231

2332
const TITLEBAR_HEIGHT = 40;
2433
const TITLEBAR_COLOR = "#01000000"; // #00000000 does not work correctly on Linux
@@ -51,6 +60,8 @@ type DesktopWindowRuntimeServices =
5160
| DesktopEnvironment.DesktopEnvironment
5261
| DesktopAssets.DesktopAssets
5362
| DesktopAppSettings.DesktopAppSettings
63+
| DesktopClientSettings.DesktopClientSettings
64+
| ElectronApp.ElectronApp
5465
| ElectronMenu.ElectronMenu
5566
| ElectronShell.ElectronShell
5667
| ElectronTheme.ElectronTheme
@@ -261,6 +272,8 @@ export const make = Effect.gen(function* () {
261272
const electronWindow = yield* ElectronWindow.ElectronWindow;
262273
const previewManager = yield* PreviewManager.PreviewManager;
263274
const desktopSettings = yield* DesktopAppSettings.DesktopAppSettings;
275+
const clientSettings = yield* DesktopClientSettings.DesktopClientSettings;
276+
const electronApp = yield* ElectronApp.ElectronApp;
264277
// Window-side latch for the primary backend's readiness. Set by
265278
// handleBackendReady (driven by the pool's onReady callback), cleared
266279
// by handleBackendNotReady (driven by onShutdown). Only consumed by
@@ -533,7 +546,32 @@ export const make = Effect.gen(function* () {
533546
// close-terminal shortcut can outlive the terminal that handled its first
534547
// press, so reject repeats before they reach the native window accelerator.
535548
// Deliberate presses still flow through the renderer or native menu.
549+
// Chrome-style hold-to-quit: intercept the quit accelerator before the
550+
// native menu sees it and only quit after the shortcut is held. The
551+
// renderer shows the "Hold to Quit" hint via QUIT_SHORTCUT_CHANNEL.
552+
const quitHoldHandler = makeQuitHoldHandler({
553+
platform: environment.platform,
554+
isEnabled: () =>
555+
runPromise(
556+
Effect.map(
557+
clientSettings.get,
558+
Option.match({
559+
onNone: () => DEFAULT_CLIENT_SETTINGS.confirmQuit,
560+
onSome: (settings) => settings.confirmQuit,
561+
}),
562+
),
563+
),
564+
notify: (state) => {
565+
if (!window.isDestroyed()) {
566+
window.webContents.send(QUIT_SHORTCUT_CHANNEL, state);
567+
}
568+
},
569+
quit: () => {
570+
void runPromise(electronApp.quit);
571+
},
572+
});
536573
window.webContents.on("before-input-event", (event, input) => {
574+
quitHoldHandler(event, input);
537575
if (input.type !== "keyDown" || !input.isAutoRepeat) return;
538576
const modifier = environment.platform === "darwin" ? input.meta : input.control;
539577
if (modifier && !input.alt && !input.shift && input.key.toLowerCase() === "w") {
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
import {
4+
makeQuitHoldHandler,
5+
QUIT_DOUBLE_TAP_MS,
6+
QUIT_HOLD_DURATION_MS,
7+
QUIT_HOLD_RELEASE_GRACE_MS,
8+
} from "./QuitHold.ts";
9+
import type { QuitHoldKeyInput, QuitHoldState } from "./QuitHold.ts";
10+
11+
function makeInput(overrides: Partial<QuitHoldKeyInput>): QuitHoldKeyInput {
12+
return {
13+
type: "keyDown",
14+
key: "q",
15+
meta: true,
16+
control: false,
17+
alt: false,
18+
shift: false,
19+
isAutoRepeat: false,
20+
...overrides,
21+
};
22+
}
23+
24+
function makeHarness(options?: {
25+
enabled?: boolean;
26+
platform?: NodeJS.Platform;
27+
isEnabled?: () => Promise<boolean>;
28+
}) {
29+
const notifications: Array<QuitHoldState> = [];
30+
const quit = vi.fn();
31+
const handler = makeQuitHoldHandler({
32+
platform: options?.platform ?? "darwin",
33+
isEnabled: options?.isEnabled ?? (() => Promise.resolve(options?.enabled ?? true)),
34+
notify: (state) => notifications.push(state),
35+
quit,
36+
});
37+
const preventDefault = vi.fn();
38+
const send = async (input: QuitHoldKeyInput) => {
39+
handler({ preventDefault }, input);
40+
// Let the isEnabled promise settle.
41+
await Promise.resolve();
42+
await Promise.resolve();
43+
};
44+
// Simulates the OS auto-repeating the held shortcut every `intervalMs`.
45+
const holdFor = async (
46+
durationMs: number,
47+
repeatOverrides: Partial<QuitHoldKeyInput> = {},
48+
intervalMs = 100,
49+
) => {
50+
for (let elapsed = 0; elapsed < durationMs; elapsed += intervalMs) {
51+
vi.advanceTimersByTime(intervalMs);
52+
await send(makeInput({ isAutoRepeat: true, ...repeatOverrides }));
53+
}
54+
};
55+
return { notifications, quit, preventDefault, send, holdFor };
56+
}
57+
58+
describe("makeQuitHoldHandler", () => {
59+
beforeEach(() => {
60+
vi.useFakeTimers();
61+
});
62+
afterEach(() => {
63+
vi.useRealTimers();
64+
});
65+
66+
it("shows the hint on a tap without quitting, even when the release is never seen", async () => {
67+
// macOS suppresses the letter's keyUp while Cmd is held, so a tap may
68+
// produce no keyUp at all. Quit must still not fire.
69+
const harness = makeHarness();
70+
await harness.send(makeInput({}));
71+
expect(harness.preventDefault).toHaveBeenCalledTimes(1);
72+
expect(harness.notifications).toEqual(["down"]);
73+
74+
vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS);
75+
expect(harness.quit).not.toHaveBeenCalled();
76+
// The watchdog dismisses the hint once the press is clearly over.
77+
expect(harness.notifications).toEqual(["down", "up"]);
78+
});
79+
80+
it("quits once the shortcut auto-repeats past the hold duration", async () => {
81+
const harness = makeHarness();
82+
await harness.send(makeInput({}));
83+
await harness.holdFor(QUIT_HOLD_DURATION_MS - 200);
84+
expect(harness.quit).not.toHaveBeenCalled();
85+
await harness.holdFor(400);
86+
expect(harness.quit).toHaveBeenCalledTimes(1);
87+
// Exactly one hint cycle for the whole hold.
88+
expect(harness.notifications).toEqual(["down", "up"]);
89+
});
90+
91+
it("does not quit when the hold stops before the duration", async () => {
92+
const harness = makeHarness();
93+
await harness.send(makeInput({}));
94+
await harness.holdFor(500);
95+
await harness.send(makeInput({ type: "keyUp" }));
96+
expect(harness.notifications).toEqual(["down", "up"]);
97+
vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
98+
expect(harness.quit).not.toHaveBeenCalled();
99+
});
100+
101+
it("cancels the hold when the modifier is released first", async () => {
102+
const harness = makeHarness();
103+
await harness.send(makeInput({}));
104+
await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
105+
expect(harness.notifications).toEqual(["down", "up"]);
106+
vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
107+
expect(harness.quit).not.toHaveBeenCalled();
108+
});
109+
110+
it("quits immediately on a single press when disabled", async () => {
111+
const harness = makeHarness({ enabled: false });
112+
await harness.send(makeInput({}));
113+
expect(harness.quit).toHaveBeenCalledTimes(1);
114+
// The hint is dismissed in case the quit gets cancelled downstream.
115+
expect(harness.notifications).toEqual(["down", "up"]);
116+
});
117+
118+
it("discards a stale isEnabled resolution from a superseded press", async () => {
119+
// Press #1's isEnabled is still pending when the user releases and
120+
// presses again; its late resolution must not act for press #2.
121+
const resolvers: Array<(enabled: boolean) => void> = [];
122+
const harness = makeHarness({
123+
isEnabled: () => new Promise((resolve) => resolvers.push(resolve)),
124+
});
125+
await harness.send(makeInput({}));
126+
await harness.send(makeInput({ type: "keyUp" }));
127+
// Outside the double-tap window, so the second press starts a new hold.
128+
vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS + 100);
129+
await harness.send(makeInput({}));
130+
expect(resolvers).toHaveLength(2);
131+
132+
// Press #1 resolves late with "disabled" — it must not quit press #2.
133+
resolvers[0]?.(false);
134+
await Promise.resolve();
135+
await Promise.resolve();
136+
expect(harness.quit).not.toHaveBeenCalled();
137+
138+
// Press #2 resolves enabled and completes a full hold.
139+
resolvers[1]?.(true);
140+
await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
141+
expect(harness.quit).toHaveBeenCalledTimes(1);
142+
});
143+
144+
it("quits on a quick double tap, even when the first release was never seen", async () => {
145+
const harness = makeHarness();
146+
await harness.send(makeInput({}));
147+
vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS - 100);
148+
await harness.send(makeInput({}));
149+
expect(harness.quit).toHaveBeenCalledTimes(1);
150+
});
151+
152+
it("treats two slow taps as separate presses", async () => {
153+
const harness = makeHarness();
154+
await harness.send(makeInput({}));
155+
await harness.send(makeInput({ type: "keyUp" }));
156+
vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS + 100);
157+
await harness.send(makeInput({}));
158+
expect(harness.quit).not.toHaveBeenCalled();
159+
expect(harness.notifications).toEqual(["down", "up", "down"]);
160+
});
161+
162+
it("cancels the hold when another key interrupts it", async () => {
163+
const harness = makeHarness();
164+
await harness.send(makeInput({}));
165+
await harness.holdFor(500);
166+
// Shift pressed mid-hold breaks the gesture...
167+
await harness.send(makeInput({ shift: true }));
168+
expect(harness.notifications).toEqual(["down", "up"]);
169+
// ...so later repeats past the threshold must not quit.
170+
await harness.holdFor(QUIT_HOLD_DURATION_MS);
171+
expect(harness.quit).not.toHaveBeenCalled();
172+
});
173+
174+
it("does not count an interrupted press toward a double tap", async () => {
175+
const harness = makeHarness();
176+
await harness.send(makeInput({}));
177+
await harness.send(makeInput({ shift: true }));
178+
// A fresh press right after the interruption starts a new hold, not a
179+
// double-tap quit.
180+
await harness.send(makeInput({}));
181+
expect(harness.quit).not.toHaveBeenCalled();
182+
expect(harness.notifications).toEqual(["down", "up", "down"]);
183+
});
184+
185+
it("ignores other shortcuts", async () => {
186+
const harness = makeHarness();
187+
await harness.send(makeInput({ key: "w" }));
188+
await harness.send(makeInput({ shift: true }));
189+
await harness.send(makeInput({ meta: false }));
190+
expect(harness.preventDefault).not.toHaveBeenCalled();
191+
expect(harness.notifications).toEqual([]);
192+
});
193+
194+
it("uses control on non-mac platforms", async () => {
195+
const harness = makeHarness({ platform: "linux" });
196+
await harness.send(makeInput({ meta: false, control: true }));
197+
expect(harness.preventDefault).toHaveBeenCalledTimes(1);
198+
await harness.holdFor(QUIT_HOLD_DURATION_MS + 200, { meta: false, control: true });
199+
expect(harness.quit).toHaveBeenCalledTimes(1);
200+
});
201+
});

0 commit comments

Comments
 (0)