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

previewMiniPlayerLayout.ts

69 lines2.8 KB

1import type { PreviewMiniPlayerPosition, PreviewMiniPlayerSize } from "~/previewMiniPlayerStore";
2​
3export const PREVIEW_MINI_PLAYER_EDGE_GAP = 12;
4// The mini-player shell straddles this webview at 47 and 49; dialogs begin at 50.
5export const PREVIEW_MINI_PLAYER_WEBVIEW_Z_INDEX = 48;
6export const PREVIEW_MINI_PLAYER_DEFAULT_SIZE = { width: 320, height: 200 } as const;
7export const PREVIEW_MINI_PLAYER_MIN_SIZE = { width: 240, height: 150 } as const;
8​
9export function clampPreviewMiniPlayerSize(
10 size: PreviewMiniPlayerSize,
11 container: PreviewMiniPlayerSize,
12 bottomInset = 0,
13): PreviewMiniPlayerSize {
14 const availableWidth = Math.max(1, container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2);
15 const availableHeight = Math.max(
16 1,
17 container.height - Math.max(0, bottomInset) - PREVIEW_MINI_PLAYER_EDGE_GAP * 2,
18 );
19 return {
20 width: Math.round(
21 Math.min(Math.max(PREVIEW_MINI_PLAYER_MIN_SIZE.width, size.width), availableWidth),
22 ),
23 height: Math.round(
24 Math.min(Math.max(PREVIEW_MINI_PLAYER_MIN_SIZE.height, size.height), availableHeight),
25 ),
26 };
28​
29/**
30 * Default spot for a freshly opened mini player. Normally the top-right CSS
31 * placement stands (`fallback`), but when the inline thread-details panel is
32 * open its card occupies that corner — slide the player underneath the card,
33 * right edges aligned, so neither surface covers the other. The result still
34 * goes through `clampPreviewMiniPlayerPosition`, which pulls the player back
35 * up when a tall card would push it into the composer.
36 */
37export function defaultPreviewMiniPlayerPosition(input: {
38 readonly fallback: PreviewMiniPlayerPosition;
39 readonly parentRect: { readonly left: number; readonly top: number };
40 readonly playerWidth: number;
41 readonly detailsCardRect: { readonly right: number; readonly bottom: number } | null;
42}): PreviewMiniPlayerPosition {
43 if (input.detailsCardRect === null) return input.fallback;
44 return {
45 x: input.detailsCardRect.right - input.parentRect.left - input.playerWidth,
46 y: input.detailsCardRect.bottom - input.parentRect.top + PREVIEW_MINI_PLAYER_EDGE_GAP,
47 };
49​
50export function clampPreviewMiniPlayerPosition(
51 position: PreviewMiniPlayerPosition,
52 container: PreviewMiniPlayerSize,
53 player: PreviewMiniPlayerSize,
54 bottomInset = 0,
55): PreviewMiniPlayerPosition {
56 const reservedBottomSpace = Math.max(0, bottomInset);
57 const maxX = Math.max(
58 PREVIEW_MINI_PLAYER_EDGE_GAP,
59 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
60 );
61 const maxY = Math.max(
62 PREVIEW_MINI_PLAYER_EDGE_GAP,
63 container.height - reservedBottomSpace - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP,
64 );
65 return {
66 x: Math.min(Math.max(position.x, PREVIEW_MINI_PLAYER_EDGE_GAP), maxX),
67 y: Math.min(Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP), maxY),
68 };