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