Open History to see the last commit
1import type { DevicePlatform, PreviewViewportSetting } from "@t3tools/contracts";
2
3import type { BrowserSurfaceContentPresentation } from "~/browser/browserSurfaceStore";
4import {
5 resolveFittedBrowserViewport,
6 type BrowserViewportResizeDirection,
7} from "~/browser/browserViewportLayout";
8import type { PreviewMiniPlayerPosition, PreviewMiniPlayerSize } from "~/previewMiniPlayerStore";
9
10import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream";
11
12export const PREVIEW_MINI_PLAYER_EDGE_GAP = 12;
13export const PREVIEW_MINI_PLAYER_CORNER_RADIUS = 12;
14// The mini-player shell straddles this webview at 47 and 49: above --z-sheet, under dialogs (50).
15export const PREVIEW_MINI_PLAYER_WEBVIEW_Z_INDEX = 48;
16// A fresh player is the largest box at the source aspect ratio that fits here.
17const PREVIEW_MINI_PLAYER_DEFAULT_BOX = { width: 320, height: 320 } as const;
18const PREVIEW_MINI_PLAYER_MIN_SIZE = { width: 240, height: 150 } as const;
19
20export interface PreviewMiniPlayerFrame extends PreviewMiniPlayerPosition, PreviewMiniPlayerSize {}
21
22/**
23 * The rendered size of what the floating player mirrors: the device viewport
24 * when one is set, otherwise the size the webview had when it was floated
25 * (`fittedSourceContent`), which the hosted webview keeps as its CSS viewport.
26 */
27export function resolvePreviewMiniPlayerSourceSize(
28 viewport: PreviewViewportSetting,
29 fittedSourceContent: BrowserSurfaceContentPresentation | null,
30 zoomFactor: number,
31): PreviewMiniPlayerSize {
32 const normalizedZoomFactor = Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;
33 const fitted = resolveFittedBrowserViewport(viewport, fittedSourceContent, normalizedZoomFactor);
34 return {
35 width: fitted.width * normalizedZoomFactor,
36 height: fitted.height * normalizedZoomFactor,
37 };
38}
39
40/**
41 * The device screen as the user sees it, so a rotated phone floats as a
42 * landscape box. Before the stream reports its size the platform's usual phone
43 * shape stands in, matching the stream view's own placeholder aspect; the
44 * nominal width only keeps the source cap above any sensible player width.
45 */
46export function resolveDeviceMiniPlayerSourceSize(
47 platform: DevicePlatform,
48 screen: DeviceScreenSize | null,
49): PreviewMiniPlayerSize {
50 if (!screen) {
51 const width = 1_000;
52 return { width, height: width / (platform === "ios" ? 9 / 19.5 : 9 / 20) };
53 }
54 const landscape =
55 screen.orientation === "landscape_left" || screen.orientation === "landscape_right";
56 const long = Math.max(screen.width, screen.height);
57 const short = Math.min(screen.width, screen.height);
58 return landscape ? { width: long, height: short } : { width: short, height: long };
59}
60
61/**
62 * The Android emulator composites the skin's rounded corners into its
63 * framebuffer as black wedges (measured at ~13% of the short side on a
64 * Pixel 9), so its player clips at a matching phone-like radius; the sliver
65 * lost under the curve is status-bar padding. iOS simulators stream an
66 * edge-to-edge rectangle and keep the frame radius, which matters for iPads
67 * whose real corners are far tighter than a phone's.
68 */
69export function resolveDeviceMiniPlayerCornerRadius(
70 platform: DevicePlatform,
71 player: PreviewMiniPlayerSize,
72): number {
73 if (platform !== "android") return PREVIEW_MINI_PLAYER_CORNER_RADIUS;
74 return Math.max(
75 PREVIEW_MINI_PLAYER_CORNER_RADIUS,
76 Math.round(Math.min(player.width, player.height) * 0.14),
77 );
78}
79
80interface HorizontalSpan {
81 readonly left: number;
82 readonly right: number;
83}
84
85/**
86 * The composer stack docked to the bottom edge, in container coordinates. It
87 * only reserves the columns it covers, so the margins beside it stay open all
88 * the way down.
89 */
90export interface PreviewMiniPlayerObstacles {
91 readonly composer: (HorizontalSpan & { readonly height: number }) | null;
92}
93
94export const NO_PREVIEW_MINI_PLAYER_OBSTACLES: PreviewMiniPlayerObstacles = { composer: null };
95
96const spanOf = (x: number, width: number): HorizontalSpan => ({ left: x, right: x + width });
97
98const spansOverlap = (a: HorizontalSpan, b: HorizontalSpan) => a.left < b.right && a.right > b.left;
99
100/** The lowest row (before the edge gap) open to a player covering these columns. */
101function floorFor(
102 span: HorizontalSpan,
103 container: PreviewMiniPlayerSize,
104 obstacles: PreviewMiniPlayerObstacles,
105): number {
106 const { composer } = obstacles;
107 return composer && spansOverlap(span, composer)
108 ? container.height - Math.max(0, composer.height)
109 : container.height;
110}
111
112/**
113 * The box a stored size is fitted into. A player with a position keeps the
114 * rows its own columns have, so a tall frame parked beside the composer
115 * survives the next layout pass; without one it takes the rows above the
116 * composer, which every column has.
117 */
118const availableArea = (
119 container: PreviewMiniPlayerSize,
120 obstacles: PreviewMiniPlayerObstacles,
121 span: HorizontalSpan | null,
122): PreviewMiniPlayerSize => ({
123 width: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
124 height:
125 (span
126 ? floorFor(span, container, obstacles)
127 : container.height - Math.max(0, obstacles.composer?.height ?? 0)) -
128 PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
129});
130
131/**
132 * Width is the player's only free dimension; height always follows the source
133 * aspect ratio so the webview fills the box without letterboxing. The player
134 * never grows past the source's own size (the guest keeps its CSS viewport, so
135 * going bigger would only upscale), and a tight container wins over the minimum.
136 */
137function fitPreviewMiniPlayerWidth(
138 desiredWidth: number,
139 source: PreviewMiniPlayerSize,
140 max: PreviewMiniPlayerSize,
141): PreviewMiniPlayerSize {
142 const aspectRatio = source.width / source.height;
143 const width = Math.min(
144 Math.max(
145 desiredWidth,
146 PREVIEW_MINI_PLAYER_MIN_SIZE.width,
147 PREVIEW_MINI_PLAYER_MIN_SIZE.height * aspectRatio,
148 ),
149 source.width,
150 Math.max(1, max.width),
151 Math.max(1, max.height * aspectRatio),
152 );
153 return { width: Math.round(width), height: Math.round(width / aspectRatio) };
154}
155
156function defaultPreviewMiniPlayerWidth(source: PreviewMiniPlayerSize): number {
157 return Math.min(
158 PREVIEW_MINI_PLAYER_DEFAULT_BOX.width,
159 (PREVIEW_MINI_PLAYER_DEFAULT_BOX.height * source.width) / source.height,
160 );
161}
162
163const clampToContainer = (
164 position: PreviewMiniPlayerPosition,
165 container: PreviewMiniPlayerSize,
166 player: PreviewMiniPlayerSize,
167 bottom = container.height,
168): PreviewMiniPlayerPosition => ({
169 x: Math.min(
170 Math.max(position.x, PREVIEW_MINI_PLAYER_EDGE_GAP),
171 Math.max(
172 PREVIEW_MINI_PLAYER_EDGE_GAP,
173 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
174 ),
175 ),
176 y: Math.min(
177 Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP),
178 Math.max(PREVIEW_MINI_PLAYER_EDGE_GAP, bottom - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP),
179 ),
180});
181
182const overlapsObstacle = (
183 position: PreviewMiniPlayerPosition,
184 player: PreviewMiniPlayerSize,
185 container: PreviewMiniPlayerSize,
186 obstacles: PreviewMiniPlayerObstacles,
187): boolean =>
188 position.y + player.height > floorFor(spanOf(position.x, player.width), container, obstacles);
189
190/**
191 * Keeps the player inside the container and off the composer. An overlapping
192 * player is pushed out along whichever side needs the smaller move, so a drag
193 * slides along the composer into the margin beside it instead of stopping at
194 * its top edge; when no side leaves it fully clear it sits above the composer.
195 */
196export function clampPreviewMiniPlayerPosition(
197 position: PreviewMiniPlayerPosition,
198 container: PreviewMiniPlayerSize,
199 player: PreviewMiniPlayerSize,
200 obstacles: PreviewMiniPlayerObstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
201): PreviewMiniPlayerPosition {
202 const inside = clampToContainer(position, container, player);
203 const { composer } = obstacles;
204 if (!composer || !overlapsObstacle(inside, player, container, obstacles)) return inside;
205 const gap = PREVIEW_MINI_PLAYER_EDGE_GAP;
206 const above = { x: inside.x, y: container.height - composer.height - gap - player.height };
207 const beside = [
208 { x: composer.left - gap - player.width, y: inside.y },
209 { x: composer.right + gap, y: inside.y },
210 ];
211 let best = clampToContainer(above, container, player);
212 let bestDistance = Math.abs(best.y - inside.y);
213 for (const candidate of beside) {
214 const clamped = clampToContainer(candidate, container, player);
215 if (clamped.x !== candidate.x || overlapsObstacle(candidate, player, container, obstacles)) {
216 continue;
217 }
218 const distance = Math.abs(candidate.x - inside.x);
219 if (distance < bestDistance) {
220 best = candidate;
221 bestDistance = distance;
222 }
223 }
224 return best;
225}
226
227/**
228 * Resolves the on-screen frame from the stored width and position. Clamping
229 * happens here on every layout pass instead of being written back to the
230 * store, so a temporarily narrow container never destroys the user's chosen
231 * width. A player without a position sits in the top-right corner.
232 */
233export function resolvePreviewMiniPlayerFrame(input: {
234 readonly width: number | null;
235 readonly position: PreviewMiniPlayerPosition | null;
236 readonly source: PreviewMiniPlayerSize;
237 readonly container: PreviewMiniPlayerSize;
238 readonly obstacles?: PreviewMiniPlayerObstacles;
239}): PreviewMiniPlayerFrame {
240 const {
241 width,
242 position,
243 source,
244 container,
245 obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
246 } = input;
247 const size = fitPreviewMiniPlayerWidth(
248 width ?? defaultPreviewMiniPlayerWidth(source),
249 source,
250 availableArea(container, obstacles, position && width ? spanOf(position.x, width) : null),
251 );
252 const anchored = position ?? {
253 x: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - size.width,
254 y: PREVIEW_MINI_PLAYER_EDGE_GAP,
255 };
256 return { ...clampPreviewMiniPlayerPosition(anchored, container, size, obstacles), ...size };
257}
258
259/**
260 * Resizes from any edge or corner while holding the aspect ratio. The edge
261 * opposite the dragged one stays anchored, so growth stops at the container
262 * on that axis and the pointer keeps tracking the grabbed edge. On a plain edge
263 * drag the perpendicular axis may use the whole container, and the player
264 * shifts as needed to stay inside.
265 */
266export function resizePreviewMiniPlayer(input: {
267 readonly start: PreviewMiniPlayerFrame;
268 readonly direction: BrowserViewportResizeDirection;
269 readonly delta: PreviewMiniPlayerPosition;
270 readonly source: PreviewMiniPlayerSize;
271 readonly container: PreviewMiniPlayerSize;
272 readonly obstacles?: PreviewMiniPlayerObstacles;
273}): PreviewMiniPlayerFrame {
274 const {
275 start,
276 direction,
277 delta,
278 source,
279 container,
280 obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
281 } = input;
282 const east = direction.includes("east");
283 const west = direction.includes("west");
284 const north = direction.includes("north");
285 const south = direction.includes("south");
286 const right = start.x + start.width;
287 const bottom = start.y + start.height;
288 // Growth stops where the player's current columns meet the composer, and a
289 // plain edge drag lets the free axis use everything those columns have. A
290 // wider player may reach new columns; the clamp below slides it clear.
291 const floor = floorFor(spanOf(start.x, start.width), container, obstacles);
292 const max = {
293 width: west
294 ? right - PREVIEW_MINI_PLAYER_EDGE_GAP
295 : east
296 ? container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - start.x
297 : container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
298 height: north
299 ? bottom - PREVIEW_MINI_PLAYER_EDGE_GAP
300 : south
301 ? floor - PREVIEW_MINI_PLAYER_EDGE_GAP - start.y
302 : floor - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
303 };
304 const desiredWidth = start.width + (east ? delta.x : west ? -delta.x : 0);
305 const desiredHeight = start.height + (south ? delta.y : north ? -delta.y : 0);
306 const horizontal = east || west;
307 const vertical = north || south;
308 const widthLeads =
309 horizontal && !vertical
310 ? true
311 : vertical && !horizontal
312 ? false
313 : Math.abs(desiredWidth - start.width) / start.width >=
314 Math.abs(desiredHeight - start.height) / start.height;
315 const size = fitPreviewMiniPlayerWidth(
316 widthLeads ? desiredWidth : (desiredHeight * source.width) / source.height,
317 source,
318 max,
319 );
320 const position = clampPreviewMiniPlayerPosition(
321 { x: west ? right - size.width : start.x, y: north ? bottom - size.height : start.y },
322 container,
323 size,
324 obstacles,
325 );
326 return { ...position, ...size };
327}