Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

QuitHold.ts

170 lines5.8 KB

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