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
| 1 | import { useGT } from "gt-react"; |
| 2 | import { useEffect, useRef } from "react"; |
| 3 | import type { DuoViewer } from "@t3tools/client-runtime/device/duo-viewer"; |
| 4 | import type { DeviceModelSource } from "@t3tools/client-runtime/device/model"; |
| 5 | import type { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction"; |
| 6 | import { createCanvasFrameSink } from "@t3tools/client-runtime/device/frame"; |
| 7 | import type { DeviceScreenSize, DeviceStreamClient } from "@t3tools/client-runtime/device/stream"; |
| 8 | import { createDuoPinch } from "@t3tools/client-runtime/device/duo-control"; |
| 9 | import { mountDeviceViewer } from "./mountDeviceViewer"; |
| 10 | |
| 11 | const loadDuoViewer = () => import("@t3tools/client-runtime/device/duo-viewer"); |
| 12 | |
| 13 | export function DeviceDuoViewport(props: { |
| 14 | readonly model: DeviceModelSource; |
| 15 | readonly source: HTMLCanvasElement; |
| 16 | readonly onFrameListener: (listener: (() => void) | null) => void; |
| 17 | readonly onResetReady: (reset: (() => void) | null) => void; |
| 18 | readonly onInputCancel: (cancel: (() => void) | null) => void; |
| 19 | readonly client: DeviceStreamClient; |
| 20 | readonly screen: DeviceScreenSize | null; |
| 21 | readonly hingePreview: number | null; |
| 22 | readonly onControlRejectedListener: (listener: (() => void) | null) => void; |
| 23 | readonly onUnavailable: () => void; |
| 24 | }) { |
| 25 | const gt = useGT(); |
| 26 | const viewerRef = useRef< |
| 27 | (DuoViewer & { readonly sources: Record<1 | 3, HTMLCanvasElement> }) | null |
| 28 | >(null); |
| 29 | const interactionRef = useRef<ReturnType<typeof createPhoneInteraction> | null>(null); |
| 30 | const pinchRef = useRef<ReturnType<typeof createDuoPinch> | null>(null); |
| 31 | const screenRef = useRef(props.screen); |
| 32 | const modelRef = useRef(props.model); |
| 33 | const previewRef = useRef(props.hingePreview); |
| 34 | const { |
| 35 | source, |
| 36 | client, |
| 37 | onInputCancel, |
| 38 | onControlRejectedListener, |
| 39 | onResetReady, |
| 40 | onUnavailable, |
| 41 | onFrameListener, |
| 42 | } = props; |
| 43 | |
| 44 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- stores screen and model in refs, ends any active interaction, and pushes the screen into the 3D Duo viewer |
| 45 | useEffect(() => { |
| 46 | screenRef.current = props.screen; |
| 47 | modelRef.current = props.model; |
| 48 | interactionRef.current?.end(); |
| 49 | viewerRef.current?.setScreen(props.screen); |
| 50 | }, [props.screen, props.model]); |
| 51 | |
| 52 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes the hinge preview into the imperative 3D Duo viewer unless a pinch is active |
| 53 | useEffect(() => { |
| 54 | previewRef.current = props.hingePreview; |
| 55 | if (!pinchRef.current?.active) viewerRef.current?.setHingePreview(props.hingePreview); |
| 56 | }, [props.hingePreview]); |
| 57 | |
| 58 | // The 3D viewer lives as long as its canvas; later prop changes are pushed into it above. |
| 59 | const mountViewer = (canvas: HTMLCanvasElement | null) => { |
| 60 | const host = canvas?.parentElement; |
| 61 | if (!canvas || !host) return; |
| 62 | return mountDeviceViewer(canvas, host, { |
| 63 | load: () => |
| 64 | loadDuoViewer().then(({ createDuoViewer }) => () => { |
| 65 | const sources = { |
| 66 | 1: document.createElement("canvas"), |
| 67 | 3: document.createElement("canvas"), |
| 68 | }; |
| 69 | const viewer = createDuoViewer({ |
| 70 | canvas, |
| 71 | sources, |
| 72 | onUnavailable, |
| 73 | onPanelRequested: (panel) => |
| 74 | client.controlDuo({ |
| 75 | control: "physical", |
| 76 | value: panel === 1 ? "facedown" : "faceup", |
| 77 | }), |
| 78 | onOrientationRequested: (value) => client.controlDuo({ control: "orientation", value }), |
| 79 | onModelError: (cause) => console.warn("Device 3D asset could not load", cause), |
| 80 | model: modelRef.current, |
| 81 | }); |
| 82 | return { ...viewer, sources }; |
| 83 | }), |
| 84 | touch: (phase, point) => client.sendRawTouch(phase, point.x, point.y), |
| 85 | viewerRef, |
| 86 | interactionRef, |
| 87 | onInputCancel, |
| 88 | onResetReady, |
| 89 | onFrameListener, |
| 90 | onUnavailable, |
| 91 | cancelInput: (viewer) => viewer.cancelInput(), |
| 92 | ready: ({ viewer, cancelTrackpad }) => { |
| 93 | onControlRejectedListener(() => { |
| 94 | cancelTrackpad(); |
| 95 | viewer.rejectOrientation(); |
| 96 | }); |
| 97 | viewer.setScreen(screenRef.current); |
| 98 | viewer.setHingePreview(previewRef.current); |
| 99 | client.setDuoPanels({ |
| 100 | onScreen(next) { |
| 101 | screenRef.current = next; |
| 102 | interactionRef.current?.end(); |
| 103 | viewer.setScreen(next); |
| 104 | }, |
| 105 | cover: createCanvasFrameSink(viewer.sources[1], () => viewer.frameUpdated(1)), |
| 106 | inner: createCanvasFrameSink(viewer.sources[3], () => viewer.frameUpdated(3)), |
| 107 | }); |
| 108 | const pinch = createDuoPinch({ |
| 109 | angle: () => |
| 110 | previewRef.current ?? |
| 111 | screenRef.current?.hingeAngle ?? |
| 112 | (screenRef.current?.screenId === 1 ? 0 : 180), |
| 113 | contains: (x, y) => !!screenRef.current && viewer.beginHinge(x, y), |
| 114 | change: (angle) => { |
| 115 | viewer.setHingePreview(angle ?? previewRef.current, true); |
| 116 | if (angle !== null) client.controlDuo({ control: "angle", value: angle }); |
| 117 | }, |
| 118 | }); |
| 119 | pinchRef.current = pinch; |
| 120 | return { |
| 121 | frame: () => { |
| 122 | const panel = screenRef.current?.screenId; |
| 123 | if (panel === 1 || panel === 3) viewer.frameUpdated(panel, source); |
| 124 | }, |
| 125 | pinch: { |
| 126 | begin(x, y) { |
| 127 | interactionRef.current?.end(); |
| 128 | return pinch.begin(x, y); |
| 129 | }, |
| 130 | move: pinch.move, |
| 131 | end: pinch.end, |
| 132 | }, |
| 133 | dispose: () => { |
| 134 | pinchRef.current = null; |
| 135 | onControlRejectedListener(null); |
| 136 | client.setDuoPanels(null); |
| 137 | }, |
| 138 | }; |
| 139 | }, |
| 140 | }); |
| 141 | }; |
| 142 | |
| 143 | const point = (event: React.PointerEvent<HTMLCanvasElement>) => { |
| 144 | const rect = event.currentTarget.getBoundingClientRect(); |
| 145 | return { |
| 146 | x: (event.clientX - rect.left) / rect.width, |
| 147 | y: (event.clientY - rect.top) / rect.height, |
| 148 | }; |
| 149 | }; |
| 150 | return ( |
| 151 | <div className="absolute inset-0"> |
| 152 | <div className="absolute inset-0"> |
| 153 | <div |
| 154 | aria-hidden |
| 155 | className="pointer-events-none absolute bottom-[6%] left-1/2 h-5 w-2/5 -translate-x-1/2 rounded-full bg-foreground/10 blur-xl" |
| 156 | /> |
| 157 | <canvas |
| 158 | ref={mountViewer} |
| 159 | aria-label={gt( |
| 160 | "Interactive 3D iPhone Duo. Drag the screen to interact. Drag outside it or swipe with two fingers to turn. Pinch over the device to open or close its hinge.", |
| 161 | )} |
| 162 | className="size-full touch-none" |
| 163 | onPointerDown={(event) => { |
| 164 | if (event.button !== 0) return; |
| 165 | if (!interactionRef.current?.begin(event.pointerId, point(event), event.altKey)) return; |
| 166 | event.preventDefault(); |
| 167 | event.currentTarget.setPointerCapture(event.pointerId); |
| 168 | (event.currentTarget.closest('[role="application"]') as HTMLElement | null)?.focus(); |
| 169 | }} |
| 170 | onPointerMove={(event) => interactionRef.current?.move(event.pointerId, point(event))} |
| 171 | onPointerUp={(event) => { |
| 172 | interactionRef.current?.move(event.pointerId, point(event)); |
| 173 | interactionRef.current?.end(event.pointerId); |
| 174 | }} |
| 175 | onPointerCancel={(event) => interactionRef.current?.end(event.pointerId)} |
| 176 | onLostPointerCapture={(event) => interactionRef.current?.end(event.pointerId)} |
| 177 | /> |
| 178 | </div> |
| 179 | </div> |
| 180 | ); |
| 181 | } |