Open History to see the last commit
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 };
35}
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) };
68}
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 );
75}
76
77/** Place a new player below the inline details card when it owns the top-right corner. */
78export function defaultPreviewMiniPlayerPosition(input: {
79 readonly fallback: PreviewMiniPlayerPosition;
80 readonly parentRect: { readonly left: number; readonly top: number };
81 readonly playerWidth: number;
82 readonly detailsCardRect: { readonly right: number; readonly bottom: number } | null;
83}): PreviewMiniPlayerPosition {
84 if (input.detailsCardRect === null) return input.fallback;
85 return {
86 x: input.detailsCardRect.right - input.parentRect.left - input.playerWidth,
87 y: input.detailsCardRect.bottom - input.parentRect.top + PREVIEW_MINI_PLAYER_EDGE_GAP,
88 };
89}
90
91export function clampPreviewMiniPlayerPosition(
92 position: PreviewMiniPlayerPosition,
93 container: PreviewMiniPlayerSize,
94 player: PreviewMiniPlayerSize,
95 bottomInset = 0,
96): PreviewMiniPlayerPosition {
97 const reservedBottomSpace = Math.max(0, bottomInset);
98 const maxX = Math.max(
99 PREVIEW_MINI_PLAYER_EDGE_GAP,
100 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
101 );
102 const maxY = Math.max(
103 PREVIEW_MINI_PLAYER_EDGE_GAP,
104 container.height - reservedBottomSpace - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP,
105 );
106 return {
107 x: Math.min(Math.max(position.x, PREVIEW_MINI_PLAYER_EDGE_GAP), maxX),
108 y: Math.min(Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP), maxY),
109 };
110}
111
112/**
113 * Resolves the on-screen frame from the stored width and position. Clamping
114 * happens here on every layout pass instead of being written back to the
115 * store, so a temporarily narrow container never destroys the user's chosen
116 * width. A player without a position sits in the top-right corner.
117 */
118export function resolvePreviewMiniPlayerFrame(input: {
119 readonly width: number | null;
120 readonly position: PreviewMiniPlayerPosition | null;
121 readonly source: PreviewMiniPlayerSize;
122 readonly container: PreviewMiniPlayerSize;
123 readonly bottomInset?: number;
124}): PreviewMiniPlayerFrame {
125 const { width, position, source, container, bottomInset = 0 } = input;
126 const size = fitPreviewMiniPlayerWidth(
127 width ?? defaultPreviewMiniPlayerWidth(source),
128 source,
129 availableArea(container, bottomInset),
130 );
131 const anchored = position ?? {
132 x: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - size.width,
133 y: PREVIEW_MINI_PLAYER_EDGE_GAP,
134 };
135 return { ...clampPreviewMiniPlayerPosition(anchored, container, size, bottomInset), ...size };
136}
137
138/**
139 * Resizes from any edge or corner while holding the aspect ratio. The edge
140 * opposite the dragged one stays anchored, so growth stops at the container
141 * on that axis and the pointer keeps tracking the grabbed edge. On a plain edge
142 * drag the perpendicular axis may use the whole container, and the player
143 * shifts as needed to stay inside.
144 */
145export function resizePreviewMiniPlayer(input: {
146 readonly start: PreviewMiniPlayerFrame;
147 readonly direction: BrowserViewportResizeDirection;
148 readonly delta: PreviewMiniPlayerPosition;
149 readonly source: PreviewMiniPlayerSize;
150 readonly container: PreviewMiniPlayerSize;
151 readonly bottomInset?: number;
152}): PreviewMiniPlayerFrame {
153 const { start, direction, delta, source, container, bottomInset = 0 } = input;
154 const east = direction.includes("east");
155 const west = direction.includes("west");
156 const north = direction.includes("north");
157 const south = direction.includes("south");
158 const available = availableArea(container, bottomInset);
159 const right = start.x + start.width;
160 const bottom = start.y + start.height;
161 const max = {
162 width: west
163 ? right - PREVIEW_MINI_PLAYER_EDGE_GAP
164 : east
165 ? container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - start.x
166 : available.width,
167 height: north
168 ? bottom - PREVIEW_MINI_PLAYER_EDGE_GAP
169 : south
170 ? container.height - Math.max(0, bottomInset) - PREVIEW_MINI_PLAYER_EDGE_GAP - start.y
171 : available.height,
172 };
173 const desiredWidth = start.width + (east ? delta.x : west ? -delta.x : 0);
174 const desiredHeight = start.height + (south ? delta.y : north ? -delta.y : 0);
175 const horizontal = east || west;
176 const vertical = north || south;
177 const widthLeads =
178 horizontal && !vertical
179 ? true
180 : vertical && !horizontal
181 ? false
182 : Math.abs(desiredWidth - start.width) / start.width >=
183 Math.abs(desiredHeight - start.height) / start.height;
184 const size = fitPreviewMiniPlayerWidth(
185 widthLeads ? desiredWidth : (desiredHeight * source.width) / source.height,
186 source,
187 max,
188 );
189 const position = clampPreviewMiniPlayerPosition(
190 { x: west ? right - size.width : start.x, y: north ? bottom - size.height : start.y },
191 container,
192 size,
193 bottomInset,
194 );
195 return { ...position, ...size };
196}