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

main
ts 333 lines 11.3 KB
Raw
1 import {
2 PREVIEW_VIEWPORT_MAX_AREA,
3 PREVIEW_VIEWPORT_MAX_DIMENSION,
4 PREVIEW_VIEWPORT_MIN_DIMENSION,
5 type PreviewViewportSetting,
6 type PreviewViewportSize,
7 } from "@t3tools/contracts";
8
9 export interface BrowserViewportLayout {
10 readonly canvasWidth: number;
11 readonly canvasHeight: number;
12 readonly viewportX: number;
13 readonly viewportY: number;
14 readonly viewportWidth: number;
15 readonly viewportHeight: number;
16 readonly viewportScale: number;
17 readonly fillsPanel: boolean;
18 }
19
20 export const BROWSER_DEVICE_TOOLBAR_HEIGHT = 38;
21 export const BROWSER_VIEWPORT_RESIZE_RAIL_SIZE = 10;
22
23 export type BrowserViewportResizeDirection =
24 | "north"
25 | "northeast"
26 | "east"
27 | "southeast"
28 | "south"
29 | "southwest"
30 | "west"
31 | "northwest";
32
33 export const browserViewportSettingKey = (setting: PreviewViewportSetting): string =>
34 setting._tag === "fill"
35 ? "fill"
36 : `${setting._tag}:${setting.width}:${setting.height}:${setting._tag === "preset" ? setting.presetId : ""}`;
37
38 const normalizeZoomFactor = (zoomFactor: number): number =>
39 Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;
40
41 // Convert guest CSS pixels to host CSS pixels without changing the guest's page zoom.
42 export const resolveBrowserLayoutZoomFactor = (
43 previewZoomFactor: number,
44 windowZoomFactor: number,
45 ): number => normalizeZoomFactor(previewZoomFactor) / normalizeZoomFactor(windowZoomFactor);
46
47 export function resolveFittedBrowserViewport(
48 setting: PreviewViewportSetting,
49 sourceContent: {
50 readonly width: number;
51 readonly height: number;
52 readonly scale: number;
53 } | null,
54 zoomFactor = 1,
55 ): Exclude<PreviewViewportSetting, { readonly _tag: "fill" }> {
56 if (setting._tag !== "fill") return setting;
57 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
58 if (sourceContent) {
59 return {
60 _tag: "freeform",
61 width: Math.max(
62 1,
63 Math.round(sourceContent.width / sourceContent.scale / normalizedZoomFactor),
64 ),
65 height: Math.max(
66 1,
67 Math.round(sourceContent.height / sourceContent.scale / normalizedZoomFactor),
68 ),
69 };
70 }
71 return { _tag: "freeform", width: 1280, height: 800 };
72 }
73
74 export function resolveBrowserDeviceViewportArea(container: {
75 readonly width: number;
76 readonly height: number;
77 }): PreviewViewportSize {
78 return {
79 width: Math.max(1, container.width - BROWSER_VIEWPORT_RESIZE_RAIL_SIZE * 2),
80 height: Math.max(
81 1,
82 container.height - BROWSER_DEVICE_TOOLBAR_HEIGHT - BROWSER_VIEWPORT_RESIZE_RAIL_SIZE,
83 ),
84 };
85 }
86
87 export function resolveBrowserViewportLayout(
88 container: { readonly width: number; readonly height: number },
89 setting: PreviewViewportSetting,
90 zoomFactor = 1,
91 ): BrowserViewportLayout {
92 const containerWidth = Math.max(1, Math.round(container.width));
93 const containerHeight = Math.max(1, Math.round(container.height));
94 if (setting._tag === "fill") {
95 return {
96 canvasWidth: containerWidth,
97 canvasHeight: containerHeight,
98 viewportX: 0,
99 viewportY: 0,
100 viewportWidth: containerWidth,
101 viewportHeight: containerHeight,
102 viewportScale: 1,
103 fillsPanel: true,
104 };
105 }
106 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
107 const renderedWidth = setting.width * normalizedZoomFactor;
108 const renderedHeight = setting.height * normalizedZoomFactor;
109 const viewportScale = Math.min(
110 1,
111 containerWidth / renderedWidth,
112 containerHeight / renderedHeight,
113 );
114 const viewportWidth = renderedWidth * viewportScale;
115 const viewportHeight = renderedHeight * viewportScale;
116 return {
117 canvasWidth: containerWidth,
118 canvasHeight: containerHeight,
119 viewportX: Math.max(0, Math.round((containerWidth - viewportWidth) / 2)),
120 viewportY: Math.max(0, Math.round((containerHeight - viewportHeight) / 2)),
121 viewportWidth,
122 viewportHeight,
123 viewportScale,
124 fillsPanel: false,
125 };
126 }
127
128 /**
129 * Chromium rasters a CSS-scaled guest at its on-screen scale, so captures of it lose detail.
130 * Its raster scale also counts x/y-to-z terms, which a `preserve-3d` parent keeps and the
131 * screen drops, so `fullDetail` draws like `scale()` but keeps full-resolution frames.
132 */
133 export function resolveBrowserViewportTransform(
134 scale: number,
135 fullDetail: boolean,
136 ): string | undefined {
137 if (scale >= 1) return undefined;
138 if (!fullDetail) return `scale(${scale})`;
139 // Positive depth keeps the guest in front of its parent for hit testing.
140 const depth = Math.sqrt(1 - scale * scale);
141 return `matrix3d(${scale}, 0, ${depth}, 0, 0, ${scale}, ${depth}, 0, 0, 0, 1, 0, 0, 0, 0, 1)`;
142 }
143
144 export function resolveBrowserDeviceViewportLayout(
145 container: { readonly width: number; readonly height: number },
146 setting: Exclude<PreviewViewportSetting, { readonly _tag: "fill" }>,
147 zoomFactor = 1,
148 ): BrowserViewportLayout {
149 const layout = resolveBrowserViewportLayout(
150 resolveBrowserDeviceViewportArea(container),
151 setting,
152 zoomFactor,
153 );
154 return {
155 ...layout,
156 canvasWidth: Math.max(1, Math.round(container.width)),
157 canvasHeight: Math.max(1, Math.round(container.height)),
158 viewportX: layout.viewportX + BROWSER_VIEWPORT_RESIZE_RAIL_SIZE,
159 viewportY: layout.viewportY + BROWSER_DEVICE_TOOLBAR_HEIGHT,
160 };
161 }
162
163 const clampViewportDimension = (value: number): number =>
164 Math.min(PREVIEW_VIEWPORT_MAX_DIMENSION, Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, value));
165
166 const validAspectRatio = (aspectRatio: number | undefined): aspectRatio is number =>
167 aspectRatio !== undefined && Number.isFinite(aspectRatio) && aspectRatio > 0;
168
169 function resizeAtAspectRatio(
170 desired: number,
171 aspectRatio: number,
172 primaryAxis: "width" | "height",
173 ): PreviewViewportSize {
174 if (primaryAxis === "width") {
175 const minimum = Math.ceil(
176 Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, PREVIEW_VIEWPORT_MIN_DIMENSION * aspectRatio),
177 );
178 const maximum = Math.floor(
179 Math.min(
180 PREVIEW_VIEWPORT_MAX_DIMENSION,
181 PREVIEW_VIEWPORT_MAX_DIMENSION * aspectRatio,
182 Math.sqrt(PREVIEW_VIEWPORT_MAX_AREA * aspectRatio),
183 ),
184 );
185 let width = Math.min(maximum, Math.max(minimum, Math.round(desired)));
186 let height = Math.round(width / aspectRatio);
187 while (width * height > PREVIEW_VIEWPORT_MAX_AREA && width > minimum) {
188 width -= 1;
189 height = Math.round(width / aspectRatio);
190 }
191 return { width, height };
192 }
193
194 const minimum = Math.ceil(
195 Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, PREVIEW_VIEWPORT_MIN_DIMENSION / aspectRatio),
196 );
197 const maximum = Math.floor(
198 Math.min(
199 PREVIEW_VIEWPORT_MAX_DIMENSION,
200 PREVIEW_VIEWPORT_MAX_DIMENSION / aspectRatio,
201 Math.sqrt(PREVIEW_VIEWPORT_MAX_AREA / aspectRatio),
202 ),
203 );
204 let height = Math.min(maximum, Math.max(minimum, Math.round(desired)));
205 let width = Math.round(height * aspectRatio);
206 while (width * height > PREVIEW_VIEWPORT_MAX_AREA && height > minimum) {
207 height -= 1;
208 width = Math.round(height * aspectRatio);
209 }
210 return { width, height };
211 }
212
213 export function resizeFreeformViewport(
214 start: PreviewViewportSize,
215 delta: { readonly x: number; readonly y: number },
216 zoomFactor = 1,
217 direction: BrowserViewportResizeDirection = "southeast",
218 aspectRatio?: number,
219 ): PreviewViewportSize {
220 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
221 const horizontalDelta = direction.includes("east")
222 ? delta.x
223 : direction.includes("west")
224 ? -delta.x
225 : 0;
226 const verticalDelta = direction.includes("south")
227 ? delta.y
228 : direction.includes("north")
229 ? -delta.y
230 : 0;
231 const desiredWidth = start.width + horizontalDelta / normalizedZoomFactor;
232 const desiredHeight = start.height + verticalDelta / normalizedZoomFactor;
233 if (validAspectRatio(aspectRatio)) {
234 const controlsWidth = horizontalDelta !== 0 || direction === "east" || direction === "west";
235 const controlsHeight = verticalDelta !== 0 || direction === "north" || direction === "south";
236 const primaryAxis =
237 controlsWidth && !controlsHeight
238 ? "width"
239 : controlsHeight && !controlsWidth
240 ? "height"
241 : Math.abs(desiredWidth - start.width) / start.width >=
242 Math.abs(desiredHeight - start.height) / start.height
243 ? "width"
244 : "height";
245 return resizeAtAspectRatio(
246 primaryAxis === "width" ? desiredWidth : desiredHeight,
247 aspectRatio,
248 primaryAxis,
249 );
250 }
251 let width = clampViewportDimension(Math.round(desiredWidth));
252 let height = clampViewportDimension(Math.round(desiredHeight));
253 if (width * height <= PREVIEW_VIEWPORT_MAX_AREA) return { width, height };
254 if (Math.abs(horizontalDelta) >= Math.abs(verticalDelta)) {
255 width = Math.max(
256 PREVIEW_VIEWPORT_MIN_DIMENSION,
257 Math.floor(PREVIEW_VIEWPORT_MAX_AREA / height),
258 );
259 } else {
260 height = Math.max(
261 PREVIEW_VIEWPORT_MIN_DIMENSION,
262 Math.floor(PREVIEW_VIEWPORT_MAX_AREA / width),
263 );
264 }
265 return { width, height };
266 }
267
268 const resizeFromEndRail = (start: number, pointerDelta: number, available: number): number => {
269 const startEdge = start < available ? (available + start) / 2 : start;
270 const targetEdge = startEdge + pointerDelta;
271 return targetEdge <= available ? targetEdge * 2 - available : targetEdge;
272 };
273
274 const resizeFromStartRail = (start: number, pointerDelta: number, available: number): number => {
275 if (start > available) {
276 const distanceToFit = start - available;
277 return pointerDelta <= distanceToFit
278 ? start - pointerDelta
279 : available - (pointerDelta - distanceToFit) * 2;
280 }
281 const targetEdge = (available - start) / 2 + pointerDelta;
282 return targetEdge >= 0 ? available - targetEdge * 2 : available - targetEdge;
283 };
284
285 export function resizeBrowserViewportFromRail(
286 start: PreviewViewportSize,
287 pointerDelta: { readonly x: number; readonly y: number },
288 available: PreviewViewportSize,
289 zoomFactor = 1,
290 direction: BrowserViewportResizeDirection = "southeast",
291 aspectRatio?: number,
292 ): PreviewViewportSize {
293 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
294 const startWidth = start.width * normalizedZoomFactor;
295 const startHeight = start.height * normalizedZoomFactor;
296 const desiredWidth = direction.includes("east")
297 ? resizeFromEndRail(startWidth, pointerDelta.x, available.width)
298 : direction.includes("west")
299 ? resizeFromStartRail(startWidth, pointerDelta.x, available.width)
300 : startWidth;
301 const desiredHeight = direction.includes("south")
302 ? resizeFromEndRail(startHeight, pointerDelta.y, available.height)
303 : direction.includes("north")
304 ? resizeFromStartRail(startHeight, pointerDelta.y, available.height)
305 : startHeight;
306 const widthDelta = desiredWidth - startWidth;
307 const heightDelta = desiredHeight - startHeight;
308 return resizeFreeformViewport(
309 start,
310 {
311 x: direction.includes("west") ? -widthDelta : widthDelta,
312 y: direction.includes("north") ? -heightDelta : heightDelta,
313 },
314 normalizedZoomFactor,
315 direction,
316 aspectRatio,
317 );
318 }
319
320 export function resolveResponsiveBrowserViewportSize(
321 container: { readonly width: number; readonly height: number },
322 zoomFactor = 1,
323 ): PreviewViewportSize {
324 const area = resolveBrowserDeviceViewportArea(container);
325 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
326 return resizeFreeformViewport(
327 {
328 width: area.width / normalizedZoomFactor,
329 height: area.height / normalizedZoomFactor,
330 },
331 { x: 0, y: 0 },
332 );
333 }