Open History to see the last commit
1// @effect-diagnostics globalDate:off globalTimers:off -- Synchronous before-input-event handler; key events must be timed and the watchdog scheduled outside any Effect runtime.
2
3// Chrome-style hold-to-quit. The quit accelerator is intercepted in
4// before-input-event (which runs before the native menu accelerator), and the
5// app only quits once the shortcut has been held for QUIT_HOLD_DURATION_MS.
6// A quick tap just shows the renderer's "Hold to Quit" hint, and a second tap
7// within QUIT_DOUBLE_TAP_MS quits immediately. Quitting from the application
8// menu itself is untouched and quits immediately.
9export const QUIT_HOLD_DURATION_MS = 1200;
10// A second quick tap of the shortcut is the user insisting: quit immediately.
11export const QUIT_DOUBLE_TAP_MS = 500;
12// "Still held" is proven by auto-repeat keydowns, not by the absence of a
13// release: macOS suppresses a letter's keyUp while the command key is down, so
14// a tap's release can go completely unseen and a release-based timer would
15// quit anyway. The press is treated as released once no key event has arrived
16// for QUIT_HOLD_RELEASE_GRACE_MS past the hold duration. Keyboards with
17// auto-repeat disabled cannot hold-to-quit and fall back to the menu's Quit.
18export const QUIT_HOLD_RELEASE_GRACE_MS = 600;
19
20export type QuitHoldState = "down" | "up";
21
22export interface QuitHoldKeyInput {
23 readonly type: string;
24 readonly key: string;
25 readonly meta: boolean;
26 readonly control: boolean;
27 readonly alt: boolean;
28 readonly shift: boolean;
29 readonly isAutoRepeat: boolean;
30}
31
32export interface QuitHoldOptions {
33 readonly platform: NodeJS.Platform;
34 readonly isEnabled: () => Promise<boolean>;
35 readonly notify: (state: QuitHoldState) => void;
36 readonly quit: () => void;
37}
38
39export function makeQuitHoldHandler(
40 options: QuitHoldOptions,
41): (event: { preventDefault: () => void }, input: QuitHoldKeyInput) => void {
42 const modifierKey = options.platform === "darwin" ? "meta" : "control";
43 let watchdog: NodeJS.Timeout | undefined;
44 let holding = false;
45 // Set once isEnabled resolves true; auto-repeats may only quit when armed.
46 let armed = false;
47 let heldSince = 0;
48 let lastPressAt = 0;
49 // Incremented on every new press and every release/quit so a pending
50 // isEnabled() resolution from a superseded press cannot arm (or quit for)
51 // the current one.
52 let generation = 0;
53
54 const clearWatchdog = () => {
55 if (watchdog !== undefined) {
56 clearTimeout(watchdog);
57 watchdog = undefined;
58 }
59 };
60
61 const release = () => {
62 if (!holding) return;
63 generation += 1;
64 holding = false;
65 armed = false;
66 clearWatchdog();
67 options.notify("up");
68 };
69
70 // Dismisses the overlay first: if the quit is cancelled downstream the
71 // renderer must not be left with a stuck "Hold to Quit" hint.
72 const quitNow = () => {
73 release();
74 options.quit();
75 };
76
77 return (event, input) => {
78 const key = input.key.toLowerCase();
79 if (input.type === "keyUp") {
80 if (key === "q" || key === modifierKey) release();
81 return;
82 }
83 if (input.type !== "keyDown") return;
84
85 const modifierDown = options.platform === "darwin" ? input.meta : input.control;
86 if (!modifierDown || input.alt || input.shift || key !== "q") {
87 // Any other key (or an extra modifier) pressed mid-hold breaks the
88 // gesture; without this the hold timer keeps running through the
89 // interruption and the next qualifying repeat would quit early. The
90 // interrupted press also stops counting toward a double tap — but only
91 // here, not in release(), which runs mid-restart on an unseen-release
92 // re-press and must not wipe that press's own tap timestamp.
93 if (holding && !input.isAutoRepeat) {
94 lastPressAt = 0;
95 release();
96 }
97 return;
98 }
99
100 event.preventDefault();
101
102 if (input.isAutoRepeat) {
103 if (armed && Date.now() - heldSince >= QUIT_HOLD_DURATION_MS) {
104 quitNow();
105 }
106 return;
107 }
108
109 const now = Date.now();
110 const previousPressAt = lastPressAt;
111 lastPressAt = now;
112 // A fresh keydown while "holding" means the key came back down after a
113 // release macOS never delivered — so both branches below see real taps.
114 if (previousPressAt !== 0 && now - previousPressAt <= QUIT_DOUBLE_TAP_MS) {
115 quitNow();
116 return;
117 }
118 if (holding) release();
119
120 generation += 1;
121 const pressGeneration = generation;
122 holding = true;
123 heldSince = now;
124 options.notify("down");
125 void options.isEnabled().then(
126 (enabled) => {
127 if (generation !== pressGeneration) return;
128 if (!enabled) {
129 // Hold-to-quit disabled: a single press quits immediately.
130 quitNow();
131 return;
132 }
133 armed = true;
134 // No auto-repeat by then means the key was released (possibly with a
135 // suppressed keyUp) or repeat is disabled; either way, don't quit.
136 watchdog = setTimeout(() => {
137 watchdog = undefined;
138 release();
139 }, QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS);
140 },
141 // A failed settings read must never strand the quit request.
142 () => {
143 if (generation !== pressGeneration) return;
144 quitNow();
145 },
146 );
147 };
148}