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

previewMiniPlayerLayout.ts

327 lines11.4 KB

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;
16const PREVIEW_MINI_PLAYER_DEFAULT_BOX = { width: 320, height: 320 } as const;
17const PREVIEW_MINI_PLAYER_MIN_SIZE = { width: 240, height: 150 } as const;
18​
19export interface PreviewMiniPlayerFrame extends PreviewMiniPlayerPosition, PreviewMiniPlayerSize {}
20​
21export function resolvePreviewMiniPlayerSourceSize(
22 viewport: PreviewViewportSetting,
23 fittedSourceContent: BrowserSurfaceContentPresentation | null,
24 zoomFactor: number,
25): PreviewMiniPlayerSize {
26 const normalizedZoomFactor = Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;
27 const fitted = resolveFittedBrowserViewport(viewport, fittedSourceContent, normalizedZoomFactor);
28 return {
29 width: fitted.width * normalizedZoomFactor,
30 height: fitted.height * normalizedZoomFactor,
31 };
33​
34export function resolveDeviceMiniPlayerSourceSize(
35 platform: DevicePlatform,
36 screen: DeviceScreenSize | null,
37): PreviewMiniPlayerSize {
38 if (!screen) {
39 const width = 1_000;
40 return { width, height: width / (platform === "ios" ? 9 / 19.5 : 9 / 20) };
41 }
42 const landscape =
43 screen.orientation === "landscape_left" || screen.orientation === "landscape_right";
44 const long = Math.max(screen.width, screen.height);
45 const short = Math.min(screen.width, screen.height);
46 return landscape ? { width: long, height: short } : { width: short, height: long };
48​
49/**
50 * The Android emulator composites the skin's rounded corners into its
51 * framebuffer as black wedges (measured at ~13% of the short side on a
52 * Pixel 9), so its player clips at a matching phone-like radius; the sliver
53 * lost under the curve is status-bar padding. iOS simulators stream an
54 * edge-to-edge rectangle and keep the frame radius, which matters for iPads
55 * whose real corners are far tighter than a phone's.
56 */
57export function resolveDeviceMiniPlayerCornerRadius(
58 platform: DevicePlatform,
59 player: PreviewMiniPlayerSize,
60): number {
61 if (platform !== "android") return PREVIEW_MINI_PLAYER_CORNER_RADIUS;
62 return Math.max(
63 PREVIEW_MINI_PLAYER_CORNER_RADIUS,
64 Math.round(Math.min(player.width, player.height) * 0.14),
65 );
67​
68interface HorizontalSpan {
69 readonly left: number;
70 readonly right: number;
72​
73export interface PreviewMiniPlayerObstacles {
74 readonly composer: (HorizontalSpan & { readonly height: number }) | null;
75 readonly detailsCard: (HorizontalSpan & { readonly bottom: number }) | null;
77​
78export const NO_PREVIEW_MINI_PLAYER_OBSTACLES: PreviewMiniPlayerObstacles = {
79 composer: null,
80 detailsCard: null,
81};
82​
83const spanOf = (x: number, width: number): HorizontalSpan => ({ left: x, right: x + width });
84​
85const spansOverlap = (a: HorizontalSpan, b: HorizontalSpan) => a.left < b.right && a.right > b.left;
86​
87function floorFor(
88 span: HorizontalSpan,
89 container: PreviewMiniPlayerSize,
90 obstacles: PreviewMiniPlayerObstacles,
91): number {
92 const { composer } = obstacles;
93 return composer && spansOverlap(span, composer)
94 ? container.height - Math.max(0, composer.height)
95 : container.height;
97​
98function ceilingFor(span: HorizontalSpan, obstacles: PreviewMiniPlayerObstacles): number {
99 const { detailsCard } = obstacles;
100 return detailsCard && spansOverlap(span, detailsCard) ? Math.max(0, detailsCard.bottom) : 0;
102​
103const availableArea = (
104 container: PreviewMiniPlayerSize,
105 obstacles: PreviewMiniPlayerObstacles,
106 span: HorizontalSpan | null,
107): PreviewMiniPlayerSize => ({
108 width: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
109 height:
110 (span
111 ? floorFor(span, container, obstacles) - ceilingFor(span, obstacles)
112 : container.height - Math.max(0, obstacles.composer?.height ?? 0)) -
113 PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
114});
115​
116function fitPreviewMiniPlayerWidth(
117 desiredWidth: number,
118 source: PreviewMiniPlayerSize,
119 max: PreviewMiniPlayerSize,
120): PreviewMiniPlayerSize {
121 const aspectRatio = source.width / source.height;
122 const width = Math.min(
123 Math.max(
124 desiredWidth,
125 PREVIEW_MINI_PLAYER_MIN_SIZE.width,
126 PREVIEW_MINI_PLAYER_MIN_SIZE.height * aspectRatio,
127 ),
128 Math.max(1, max.width),
129 Math.max(1, max.height * aspectRatio),
130 );
131 return { width: Math.round(width), height: Math.round(width / aspectRatio) };
133​
134function defaultPreviewMiniPlayerWidth(source: PreviewMiniPlayerSize): number {
135 return Math.min(
136 PREVIEW_MINI_PLAYER_DEFAULT_BOX.width,
137 (PREVIEW_MINI_PLAYER_DEFAULT_BOX.height * source.width) / source.height,
138 );
140​
141const clampToContainer = (
142 position: PreviewMiniPlayerPosition,
143 container: PreviewMiniPlayerSize,
144 player: PreviewMiniPlayerSize,
145 minimumX = PREVIEW_MINI_PLAYER_EDGE_GAP,
146): PreviewMiniPlayerPosition => ({
147 x: Math.min(
148 Math.max(position.x, minimumX, PREVIEW_MINI_PLAYER_EDGE_GAP),
149 Math.max(
150 PREVIEW_MINI_PLAYER_EDGE_GAP,
151 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
152 ),
153 ),
154 y: Math.min(
155 Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP),
156 Math.max(
157 PREVIEW_MINI_PLAYER_EDGE_GAP,
158 container.height - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP,
159 ),
160 ),
161});
162​
163const overlapsObstacle = (
164 position: PreviewMiniPlayerPosition,
165 player: PreviewMiniPlayerSize,
166 container: PreviewMiniPlayerSize,
167 obstacles: PreviewMiniPlayerObstacles,
168): boolean => {
169 const span = spanOf(position.x, player.width);
170 return (
171 position.y + player.height > floorFor(span, container, obstacles) ||
172 position.y < ceilingFor(span, obstacles)
173 );
174};
175​
176export function clampPreviewMiniPlayerPosition(
177 position: PreviewMiniPlayerPosition,
178 container: PreviewMiniPlayerSize,
179 player: PreviewMiniPlayerSize,
180 obstacles: PreviewMiniPlayerObstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
181 minimumX = PREVIEW_MINI_PLAYER_EDGE_GAP,
182): PreviewMiniPlayerPosition {
183 const inside = clampToContainer(position, container, player, minimumX);
184 if (!overlapsObstacle(inside, player, container, obstacles)) return inside;
185 const gap = PREVIEW_MINI_PLAYER_EDGE_GAP;
186 const fitRows = (x: number): PreviewMiniPlayerPosition => {
187 const span = spanOf(x, player.width);
188 return {
189 x,
190 y: Math.min(
191 container.height - gap - player.height,
192 Math.max(
193 Math.min(inside.y, floorFor(span, container, obstacles) - gap - player.height),
194 ceilingFor(span, obstacles) + gap,
195 gap,
196 ),
197 ),
198 };
199 };
200 const fallback = fitRows(inside.x);
201 const { composer, detailsCard } = obstacles;
202 const span = spanOf(inside.x, player.width);
203 const beside = [composer, detailsCard]
204 .filter(
205 (obstacle): obstacle is NonNullable<typeof obstacle> =>
206 obstacle !== null && spansOverlap(span, obstacle),
207 )
208 .flatMap((obstacle) => [obstacle.left - gap - player.width, obstacle.right + gap]);
209 let best = fallback;
210 let bestDistance = overlapsObstacle(fallback, player, container, obstacles)
211 ? Number.POSITIVE_INFINITY
212 : Math.abs(fallback.y - inside.y);
213 for (const x of beside) {
214 const candidate = fitRows(x);
215 if (
216 clampToContainer(candidate, container, player, minimumX).x !== x ||
217 overlapsObstacle(candidate, player, container, obstacles)
218 ) {
219 continue;
220 }
221 const distance = Math.abs(x - inside.x) + Math.abs(candidate.y - inside.y);
222 if (distance < bestDistance) {
223 best = candidate;
224 bestDistance = distance;
225 }
226 }
227 return best;
229​
230/**
231 * Clamping happens here on every layout pass instead of being written back to the store, so a
232 * temporarily narrow container never destroys the user's chosen width.
233 */
234export function resolvePreviewMiniPlayerFrame(input: {
235 readonly width: number | null;
236 readonly position: PreviewMiniPlayerPosition | null;
237 readonly source: PreviewMiniPlayerSize;
238 readonly container: PreviewMiniPlayerSize;
239 readonly obstacles?: PreviewMiniPlayerObstacles;
240}): PreviewMiniPlayerFrame {
241 const {
242 width,
243 position,
244 source,
245 container,
246 obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
247 } = input;
248 const size = fitPreviewMiniPlayerWidth(
249 width ?? defaultPreviewMiniPlayerWidth(source),
250 source,
251 availableArea(
252 container,
253 position && width ? { composer: obstacles.composer, detailsCard: null } : obstacles,
254 position && width ? spanOf(position.x, width) : null,
255 ),
256 );
257 const { detailsCard } = obstacles;
258 const anchored =
259 position ??
260 (detailsCard
261 ? { x: detailsCard.right - size.width, y: detailsCard.bottom + PREVIEW_MINI_PLAYER_EDGE_GAP }
262 : {
263 x: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - size.width,
264 y: PREVIEW_MINI_PLAYER_EDGE_GAP,
265 });
266 return { ...clampPreviewMiniPlayerPosition(anchored, container, size, obstacles), ...size };
268​
269export function resizePreviewMiniPlayer(input: {
270 readonly start: PreviewMiniPlayerFrame;
271 readonly direction: BrowserViewportResizeDirection;
272 readonly delta: PreviewMiniPlayerPosition;
273 readonly source: PreviewMiniPlayerSize;
274 readonly container: PreviewMiniPlayerSize;
275 readonly obstacles?: PreviewMiniPlayerObstacles;
276}): PreviewMiniPlayerFrame {
277 const {
278 start,
279 direction,
280 delta,
281 source,
282 container,
283 obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
284 } = input;
285 const east = direction.includes("east");
286 const west = direction.includes("west");
287 const north = direction.includes("north");
288 const south = direction.includes("south");
289 const right = start.x + start.width;
290 const bottom = start.y + start.height;
291 const span = spanOf(start.x, start.width);
292 const floor = floorFor(span, container, obstacles);
293 const ceiling = ceilingFor(span, obstacles);
294 const max = {
295 width: west
296 ? right - PREVIEW_MINI_PLAYER_EDGE_GAP
297 : east
298 ? container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - start.x
299 : container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
300 height: north
301 ? bottom - ceiling - PREVIEW_MINI_PLAYER_EDGE_GAP
302 : south
303 ? floor - PREVIEW_MINI_PLAYER_EDGE_GAP - start.y
304 : floor - ceiling - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
305 };
306 const desiredWidth = start.width + (east ? delta.x : west ? -delta.x : 0);
307 const desiredHeight = start.height + (south ? delta.y : north ? -delta.y : 0);
308 const horizontal = east || west;
309 const vertical = north || south;
310 const aspectRatio = source.width / source.height;
311 // Project corner motion onto the aspect-ratio diagonal. Switching between
312 // dominant axes jumps when one axis grows while the other shrinks.
313 const desired =
314 horizontal && vertical
315 ? (desiredWidth + desiredHeight / aspectRatio) / (1 + 1 / aspectRatio ** 2)
316 : horizontal
317 ? desiredWidth
318 : desiredHeight * aspectRatio;
319 const size = fitPreviewMiniPlayerWidth(desired, source, max);
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 };