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

previewMiniPlayerLayout.ts

48 lines1.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​
29export function clampPreviewMiniPlayerPosition(
30 position: PreviewMiniPlayerPosition,
31 container: PreviewMiniPlayerSize,
32 player: PreviewMiniPlayerSize,
33 bottomInset = 0,
34): PreviewMiniPlayerPosition {
35 const reservedBottomSpace = Math.max(0, bottomInset);
36 const maxX = Math.max(
37 PREVIEW_MINI_PLAYER_EDGE_GAP,
38 container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP,
39 );
40 const maxY = Math.max(
41 PREVIEW_MINI_PLAYER_EDGE_GAP,
42 container.height - reservedBottomSpace - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP,
43 );
44 return {
45 x: Math.min(Math.max(position.x, PREVIEW_MINI_PLAYER_EDGE_GAP), maxX),
46 y: Math.min(Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP), maxY),
47 };