@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 195 lines 6.47 KB
Raw
1 import {
2 createDeviceStreamClient,
3 type DeviceScreenSize,
4 } from "@t3tools/client-runtime/device/stream";
5
6 import type { DeviceStreamConfiguration } from "./device-stream-document";
7
8 declare global {
9 interface Window {
10 ReactNativeWebView: { postMessage: (message: string) => void };
11 }
12 }
13
14 let activeClient: ReturnType<typeof createDeviceStreamClient> | null = null;
15
16 // The WebView has no catalogs: status details leave marked, and React Native resolves them.
17 const keepMarked = ((message: string) => message) as NonNullable<
18 Parameters<typeof createDeviceStreamClient>[3]
19 >;
20
21 export function stop() {
22 activeClient?.stop();
23 activeClient = null;
24 }
25
26 export function command(button: "home" | "back" | "appSwitcher" | "rotate") {
27 if (button === "rotate") activeClient?.rotate();
28 else activeClient?.pressButton(button);
29 }
30
31 export function start(configuration: DeviceStreamConfiguration) {
32 stop();
33 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- The native WebView bridge takes one string.
34 const post = (message: object) => window.ReactNativeWebView.postMessage(JSON.stringify(message));
35 const { colors, platform } = configuration;
36 Object.assign(document.documentElement.style, { height: "100%", overflow: "hidden" });
37 Object.assign(document.body.style, {
38 margin: "0",
39 height: "100%",
40 overflow: "hidden",
41 background: colors.background,
42 color: colors.foreground,
43 fontFamily: "system-ui",
44 });
45 const container = document.createElement("div");
46 Object.assign(container.style, {
47 position: "fixed",
48 inset: "0",
49 containerType: "size",
50 display: "flex",
51 alignItems: "center",
52 justifyContent: "center",
53 });
54 const frame = document.createElement("div");
55 frame.setAttribute("role", "application");
56 frame.setAttribute("aria-label", configuration.labels.screen);
57 frame.tabIndex = 0;
58 Object.assign(frame.style, {
59 position: "relative",
60 touchAction: "none",
61 userSelect: "none",
62 webkitUserSelect: "none",
63 webkitTouchCallout: "none",
64 outline: "none",
65 });
66 const canvas = document.createElement("canvas");
67 const image = document.createElement("img");
68 image.alt = "";
69 image.draggable = false;
70 image.style.display = "none";
71 const inputStatus = document.createElement("div");
72 inputStatus.setAttribute("role", "status");
73 inputStatus.textContent = configuration.labels.reconnectingControls;
74 Object.assign(inputStatus.style, {
75 position: "fixed",
76 bottom: "12px",
77 left: "0",
78 right: "0",
79 textAlign: "center",
80 pointerEvents: "none",
81 fontSize: "13px",
82 color: colors.muted,
83 background: colors.background,
84 });
85 frame.append(canvas, image);
86 container.append(frame);
87 document.body.replaceChildren(container, inputStatus);
88
89 let pointerId: number | null = null;
90 let inputConnected = false;
91 let streaming = false;
92 const reportStatus = (status: "connecting" | "streaming" | "error", detail?: string) => {
93 if (activeClient !== client) return;
94 streaming = status === "streaming";
95 inputStatus.style.display = streaming && !inputConnected ? "block" : "none";
96 post({ type: "status", status, detail });
97 };
98 const layout = (screen: DeviceScreenSize | null) => {
99 const landscape =
100 screen?.orientation === "landscape_left" || screen?.orientation === "landscape_right";
101 const aspect = screen
102 ? landscape
103 ? Math.max(screen.width, screen.height) / Math.min(screen.width, screen.height)
104 : Math.min(screen.width, screen.height) / Math.max(screen.width, screen.height)
105 : 9 / 19.5;
106 const rotation =
107 platform === "ios" && screen && screen.width <= screen.height
108 ? screen.orientation === "landscape_left"
109 ? 90
110 : screen.orientation === "landscape_right"
111 ? -90
112 : screen.orientation === "portrait_upside_down"
113 ? 180
114 : 0
115 : 0;
116 const sideways = Math.abs(rotation) === 90;
117 frame.style.width = `min(100cqw, ${aspect * 100}cqh)`;
118 frame.style.height = `min(100cqh, ${100 / aspect}cqw)`;
119 for (const media of [canvas, image]) {
120 Object.assign(media.style, {
121 position: "absolute",
122 width: sideways ? `${100 / aspect}%` : "100%",
123 height: sideways ? `${100 * aspect}%` : "100%",
124 left: "50%",
125 top: "50%",
126 transform: `translate(-50%, -50%) rotate(${rotation}deg)`,
127 pointerEvents: "none",
128 });
129 }
130 };
131 layout(null);
132 const unauthorized = () => {
133 if (activeClient === client) post({ type: "unauthorized" });
134 };
135 const client = createDeviceStreamClient(
136 configuration,
137 canvas,
138 {
139 onStatus: reportStatus,
140 onScreen: layout,
141 onMjpegFallback: () => {
142 canvas.style.display = "none";
143 image.style.display = "block";
144 },
145 onCanvasVideo: () => {
146 image.style.display = "none";
147 canvas.style.display = "";
148 },
149 onUnauthorized: unauthorized,
150 onInputConnected: (connected) => {
151 inputConnected = connected;
152 inputStatus.style.display = streaming && !connected ? "block" : "none";
153 post({ type: "input", connected });
154 },
155 },
156 keepMarked,
157 );
158 activeClient = client;
159 client.setMjpegImage(image);
160 const touch = (event: PointerEvent, phase: "begin" | "move" | "end") => {
161 const rect = frame.getBoundingClientRect();
162 client.sendTouch(
163 phase,
164 Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width)),
165 Math.max(0, Math.min(1, (event.clientY - rect.top) / rect.height)),
166 );
167 };
168 frame.addEventListener("pointerdown", (event) => {
169 if (!inputConnected || pointerId !== null) return;
170 event.preventDefault();
171 pointerId = event.pointerId;
172 frame.setPointerCapture(event.pointerId);
173 frame.focus();
174 touch(event, "begin");
175 });
176 frame.addEventListener("pointermove", (event) => {
177 if (pointerId === event.pointerId) touch(event, "move");
178 });
179 const endTouch = (event: PointerEvent) => {
180 if (pointerId !== event.pointerId) return;
181 pointerId = null;
182 touch(event, "end");
183 };
184 frame.addEventListener("pointerup", endTouch);
185 frame.addEventListener("pointercancel", endTouch);
186 frame.addEventListener("lostpointercapture", endTouch);
187 frame.addEventListener("keydown", (event) => {
188 event.preventDefault();
189 client.sendKey(event, "down");
190 });
191 frame.addEventListener("keyup", (event) => client.sendKey(event, "up"));
192 window.addEventListener("pagehide", stop, { once: true });
193 post({ type: "input", connected: false });
194 client.start();
195 }