Open History to see the last commit
1import { useSyncExternalStore } from "react";
2
3import { isMacPlatform } from "../lib/utils";
4
5const HOLD_HINT_LINGER_MS = 1200;
6
7type QuitHintMode = "hold" | "double-click";
8
9let visibleMode: QuitHintMode | null = null;
10let pressedMode: QuitHintMode = "hold";
11let hideTimer: number | undefined;
12const listeners = new Set<() => void>();
13let stopBridge: (() => void) | null = null;
14
15function setVisibleMode(next: QuitHintMode | null) {
16 if (next === visibleMode) return;
17 visibleMode = next;
18 for (const listener of listeners) listener();
19}
20
21function subscribeQuitHint(listener: () => void): () => void {
22 listeners.add(listener);
23 if (listeners.size === 1) {
24 stopBridge =
25 window.desktopBridge?.onQuitShortcut((hint) => {
26 window.clearTimeout(hideTimer);
27 if (hint.state === "down") {
28 pressedMode = hint.mode;
29 setVisibleMode(hint.mode);
30 return;
31 }
32 if (pressedMode === "double-click") {
33 setVisibleMode(null);
34 return;
35 }
36 hideTimer = window.setTimeout(() => setVisibleMode(null), HOLD_HINT_LINGER_MS);
37 }) ?? null;
38 }
39 return () => {
40 listeners.delete(listener);
41 if (listeners.size > 0) return;
42 window.clearTimeout(hideTimer);
43 stopBridge?.();
44 stopBridge = null;
45 visibleMode = null;
46 };
47}
48
49const readQuitHint = () => visibleMode;
50
51export function QuitHoldOverlay() {
52 const visibleMode = useSyncExternalStore(subscribeQuitHint, readQuitHint, readQuitHint);
53
54 if (!visibleMode) return null;
55 const shortcut = isMacPlatform(navigator.platform) ? "⌘Q" : "Ctrl+Q";
56 const message =
57 visibleMode === "hold"
58 ? `Hold ${shortcut} or press twice to quit`
59 : `Press ${shortcut} again to quit`;
60 return (
61 <div
62 role="status"
63 className="pointer-events-none fixed inset-x-0 top-[22%] z-100 flex justify-center"
64 >
65 <div className="rounded-full bg-foreground/95 px-8 py-4 text-2xl font-bold text-background shadow-xl">
66 {message}
67 </div>
68 </div>
69 );
70}