Open History to see the last commit
1import { useEffect, useRef, type RefObject } from "react";
2import type { PhoneViewer } from "@t3tools/client-runtime/device/phone-viewer";
3import type {
4 DeviceAccessorySource,
5 DeviceModelSource,
6} from "@t3tools/client-runtime/device/model";
7import type { DeviceShapeProfile } from "@t3tools/client-runtime/device/shape-profile";
8import { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
9import type { DeviceScreenSize, DeviceStreamClient } from "@t3tools/client-runtime/device/stream";
10import { bindPhoneTrackpad } from "./phoneTrackpad";
11
12const loadPhoneViewer = () => import("@t3tools/client-runtime/device/phone-viewer");
13
14/** Web shell for the framework-independent viewer. The decoded screen and input connection remain owned by DeviceStreamView. */
15export function DevicePhoneViewport(props: {
16 readonly profile: DeviceShapeProfile;
17 readonly model: DeviceModelSource | null;
18 readonly accessory: DeviceAccessorySource | null;
19 readonly source: RefObject<HTMLCanvasElement | null>;
20 readonly onFrameListener: (listener: (() => void) | null) => void;
21 readonly onResetReady: (reset: (() => void) | null) => void;
22 readonly onInputCancel: (cancel: (() => void) | null) => void;
23 readonly client: RefObject<DeviceStreamClient | null>;
24 readonly screen: DeviceScreenSize | null;
25 readonly onUnavailable: () => void;
26}) {
27 const hostRef = useRef<HTMLDivElement | null>(null);
28 const canvasRef = useRef<HTMLCanvasElement | null>(null);
29 const viewerRef = useRef<PhoneViewer | null>(null);
30 const interactionRef = useRef<ReturnType<typeof createPhoneInteraction> | null>(null);
31 const screenRef = useRef(props.screen);
32 const profileRef = useRef(props.profile);
33 const modelRef = useRef(props.model);
34 const accessoryRef = useRef(props.accessory);
35 const { source, onFrameListener, client, onInputCancel, onResetReady, onUnavailable } = props;
36
37 useEffect(() => {
38 screenRef.current = props.screen;
39 profileRef.current = props.profile;
40 modelRef.current = props.model;
41 accessoryRef.current = props.accessory;
42 interactionRef.current?.end();
43 viewerRef.current?.setModel(props.model);
44 viewerRef.current?.setAccessory(props.accessory);
45 viewerRef.current?.setScreen(props.screen, props.profile);
46 }, [props.screen, props.profile, props.model, props.accessory]);
47
48 useEffect(() => {
49 const host = hostRef.current;
50 const canvas = canvasRef.current;
51 const decoded = source.current;
52 if (!host || !canvas || !decoded) return;
53 let disposed = false;
54 let trackpad: ReturnType<typeof bindPhoneTrackpad> | null = null;
55 let stopTrackpadEnd: (() => void) | undefined;
56 const resize = () => {
57 const { width, height } = host.getBoundingClientRect();
58 viewerRef.current?.resize(width, height, window.devicePixelRatio);
59 };
60 const observer = new ResizeObserver(resize);
61 observer.observe(host);
62 const blur = () => {
63 interactionRef.current?.end();
64 trackpad?.cancel();
65 };
66 onInputCancel(blur);
67 window.addEventListener("blur", blur);
68 void loadPhoneViewer()
69 .then(({ createPhoneViewer }) => {
70 if (disposed) return;
71 const viewer = createPhoneViewer({
72 canvas,
73 source: decoded,
74 onUnavailable,
75 onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
76 profile: profileRef.current,
77 model: modelRef.current,
78 accessory: accessoryRef.current,
79 });
80 viewerRef.current = viewer;
81 onResetReady(viewer.resetPose);
82 viewer.setScreen(screenRef.current);
83 onFrameListener(viewer.frameUpdated);
84 interactionRef.current = createPhoneInteraction({
85 screenPoint: (point, captured) => viewer.screenPoint(point.x, point.y, captured),
86 touch: (phase, point) => client.current?.sendTouch(phase, point.x, point.y),
87 orbit: viewer.orbit,
88 zoomBy: () => {},
89 onInteractionActive: viewer.setInteractionActive,
90 });
91 trackpad = bindPhoneTrackpad(canvas, interactionRef.current);
92 stopTrackpadEnd = window.desktopBridge?.onTrackpadScrollEnd?.(() => trackpad?.endOrbit());
93 resize();
94 viewer.frameUpdated();
95 })
96 .catch(() => {
97 if (!disposed) onUnavailable();
98 });
99 return () => {
100 disposed = true;
101 stopTrackpadEnd?.();
102 trackpad?.dispose();
103 interactionRef.current?.end();
104 interactionRef.current = null;
105 onInputCancel(null);
106 onResetReady(null);
107 onFrameListener(null);
108 observer.disconnect();
109 window.removeEventListener("blur", blur);
110 viewerRef.current?.dispose();
111 viewerRef.current = null;
112 };
113 }, [onInputCancel, onResetReady, client, onFrameListener, onUnavailable, source]);
114
115 const point = (event: React.PointerEvent<HTMLCanvasElement>) => {
116 const rect = event.currentTarget.getBoundingClientRect();
117 return {
118 x: (event.clientX - rect.left) / rect.width,
119 y: (event.clientY - rect.top) / rect.height,
120 };
121 };
122 return (
123 <div className="absolute inset-0">
124 <div ref={hostRef} className="absolute inset-0">
125 <div
126 aria-hidden
127 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"
128 />
129 <canvas
130 ref={canvasRef}
131 aria-label="Interactive 3D device. Drag the screen to interact. Drag outside it or swipe with two fingers to turn."
132 className="size-full touch-none"
133 onPointerDown={(event) => {
134 if (event.button !== 0) return;
135 if (!interactionRef.current?.begin(event.pointerId, point(event), event.altKey)) return;
136 event.preventDefault();
137 event.currentTarget.setPointerCapture(event.pointerId);
138 (event.currentTarget.closest('[role="application"]') as HTMLElement | null)?.focus();
139 }}
140 onPointerMove={(event) => interactionRef.current?.move(event.pointerId, point(event))}
141 onPointerUp={(event) => {
142 interactionRef.current?.move(event.pointerId, point(event));
143 interactionRef.current?.end(event.pointerId);
144 }}
145 onPointerCancel={(event) => interactionRef.current?.end(event.pointerId)}
146 onLostPointerCapture={(event) => interactionRef.current?.end(event.pointerId)}
147 />
148 </div>
149 </div>
150 );
151}