@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 93 lines 2.94 KB
Raw
1 import * as NodeWorkerThreads from "node:worker_threads";
2
3 import type { WindowsForegroundFocusTarget } from "./WindowsForegroundFocusThread.ts";
4 import type { Element } from "@crowecawcaw/xa11y";
5
6 type FocusRequest = {
7 readonly type: "prepare" | "focus";
8 readonly requestId: number;
9 readonly target: WindowsForegroundFocusTarget;
10 };
11
12 function matchesTarget(
13 element: {
14 readonly name?: string | null;
15 readonly bounds: WindowsForegroundFocusTarget["bounds"] | null;
16 },
17 target: WindowsForegroundFocusTarget,
18 ): boolean {
19 if ((element.name ?? "").trim() !== target.title.trim()) return false;
20 if (!element.bounds) return false;
21 return [target.bounds, target.contentBounds].some((bounds) =>
22 (["x", "y", "width", "height"] as const).every(
23 (key) => Math.abs(element.bounds![key] - bounds[key]) <= 2,
24 ),
25 );
26 }
27
28 async function findTarget(
29 App: (typeof import("@crowecawcaw/xa11y"))["App"],
30 target: WindowsForegroundFocusTarget,
31 ): Promise<Element | undefined> {
32 const children = await App.byPid(target.processId, { timeout: 0 })
33 .then((app) => app.children())
34 .catch(() => []);
35 return children.find((candidate) => matchesTarget(candidate, target));
36 }
37
38 const cachedElements = new Map<number, Element>();
39
40 async function prepareTarget(
41 App: (typeof import("@crowecawcaw/xa11y"))["App"],
42 target: WindowsForegroundFocusTarget,
43 ): Promise<boolean> {
44 const element = await findTarget(App, target);
45 if (!element) {
46 cachedElements.delete(target.windowId);
47 return false;
48 }
49 cachedElements.set(target.windowId, element);
50 return true;
51 }
52
53 async function focusTarget(
54 App: (typeof import("@crowecawcaw/xa11y"))["App"],
55 target: WindowsForegroundFocusTarget,
56 ): Promise<boolean> {
57 let element = cachedElements.get(target.windowId);
58 if (!element) {
59 element = await findTarget(App, target);
60 if (!element) return false;
61 cachedElements.set(target.windowId, element);
62 }
63 try {
64 await element.focus();
65 return true;
66 } catch {
67 cachedElements.delete(target.windowId);
68 return false;
69 }
70 }
71
72 async function start() {
73 const { App } = await import("@crowecawcaw/xa11y");
74 const parentPort = NodeWorkerThreads.parentPort;
75 if (!parentPort) return;
76 let work = Promise.resolve();
77 parentPort.on("message", (message: FocusRequest) => {
78 if (message.type !== "prepare" && message.type !== "focus") return;
79 work = work.then(async () => {
80 const focused = await (
81 message.type === "prepare"
82 ? prepareTarget(App, message.target)
83 : focusTarget(App, message.target)
84 ).catch(() => false);
85 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
86 parentPort.postMessage({ type: "result", requestId: message.requestId, focused });
87 });
88 });
89 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
90 parentPort.postMessage("ready");
91 }
92
93 void start();