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

main
ts 82 lines 3.07 KB
Raw
1 interface Point {
2 readonly x: number;
3 readonly y: number;
4 }
5
6 export type PhoneNavigation =
7 | { readonly type: "orbit"; readonly x: number; readonly y: number }
8 | { readonly type: "zoom"; readonly delta: number };
9
10 /** Browser wheel units vary by device. */
11 export function phoneWheelNavigation(input: {
12 readonly deltaX: number;
13 readonly deltaY: number;
14 readonly deltaMode: number;
15 readonly ctrlKey: boolean;
16 readonly width: number;
17 readonly height: number;
18 }): PhoneNavigation | null {
19 const { deltaX, deltaY, deltaMode, ctrlKey, width, height } = input;
20 if (![deltaX, deltaY, width, height].every(Number.isFinite) || width <= 0 || height <= 0)
21 return null;
22 if (deltaMode !== 0 && deltaMode !== 1 && deltaMode !== 2) return null;
23 const unit = deltaMode === 1 ? 16 : deltaMode === 2 ? height : 1;
24 const clamp = (value: number, limit: number) => Math.min(limit, Math.max(-limit, value));
25 if (ctrlKey) return { type: "zoom", delta: clamp(-deltaY * unit * 0.01, 1) };
26 return {
27 type: "orbit",
28 x: clamp((-deltaX * unit) / width, 0.25),
29 y: clamp((-deltaY * unit) / height, 0.25),
30 };
31 }
32
33 export function createPhoneInteraction(options: {
34 readonly screenPoint: (point: Point, captured: boolean) => Point | null;
35 readonly touch: (phase: "begin" | "move" | "end", point: Point) => void;
36 readonly orbit: (deltaX: number, deltaY: number) => void;
37 readonly zoomBy: (logDelta: number) => void;
38 readonly onInteractionActive?: (active: boolean, mode: "touch" | "orbit") => void;
39 }) {
40 let active: { id: number; mode: "touch" | "orbit"; last: Point; screen: Point | null } | null =
41 null;
42 return {
43 navigate(gesture: PhoneNavigation) {
44 // Moving the camera during a captured device touch would change its projected coordinates.
45 if (active) return false;
46 if (gesture.type === "zoom") options.zoomBy(gesture.delta);
47 else options.orbit(gesture.x, gesture.y);
48 return true;
49 },
50 endWheel() {
51 if (!active) options.onInteractionActive?.(false, "orbit");
52 },
53 begin(id: number, point: Point, forceOrbit = false) {
54 if (active) return false;
55 const screen = forceOrbit ? null : options.screenPoint(point, false);
56 active = { id, mode: screen ? "touch" : "orbit", last: point, screen };
57 options.onInteractionActive?.(true, active.mode);
58 if (screen) options.touch("begin", screen);
59 return true;
60 },
61 move(id: number, point: Point) {
62 if (active?.id !== id) return;
63 if (active.mode === "touch") {
64 const screen = options.screenPoint(point, true);
65 if (screen) {
66 active.screen = screen;
67 options.touch("move", screen);
68 }
69 } else {
70 options.orbit(point.x - active.last.x, point.y - active.last.y);
71 }
72 active.last = point;
73 },
74 end(id?: number) {
75 if (!active || (id !== undefined && active.id !== id)) return;
76 const previous = active;
77 active = null;
78 if (previous.mode === "touch" && previous.screen) options.touch("end", previous.screen);
79 options.onInteractionActive?.(false, previous.mode);
80 },
81 };
82 }