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

previewMiniPlayerLayout.ts

182 lines7.0 KB

1import type { 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​
10export const PREVIEW_MINI_PLAYER_EDGE_GAP = 12;
11// The mini-player shell straddles this webview at 47 and 49; dialogs begin at 50.
12export const PREVIEW_MINI_PLAYER_WEBVIEW_Z_INDEX = 48;
13// A fresh player is the largest box at the source aspect ratio that fits here.
14const PREVIEW_MINI_PLAYER_DEFAULT_BOX = { width: 320, height: 320 } as const;
15const PREVIEW_MINI_PLAYER_MIN_SIZE = { width: 240, height: 150 } as const;
16​
17export interface PreviewMiniPlayerFrame extends PreviewMiniPlayerPosition, PreviewMiniPlayerSize {}
18​
19/**
20 * The rendered size of what the floating player mirrors: the device viewport
21 * when one is set, otherwise the size the webview had when it was floated
22 * (`fittedSourceContent`), which the hosted webview keeps as its CSS viewport.
23 */
24export function resolvePreviewMiniPlayerSourceSize(
25 viewport: PreviewViewportSetting,
26 fittedSourceContent: BrowserSurfaceContentPresentation | null,
27 zoomFactor: number,
28): PreviewMiniPlayerSize {
29 const normalizedZoomFactor = Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;
30 const fitted = resolveFittedBrowserViewport(viewport, fittedSourceContent, normalizedZoomFactor);
31 return {
32 width: fitted.width * normalizedZoomFactor,
33 height: fitted.height * normalizedZoomFactor,
34 };
36​
37const availableArea = (
38 container: PreviewMiniPlayerSize,
39 bottomInset: number,
40): PreviewMiniPlayerSize => ({
41 width: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
42 height: container.height - Math.max(0, bottomInset) - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
43});
44​
45/**
46 * Width is the player's only free dimension; height always follows the source
47 * aspect ratio so the webview fills the box without letterboxing. The player
48 * never grows past the source's own size (the guest keeps its CSS viewport, so
49 * going bigger would only upscale), and a tight container wins over the minimum.
50 */
51function fitPreviewMiniPlayerWidth(
52 desiredWidth: number,
53 source: PreviewMiniPlayerSize,
54 max: PreviewMiniPlayerSize,
55): PreviewMiniPlayerSize {
56 const aspectRatio = source.width / source.height;
57 const width = Math.min(
58 Math.max(
59 desiredWidth,
60 PREVIEW_MINI_PLAYER_MIN_SIZE.width,
61 PREVIEW_MINI_PLAYER_MIN_SIZE.height * aspectRatio,
62 ),
63 source.width,
64 Math.max(1, max.width),
65 Math.max(1, max.height * aspectRatio),
66 );
67 return { width: Math.round(width), height: Math.round(width / aspectRatio) };
69​
70function defaultPreviewMiniPlayerWidth(source: PreviewMiniPlayerSize): number {
71 return Math.min(
72 PREVIEW_MINI_PLAYER_DEFAULT_BOX.width,
73 (PREVIEW_MINI_PLAYER_DEFAULT_BOX.height * source.width) / source.height,
74 );
76​
77export function clampPreviewMiniPlayerPosition(
78 position: PreviewMiniPlayerPosition,
79 container: PreviewMiniPlayerSize,
80 player: PreviewMiniPlayerSize,
81 bottomInset = 0,
82): PreviewMiniPlayerPosition {
83 const reservedBottomSpace = Math.max(0, bottomInset);
84 const maxX = Math.max(
85 PREVIEW_MINI_PLAYER_EDGE_GAP,
86 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
87 );
88 const maxY = Math.max(
89 PREVIEW_MINI_PLAYER_EDGE_GAP,
90 container.height - reservedBottomSpace - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP,
91 );
92 return {
93 x: Math.min(Math.max(position.x, PREVIEW_MINI_PLAYER_EDGE_GAP), maxX),
94 y: Math.min(Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP), maxY),
95 };
97​
98/**
99 * Resolves the on-screen frame from the stored width and position. Clamping
100 * happens here on every layout pass instead of being written back to the
101 * store, so a temporarily narrow container never destroys the user's chosen
102 * width. A player without a position sits in the top-right corner.
103 */
104export function resolvePreviewMiniPlayerFrame(input: {
105 readonly width: number | null;
106 readonly position: PreviewMiniPlayerPosition | null;
107 readonly source: PreviewMiniPlayerSize;
108 readonly container: PreviewMiniPlayerSize;
109 readonly bottomInset?: number;
110}): PreviewMiniPlayerFrame {
111 const { width, position, source, container, bottomInset = 0 } = input;
112 const size = fitPreviewMiniPlayerWidth(
113 width ?? defaultPreviewMiniPlayerWidth(source),
114 source,
115 availableArea(container, bottomInset),
116 );
117 const anchored = position ?? {
118 x: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - size.width,
119 y: PREVIEW_MINI_PLAYER_EDGE_GAP,
120 };
121 return { ...clampPreviewMiniPlayerPosition(anchored, container, size, bottomInset), ...size };
123​
124/**
125 * Resizes from any edge or corner while holding the aspect ratio. The edge
126 * opposite the dragged one stays anchored, so growth stops at the container
127 * on that axis and the pointer keeps tracking the grabbed edge. On a plain edge
128 * drag the perpendicular axis may use the whole container, and the player
129 * shifts as needed to stay inside.
130 */
131export function resizePreviewMiniPlayer(input: {
132 readonly start: PreviewMiniPlayerFrame;
133 readonly direction: BrowserViewportResizeDirection;
134 readonly delta: PreviewMiniPlayerPosition;
135 readonly source: PreviewMiniPlayerSize;
136 readonly container: PreviewMiniPlayerSize;
137 readonly bottomInset?: number;
138}): PreviewMiniPlayerFrame {
139 const { start, direction, delta, source, container, bottomInset = 0 } = input;
140 const east = direction.includes("east");
141 const west = direction.includes("west");
142 const north = direction.includes("north");
143 const south = direction.includes("south");
144 const available = availableArea(container, bottomInset);
145 const right = start.x + start.width;
146 const bottom = start.y + start.height;
147 const max = {
148 width: west
149 ? right - PREVIEW_MINI_PLAYER_EDGE_GAP
150 : east
151 ? container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - start.x
152 : available.width,
153 height: north
154 ? bottom - PREVIEW_MINI_PLAYER_EDGE_GAP
155 : south
156 ? container.height - Math.max(0, bottomInset) - PREVIEW_MINI_PLAYER_EDGE_GAP - start.y
157 : available.height,
158 };
159 const desiredWidth = start.width + (east ? delta.x : west ? -delta.x : 0);
160 const desiredHeight = start.height + (south ? delta.y : north ? -delta.y : 0);
161 const horizontal = east || west;
162 const vertical = north || south;
163 const widthLeads =
164 horizontal && !vertical
165 ? true
166 : vertical && !horizontal
167 ? false
168 : Math.abs(desiredWidth - start.width) / start.width >=
169 Math.abs(desiredHeight - start.height) / start.height;
170 const size = fitPreviewMiniPlayerWidth(
171 widthLeads ? desiredWidth : (desiredHeight * source.width) / source.height,
172 source,
173 max,
174 );
175 const position = clampPreviewMiniPlayerPosition(
176 { x: west ? right - size.width : start.x, y: north ? bottom - size.height : start.y },
177 container,
178 size,
179 bottomInset,
180 );
181 return { ...position, ...size };