Files
WebSpeak-client-for-TeamSpeak/web/src/services/audio-device-menu-placement.ts
T
2026-10-17 09:53:27 +08:00

42 lines
1.5 KiB
TypeScript

export interface AudioDeviceMenuAnchor {
top: number;
bottom: number;
left: number;
width: number;
}
export interface AudioDeviceMenuPlacement {
top: number;
left: number;
width: number;
maxHeight: number;
}
const VIEWPORT_INSET = 8;
const MENU_GAP = 4;
const MAX_MENU_HEIGHT = 240;
export function placeAudioDeviceMenu(
anchor: AudioDeviceMenuAnchor,
viewport: { width: number; height: number },
naturalMenuHeight: number,
naturalMenuWidth = anchor.width,
): AudioDeviceMenuPlacement {
const insetX = Math.min(VIEWPORT_INSET, Math.max(0, viewport.width / 2));
const availableWidth = Math.max(0, viewport.width - insetX * 2);
const desiredWidth = Number.isFinite(naturalMenuWidth)
? Math.max(anchor.width, naturalMenuWidth)
: anchor.width;
const width = Math.min(Math.max(0, desiredWidth), availableWidth);
const left = Math.max(insetX, Math.min(anchor.left, viewport.width - insetX - width));
const fullHeight = Math.min(MAX_MENU_HEIGHT, Number.isFinite(naturalMenuHeight) ? Math.max(0, naturalMenuHeight) : 0);
const below = Math.max(0, viewport.height - VIEWPORT_INSET - anchor.bottom - MENU_GAP);
const above = Math.max(0, anchor.top - VIEWPORT_INSET - MENU_GAP);
const opensAbove = below < fullHeight && above > below;
const available = opensAbove ? above : below;
const maxHeight = Math.min(fullHeight, available);
const top = opensAbove ? anchor.top - MENU_GAP - maxHeight : anchor.bottom + MENU_GAP;
return { top, left, width, maxHeight };
}