Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserViewportLayout.ts

286 lines9.7 KB

1import {
2 PREVIEW_VIEWPORT_MAX_AREA,
3 PREVIEW_VIEWPORT_MAX_DIMENSION,
4 PREVIEW_VIEWPORT_MIN_DIMENSION,
5 type PreviewViewportSetting,
6 type PreviewViewportSize,
7} from "@v12/contracts";
8​
9export interface BrowserViewportLayout {
10 readonly canvasWidth: number;
11 readonly canvasHeight: number;
12 readonly viewportX: number;
13 readonly viewportY: number;
14 /** Visible footprint inside the preview panel after fit-to-panel scaling. */
15 readonly viewportWidth: number;
16 readonly viewportHeight: number;
17 /** Presentation-only scale; the guest keeps its requested CSS viewport. */
18 readonly viewportScale: number;
19 readonly fillsPanel: boolean;
21​
22export const BROWSER_DEVICE_TOOLBAR_HEIGHT = 32;
23export const BROWSER_VIEWPORT_RESIZE_RAIL_SIZE = 10;
24​
25export type BrowserViewportResizeDirection =
26 | "north"
27 | "northeast"
28 | "east"
29 | "southeast"
30 | "south"
31 | "southwest"
32 | "west"
33 | "northwest";
34​
35export const browserViewportSettingKey = (setting: PreviewViewportSetting): string =>
36 setting._tag === "fill"
37 ? "fill"
38 : `${setting._tag}:${setting.width}:${setting.height}:${setting._tag === "preset" ? setting.presetId : ""}`;
39​
40const normalizeZoomFactor = (zoomFactor: number): number =>
41 Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;
42​
43export function resolveBrowserDeviceViewportArea(container: {
44 readonly width: number;
45 readonly height: number;
46}): PreviewViewportSize {
47 return {
48 width: Math.max(1, container.width - BROWSER_VIEWPORT_RESIZE_RAIL_SIZE * 2),
49 height: Math.max(
50 1,
51 container.height - BROWSER_DEVICE_TOOLBAR_HEIGHT - BROWSER_VIEWPORT_RESIZE_RAIL_SIZE,
52 ),
53 };
55​
56export function resolveBrowserViewportLayout(
57 container: { readonly width: number; readonly height: number },
58 setting: PreviewViewportSetting,
59 zoomFactor = 1,
60): BrowserViewportLayout {
61 const containerWidth = Math.max(1, Math.round(container.width));
62 const containerHeight = Math.max(1, Math.round(container.height));
63 if (setting._tag === "fill") {
64 return {
65 canvasWidth: containerWidth,
66 canvasHeight: containerHeight,
67 viewportX: 0,
68 viewportY: 0,
69 viewportWidth: containerWidth,
70 viewportHeight: containerHeight,
71 viewportScale: 1,
72 fillsPanel: true,
73 };
74 }
75 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
76 const renderedWidth = setting.width * normalizedZoomFactor;
77 const renderedHeight = setting.height * normalizedZoomFactor;
78 const viewportScale = Math.min(
79 1,
80 containerWidth / renderedWidth,
81 containerHeight / renderedHeight,
82 );
83 const viewportWidth = renderedWidth * viewportScale;
84 const viewportHeight = renderedHeight * viewportScale;
85 return {
86 canvasWidth: containerWidth,
87 canvasHeight: containerHeight,
88 viewportX: Math.max(0, Math.round((containerWidth - viewportWidth) / 2)),
89 viewportY: Math.max(0, Math.round((containerHeight - viewportHeight) / 2)),
90 viewportWidth,
91 viewportHeight,
92 viewportScale,
93 fillsPanel: false,
94 };
96​
97export function resolveBrowserDeviceViewportLayout(
98 container: { readonly width: number; readonly height: number },
99 setting: Exclude<PreviewViewportSetting, { readonly _tag: "fill" }>,
100 zoomFactor = 1,
101): BrowserViewportLayout {
102 const layout = resolveBrowserViewportLayout(
103 resolveBrowserDeviceViewportArea(container),
104 setting,
105 zoomFactor,
106 );
107 return {
108 ...layout,
109 canvasWidth: Math.max(1, Math.round(container.width)),
110 canvasHeight: Math.max(1, Math.round(container.height)),
111 viewportX: layout.viewportX + BROWSER_VIEWPORT_RESIZE_RAIL_SIZE,
112 viewportY: layout.viewportY + BROWSER_DEVICE_TOOLBAR_HEIGHT,
113 };
115​
116const clampViewportDimension = (value: number): number =>
117 Math.min(PREVIEW_VIEWPORT_MAX_DIMENSION, Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, value));
118​
119const validAspectRatio = (aspectRatio: number | undefined): aspectRatio is number =>
120 aspectRatio !== undefined && Number.isFinite(aspectRatio) && aspectRatio > 0;
121​
122function resizeAtAspectRatio(
123 desired: number,
124 aspectRatio: number,
125 primaryAxis: "width" | "height",
126): PreviewViewportSize {
127 if (primaryAxis === "width") {
128 const minimum = Math.ceil(
129 Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, PREVIEW_VIEWPORT_MIN_DIMENSION * aspectRatio),
130 );
131 const maximum = Math.floor(
132 Math.min(
133 PREVIEW_VIEWPORT_MAX_DIMENSION,
134 PREVIEW_VIEWPORT_MAX_DIMENSION * aspectRatio,
135 Math.sqrt(PREVIEW_VIEWPORT_MAX_AREA * aspectRatio),
136 ),
137 );
138 let width = Math.min(maximum, Math.max(minimum, Math.round(desired)));
139 let height = Math.round(width / aspectRatio);
140 while (width * height > PREVIEW_VIEWPORT_MAX_AREA && width > minimum) {
141 width -= 1;
142 height = Math.round(width / aspectRatio);
143 }
144 return { width, height };
145 }
146​
147 const minimum = Math.ceil(
148 Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, PREVIEW_VIEWPORT_MIN_DIMENSION / aspectRatio),
149 );
150 const maximum = Math.floor(
151 Math.min(
152 PREVIEW_VIEWPORT_MAX_DIMENSION,
153 PREVIEW_VIEWPORT_MAX_DIMENSION / aspectRatio,
154 Math.sqrt(PREVIEW_VIEWPORT_MAX_AREA / aspectRatio),
155 ),
156 );
157 let height = Math.min(maximum, Math.max(minimum, Math.round(desired)));
158 let width = Math.round(height * aspectRatio);
159 while (width * height > PREVIEW_VIEWPORT_MAX_AREA && height > minimum) {
160 height -= 1;
161 width = Math.round(height * aspectRatio);
162 }
163 return { width, height };
165​
166export function resizeFreeformViewport(
167 start: PreviewViewportSize,
168 delta: { readonly x: number; readonly y: number },
169 zoomFactor = 1,
170 direction: BrowserViewportResizeDirection = "southeast",
171 aspectRatio?: number,
172): PreviewViewportSize {
173 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
174 const horizontalDelta = direction.includes("east")
175 ? delta.x
176 : direction.includes("west")
177 ? -delta.x
178 : 0;
179 const verticalDelta = direction.includes("south")
180 ? delta.y
181 : direction.includes("north")
182 ? -delta.y
183 : 0;
184 const desiredWidth = start.width + horizontalDelta / normalizedZoomFactor;
185 const desiredHeight = start.height + verticalDelta / normalizedZoomFactor;
186 if (validAspectRatio(aspectRatio)) {
187 const controlsWidth = horizontalDelta !== 0 || direction === "east" || direction === "west";
188 const controlsHeight = verticalDelta !== 0 || direction === "north" || direction === "south";
189 const primaryAxis =
190 controlsWidth && !controlsHeight
191 ? "width"
192 : controlsHeight && !controlsWidth
193 ? "height"
194 : Math.abs(desiredWidth - start.width) / start.width >=
195 Math.abs(desiredHeight - start.height) / start.height
196 ? "width"
197 : "height";
198 return resizeAtAspectRatio(
199 primaryAxis === "width" ? desiredWidth : desiredHeight,
200 aspectRatio,
201 primaryAxis,
202 );
203 }
204 let width = clampViewportDimension(Math.round(desiredWidth));
205 let height = clampViewportDimension(Math.round(desiredHeight));
206 if (width * height <= PREVIEW_VIEWPORT_MAX_AREA) return { width, height };
207 if (Math.abs(horizontalDelta) >= Math.abs(verticalDelta)) {
208 width = Math.max(
209 PREVIEW_VIEWPORT_MIN_DIMENSION,
210 Math.floor(PREVIEW_VIEWPORT_MAX_AREA / height),
211 );
212 } else {
213 height = Math.max(
214 PREVIEW_VIEWPORT_MIN_DIMENSION,
215 Math.floor(PREVIEW_VIEWPORT_MAX_AREA / width),
216 );
217 }
218 return { width, height };
220​
221const resizeFromEndRail = (start: number, pointerDelta: number, available: number): number => {
222 const startEdge = start < available ? (available + start) / 2 : start;
223 const targetEdge = startEdge + pointerDelta;
224 return targetEdge <= available ? targetEdge * 2 - available : targetEdge;
225};
226​
227const resizeFromStartRail = (start: number, pointerDelta: number, available: number): number => {
228 if (start > available) {
229 const distanceToFit = start - available;
230 return pointerDelta <= distanceToFit
231 ? start - pointerDelta
232 : available - (pointerDelta - distanceToFit) * 2;
233 }
234 const targetEdge = (available - start) / 2 + pointerDelta;
235 return targetEdge >= 0 ? available - targetEdge * 2 : available - targetEdge;
236};
237​
238export function resizeBrowserViewportFromRail(
239 start: PreviewViewportSize,
240 pointerDelta: { readonly x: number; readonly y: number },
241 available: PreviewViewportSize,
242 zoomFactor = 1,
243 direction: BrowserViewportResizeDirection = "southeast",
244 aspectRatio?: number,
245): PreviewViewportSize {
246 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
247 const startWidth = start.width * normalizedZoomFactor;
248 const startHeight = start.height * normalizedZoomFactor;
249 const desiredWidth = direction.includes("east")
250 ? resizeFromEndRail(startWidth, pointerDelta.x, available.width)
251 : direction.includes("west")
252 ? resizeFromStartRail(startWidth, pointerDelta.x, available.width)
253 : startWidth;
254 const desiredHeight = direction.includes("south")
255 ? resizeFromEndRail(startHeight, pointerDelta.y, available.height)
256 : direction.includes("north")
257 ? resizeFromStartRail(startHeight, pointerDelta.y, available.height)
258 : startHeight;
259 const widthDelta = desiredWidth - startWidth;
260 const heightDelta = desiredHeight - startHeight;
261 return resizeFreeformViewport(
262 start,
263 {
264 x: direction.includes("west") ? -widthDelta : widthDelta,
265 y: direction.includes("north") ? -heightDelta : heightDelta,
266 },
267 normalizedZoomFactor,
268 direction,
269 aspectRatio,
270 );
272​
273export function resolveResponsiveBrowserViewportSize(
274 container: { readonly width: number; readonly height: number },
275 zoomFactor = 1,
276): PreviewViewportSize {
277 const area = resolveBrowserDeviceViewportArea(container);
278 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
279 return resizeFreeformViewport(
280 {
281 width: area.width / normalizedZoomFactor,
282 height: area.height / normalizedZoomFactor,
283 },
284 { x: 0, y: 0 },
285 );