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

previewMiniPlayerLayout.ts

46 lines1.6 KB

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