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

previewMiniPlayerLayout.ts

318 lines11.0 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/** Android frames include black corner wedges; clip at a matching radius. iOS streams rectangles and
50 * retains the frame radius. */
51export function resolveDeviceMiniPlayerCornerRadius(
52 platform: DevicePlatform,
53 player: PreviewMiniPlayerSize,
54): number {
55 if (platform !== "android") return PREVIEW_MINI_PLAYER_CORNER_RADIUS;
56 return Math.max(
57 PREVIEW_MINI_PLAYER_CORNER_RADIUS,
58 Math.round(Math.min(player.width, player.height) * 0.14),
59 );
61​
62interface HorizontalSpan {
63 readonly left: number;
64 readonly right: number;
66​
67export interface PreviewMiniPlayerObstacles {
68 readonly composer: (HorizontalSpan & { readonly height: number }) | null;
69 readonly detailsCard: (HorizontalSpan & { readonly bottom: number }) | null;
71​
72export const NO_PREVIEW_MINI_PLAYER_OBSTACLES: PreviewMiniPlayerObstacles = {
73 composer: null,
74 detailsCard: null,
75};
76​
77const spanOf = (x: number, width: number): HorizontalSpan => ({ left: x, right: x + width });
78​
79const spansOverlap = (a: HorizontalSpan, b: HorizontalSpan) => a.left < b.right && a.right > b.left;
80​
81function floorFor(
82 span: HorizontalSpan,
83 container: PreviewMiniPlayerSize,
84 obstacles: PreviewMiniPlayerObstacles,
85): number {
86 const { composer } = obstacles;
87 return composer && spansOverlap(span, composer)
88 ? container.height - Math.max(0, composer.height)
89 : container.height;
91​
92function ceilingFor(span: HorizontalSpan, obstacles: PreviewMiniPlayerObstacles): number {
93 const { detailsCard } = obstacles;
94 return detailsCard && spansOverlap(span, detailsCard) ? Math.max(0, detailsCard.bottom) : 0;
96​
97const availableArea = (
98 container: PreviewMiniPlayerSize,
99 obstacles: PreviewMiniPlayerObstacles,
100 span: HorizontalSpan | null,
101): PreviewMiniPlayerSize => ({
102 width: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
103 height:
104 (span
105 ? floorFor(span, container, obstacles) - ceilingFor(span, obstacles)
106 : container.height - Math.max(0, obstacles.composer?.height ?? 0)) -
107 PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
108});
109​
110function fitPreviewMiniPlayerWidth(
111 desiredWidth: number,
112 source: PreviewMiniPlayerSize,
113 max: PreviewMiniPlayerSize,
114): PreviewMiniPlayerSize {
115 const aspectRatio = source.width / source.height;
116 const width = Math.min(
117 Math.max(
118 desiredWidth,
119 PREVIEW_MINI_PLAYER_MIN_SIZE.width,
120 PREVIEW_MINI_PLAYER_MIN_SIZE.height * aspectRatio,
121 ),
122 Math.max(1, max.width),
123 Math.max(1, max.height * aspectRatio),
124 );
125 return { width: Math.round(width), height: Math.round(width / aspectRatio) };
127​
128function defaultPreviewMiniPlayerWidth(source: PreviewMiniPlayerSize): number {
129 return Math.min(
130 PREVIEW_MINI_PLAYER_DEFAULT_BOX.width,
131 (PREVIEW_MINI_PLAYER_DEFAULT_BOX.height * source.width) / source.height,
132 );
134​
135const clampToContainer = (
136 position: PreviewMiniPlayerPosition,
137 container: PreviewMiniPlayerSize,
138 player: PreviewMiniPlayerSize,
139 minimumX = PREVIEW_MINI_PLAYER_EDGE_GAP,
140): PreviewMiniPlayerPosition => ({
141 x: Math.min(
142 Math.max(position.x, minimumX, PREVIEW_MINI_PLAYER_EDGE_GAP),
143 Math.max(
144 PREVIEW_MINI_PLAYER_EDGE_GAP,
145 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
146 ),
147 ),
148 y: Math.min(
149 Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP),
150 Math.max(
151 PREVIEW_MINI_PLAYER_EDGE_GAP,
152 container.height - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP,
153 ),
154 ),
155});
156​
157const overlapsObstacle = (
158 position: PreviewMiniPlayerPosition,
159 player: PreviewMiniPlayerSize,
160 container: PreviewMiniPlayerSize,
161 obstacles: PreviewMiniPlayerObstacles,
162): boolean => {
163 const span = spanOf(position.x, player.width);
164 return (
165 position.y + player.height > floorFor(span, container, obstacles) ||
166 position.y < ceilingFor(span, obstacles)
167 );
168};
169​
170export function clampPreviewMiniPlayerPosition(
171 position: PreviewMiniPlayerPosition,
172 container: PreviewMiniPlayerSize,
173 player: PreviewMiniPlayerSize,
174 obstacles: PreviewMiniPlayerObstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
175 minimumX = PREVIEW_MINI_PLAYER_EDGE_GAP,
176): PreviewMiniPlayerPosition {
177 const inside = clampToContainer(position, container, player, minimumX);
178 if (!overlapsObstacle(inside, player, container, obstacles)) return inside;
179 const gap = PREVIEW_MINI_PLAYER_EDGE_GAP;
180 const fitRows = (x: number): PreviewMiniPlayerPosition => {
181 const span = spanOf(x, player.width);
182 return {
183 x,
184 y: Math.min(
185 container.height - gap - player.height,
186 Math.max(
187 Math.min(inside.y, floorFor(span, container, obstacles) - gap - player.height),
188 ceilingFor(span, obstacles) + gap,
189 gap,
190 ),
191 ),
192 };
193 };
194 const fallback = fitRows(inside.x);
195 const { composer, detailsCard } = obstacles;
196 const span = spanOf(inside.x, player.width);
197 const beside = [composer, detailsCard]
198 .filter(
199 (obstacle): obstacle is NonNullable<typeof obstacle> =>
200 obstacle !== null && spansOverlap(span, obstacle),
201 )
202 .flatMap((obstacle) => [obstacle.left - gap - player.width, obstacle.right + gap]);
203 let best = fallback;
204 let bestDistance = overlapsObstacle(fallback, player, container, obstacles)
205 ? Number.POSITIVE_INFINITY
206 : Math.abs(fallback.y - inside.y);
207 for (const x of beside) {
208 const candidate = fitRows(x);
209 if (
210 clampToContainer(candidate, container, player, minimumX).x !== x ||
211 overlapsObstacle(candidate, player, container, obstacles)
212 ) {
213 continue;
214 }
215 const distance = Math.abs(x - inside.x) + Math.abs(candidate.y - inside.y);
216 if (distance < bestDistance) {
217 best = candidate;
218 bestDistance = distance;
219 }
220 }
221 return best;
223​
224/** Clamp per layout without persisting so narrow containers preserve the chosen width. */
225export function resolvePreviewMiniPlayerFrame(input: {
226 readonly width: number | null;
227 readonly position: PreviewMiniPlayerPosition | null;
228 readonly source: PreviewMiniPlayerSize;
229 readonly container: PreviewMiniPlayerSize;
230 readonly obstacles?: PreviewMiniPlayerObstacles;
231}): PreviewMiniPlayerFrame {
232 const {
233 width,
234 position,
235 source,
236 container,
237 obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
238 } = input;
239 const size = fitPreviewMiniPlayerWidth(
240 width ?? defaultPreviewMiniPlayerWidth(source),
241 source,
242 availableArea(
243 container,
244 position && width ? { composer: obstacles.composer, detailsCard: null } : obstacles,
245 position && width ? spanOf(position.x, width) : null,
246 ),
247 );
248 const { detailsCard } = obstacles;
249 const anchored =
250 position ??
251 (detailsCard
252 ? { x: detailsCard.right - size.width, y: detailsCard.bottom + PREVIEW_MINI_PLAYER_EDGE_GAP }
253 : {
254 x: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - size.width,
255 y: PREVIEW_MINI_PLAYER_EDGE_GAP,
256 });
257 return { ...clampPreviewMiniPlayerPosition(anchored, container, size, obstacles), ...size };
259​
260export function resizePreviewMiniPlayer(input: {
261 readonly start: PreviewMiniPlayerFrame;
262 readonly direction: BrowserViewportResizeDirection;
263 readonly delta: PreviewMiniPlayerPosition;
264 readonly source: PreviewMiniPlayerSize;
265 readonly container: PreviewMiniPlayerSize;
266 readonly obstacles?: PreviewMiniPlayerObstacles;
267}): PreviewMiniPlayerFrame {
268 const {
269 start,
270 direction,
271 delta,
272 source,
273 container,
274 obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES,
275 } = input;
276 const east = direction.includes("east");
277 const west = direction.includes("west");
278 const north = direction.includes("north");
279 const south = direction.includes("south");
280 const right = start.x + start.width;
281 const bottom = start.y + start.height;
282 const span = spanOf(start.x, start.width);
283 const floor = floorFor(span, container, obstacles);
284 const ceiling = ceilingFor(span, obstacles);
285 const max = {
286 width: west
287 ? right - PREVIEW_MINI_PLAYER_EDGE_GAP
288 : east
289 ? container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - start.x
290 : container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
291 height: north
292 ? bottom - ceiling - PREVIEW_MINI_PLAYER_EDGE_GAP
293 : south
294 ? floor - PREVIEW_MINI_PLAYER_EDGE_GAP - start.y
295 : floor - ceiling - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
296 };
297 const desiredWidth = start.width + (east ? delta.x : west ? -delta.x : 0);
298 const desiredHeight = start.height + (south ? delta.y : north ? -delta.y : 0);
299 const horizontal = east || west;
300 const vertical = north || south;
301 const aspectRatio = source.width / source.height;
302 // Project onto the aspect-ratio diagonal; dominant-axis switching jumps when axes move in opposite
303 // directions.
304 const desired =
305 horizontal && vertical
306 ? (desiredWidth + desiredHeight / aspectRatio) / (1 + 1 / aspectRatio ** 2)
307 : horizontal
308 ? desiredWidth
309 : desiredHeight * aspectRatio;
310 const size = fitPreviewMiniPlayerWidth(desired, source, max);
311 const position = clampPreviewMiniPlayerPosition(
312 { x: west ? right - size.width : start.x, y: north ? bottom - size.height : start.y },
313 container,
314 size,
315 obstacles,
316 );
317 return { ...position, ...size };