@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 206 lines 7.95 KB
Raw
1 import type { VoiceInputState } from "@t3tools/client-runtime/voice-input";
2 import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3 import { useEffect, useRef, type RefObject } from "react";
4
5 import { isCommandPaletteOpen } from "../commandPaletteBus";
6 import { useClientSettings } from "../hooks/useSettings";
7 import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
8 import { getTerminalFocusOwner } from "../lib/terminalFocus";
9
10 const HOLD_THRESHOLD_MS = 300;
11
12 function shouldFinishDictationOnRelease(mode: "auto" | "hold" | "toggle", elapsedMs: number) {
13 return mode === "hold" || (mode === "auto" && elapsedMs >= HOLD_THRESHOLD_MS);
14 }
15
16 type SpeechActions = {
17 available: boolean;
18 state: VoiceInputState;
19 start(): Promise<void>;
20 stop(): Promise<void>;
21 cancel(): void;
22 };
23
24 export type DictationKeyHandlers = {
25 keydown(event: KeyboardEvent): void;
26 keyup(event: KeyboardEvent): void;
27 blur(): void;
28 };
29
30 type ShortcutInput = {
31 keybindings: ResolvedKeybindingsConfig;
32 speech: SpeechActions;
33 disabled: boolean;
34 terminalOpen: boolean;
35 modelPickerOpen: boolean;
36 /** Resolve the chord as if the preview had focus; the keys arrive relayed from its guest. */
37 previewFocus?: boolean;
38 /** When set, the chord targets this editor only while it has focus. */
39 targetRef?: RefObject<HTMLElement | null>;
40 /** An editor outside this document reports whether it holds the keyboard. */
41 ownsFocus?: () => boolean;
42 /** Receives handlers that replay keys an outside editor relayed, as if typed here. */
43 subscribeKeys?: (handlers: DictationKeyHandlers) => () => void;
44 };
45
46 // Several editors can bind the chord; the busy one, then the focused one, then the untargeted one wins.
47 const shortcutTargets = new Map<symbol, () => ShortcutInput>();
48
49 function ownsShortcut(id: symbol) {
50 const entries = [...shortcutTargets];
51 const busy = entries.find(([, read]) => {
52 const phase = read().speech.state.phase;
53 return phase !== "idle" && phase !== "error";
54 });
55 if (busy) return busy[0] === id;
56 const focused =
57 entries.find(([, read]) => read().ownsFocus?.()) ??
58 entries.find(([, read]) => read().targetRef?.current?.contains(document.activeElement));
59 if (focused) return focused[0] === id;
60 return entries.find(([, read]) => !read().targetRef && !read().ownsFocus)?.[0] === id;
61 }
62
63 const targetScope = (field: HTMLElement | null | undefined) =>
64 field?.closest('[role="dialog"]') ?? field;
65
66 // Keys an outside editor (the preview guest) relays arrive on window, so remember their editor.
67 const relayedFrom = new WeakMap<Event, symbol>();
68
69 // Escape inside another shown dictation editor, or the dialog hosting it, belongs to that editor.
70 // The editor itself is checked before dialogs, since one dialog can host several editors.
71 function inOtherShortcutTarget(id: symbol, event: Event) {
72 const source = relayedFrom.get(event);
73 if (source) return source !== id;
74 const target = event.target;
75 if (!(target instanceof Node)) return false;
76 const own = shortcutTargets.get(id)?.().targetRef?.current;
77 const others = [...shortcutTargets]
78 .filter(([other]) => other !== id)
79 .map(([, read]) => read().targetRef?.current)
80 .filter((field): field is HTMLElement => !!field?.getClientRects().length);
81 for (const scope of [(field?: HTMLElement | null) => field, targetScope]) {
82 if (scope(own)?.contains(target)) return false;
83 if (others.some((field) => scope(field)?.contains(target))) return true;
84 }
85 return false;
86 }
87
88 export function useDictationShortcut(input: ShortcutInput) {
89 const id = useRef(Symbol("dictation-target"));
90 const subscribeKeys = input.subscribeKeys;
91 const mode = useClientSettings((settings) => settings.voiceShortcutMode);
92 const current = useRef(input);
93 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes committed actions to the window keyboard listener and shortcut registry
94 useEffect(() => {
95 current.current = input;
96 });
97 const generation = useRef(0);
98 const pressed = useRef<{
99 generation: number;
100 code: string;
101 modifiers: string[];
102 startedAt: number;
103 mode: typeof mode;
104 starting: Promise<void>;
105 } | null>(null);
106
107 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the window keyboard listeners and dictation target registry entry
108 useEffect(() => {
109 if (typeof window === "undefined") return;
110 let disposed = false;
111 shortcutTargets.set(id.current, () => current.current);
112 const finish = (event: KeyboardEvent | null) => {
113 const press = pressed.current;
114 if (!press) return;
115 // macOS can omit the letter keyup while Command is held; releasing a chord modifier ends it too.
116 if (event && (event.code || event.key) !== press.code && !press.modifiers.includes(event.key))
117 return;
118 pressed.current = null;
119 if (shouldFinishDictationOnRelease(press.mode, performance.now() - press.startedAt)) {
120 void press.starting.then(() => {
121 if (!disposed && generation.current === press.generation)
122 return current.current.speech.stop();
123 });
124 }
125 };
126
127 const onKeyDown = (event: KeyboardEvent) => {
128 if (!ownsShortcut(id.current)) return;
129 if (event.isComposing || event.keyCode === 229) return;
130 const { speech } = current.current;
131 if (event.defaultPrevented || isCommandPaletteOpen()) return;
132 if (
133 event.key === "Escape" &&
134 (pressed.current !== null ||
135 (speech.state.phase !== "idle" && speech.state.phase !== "error")) &&
136 !inOtherShortcutTarget(id.current, event)
137 ) {
138 event.preventDefault();
139 event.stopImmediatePropagation();
140 pressed.current = null;
141 generation.current += 1;
142 speech.cancel();
143 return;
144 }
145 const { keybindings, terminalOpen, modelPickerOpen, disabled } = current.current;
146 const previewFocus = current.current.previewFocus ?? false;
147 const command = resolveShortcutCommand(event, keybindings, {
148 context: {
149 terminalFocus: getTerminalFocusOwner() !== null,
150 terminalOpen,
151 modelPickerOpen,
152 previewFocus,
153 previewOpen: previewFocus,
154 ...(previewFocus ? { editableFocus: true } : {}),
155 },
156 });
157 if (command !== "composer.dictation") return;
158 const canStart = speech.state.phase === "idle" || speech.state.phase === "error";
159 if (canStart && (!speech.available || disabled)) return;
160 event.preventDefault();
161 event.stopPropagation();
162 if (event.repeat || (!canStart && speech.state.phase !== "recording")) return;
163 // macOS may omit the previous non-modifier keyup while Command is held.
164 pressed.current = null;
165 generation.current += 1;
166 if (speech.state.phase === "recording") {
167 void speech.stop();
168 return;
169 }
170 pressed.current = {
171 generation: generation.current,
172 code: event.code || event.key,
173 modifiers: ["Alt", "Control", "Meta", "Shift"].filter((key) => event.getModifierState(key)),
174 startedAt: performance.now(),
175 mode,
176 starting: speech.start(),
177 };
178 };
179 const onBlur = () => finish(null);
180 const relay = (event: KeyboardEvent) => {
181 relayedFrom.set(event, id.current);
182 window.dispatchEvent(event);
183 };
184 const unsubscribe = subscribeKeys?.({
185 keydown: relay,
186 keyup: relay,
187 blur: onBlur,
188 });
189 window.addEventListener("keydown", onKeyDown, true);
190 window.addEventListener("keyup", finish, true);
191 window.addEventListener("blur", onBlur);
192 return () => {
193 disposed = true;
194 pressed.current = null;
195 shortcutTargets.delete(id.current);
196 unsubscribe?.();
197 window.removeEventListener("keydown", onKeyDown, true);
198 window.removeEventListener("keyup", finish, true);
199 window.removeEventListener("blur", onBlur);
200 };
201 }, [mode, subscribeKeys]);
202
203 return shortcutLabelForCommand(input.keybindings, "composer.dictation", {
204 context: { terminalFocus: false, terminalOpen: input.terminalOpen },
205 });
206 }