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