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