mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
fix(audio): keep device menus within viewport
This commit is contained in:
1 parent
1951884d12
commit
fbcbe994f0
5 files changed
+88
-12
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { placeAudioDeviceMenu } from "./audio-device-menu-placement.js";
|
||||
|
||||
test("device menu opens below the trigger and keeps the 240px cap", () => {
|
||||
assert.deepEqual(
|
||||
placeAudioDeviceMenu({ top: 80, bottom: 120, left: 40, width: 280 }, { width: 1280, height: 720 }, 20),
|
||||
{ top: 124, left: 40, width: 280, maxHeight: 240 },
|
||||
);
|
||||
});
|
||||
|
||||
test("device menu opens above when the bottom edge cannot fit the list", () => {
|
||||
assert.deepEqual(
|
||||
placeAudioDeviceMenu({ top: 600, bottom: 640, left: 80, width: 300 }, { width: 1280, height: 700 }, 20),
|
||||
{ top: 356, left: 80, width: 300, maxHeight: 240 },
|
||||
);
|
||||
});
|
||||
|
||||
test("device menu shrinks to the remaining space in a short viewport", () => {
|
||||
const placement = placeAudioDeviceMenu(
|
||||
{ top: 40, bottom: 60, left: 8, width: 180 },
|
||||
{ width: 320, height: 84 },
|
||||
8,
|
||||
);
|
||||
|
||||
assert.deepEqual(placement, { top: 8, left: 8, width: 180, maxHeight: 28 });
|
||||
assert.ok(placement.top + placement.maxHeight <= 84 - 8);
|
||||
});
|
||||
|
||||
test("device menu width stays inside narrow viewport bounds", () => {
|
||||
assert.deepEqual(
|
||||
placeAudioDeviceMenu({ top: 20, bottom: 40, left: 120, width: 220 }, { width: 200, height: 400 }, 4),
|
||||
{ top: 44, left: 8, width: 184, maxHeight: 136 },
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
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 OPTION_HEIGHT = 32;
|
||||
const MENU_PADDING = 8;
|
||||
const MAX_MENU_HEIGHT = 240;
|
||||
|
||||
export function placeAudioDeviceMenu(
|
||||
anchor: AudioDeviceMenuAnchor,
|
||||
viewport: { width: number; height: number },
|
||||
optionCount: number,
|
||||
): AudioDeviceMenuPlacement {
|
||||
const insetX = Math.min(VIEWPORT_INSET, Math.max(0, viewport.width / 2));
|
||||
const width = Math.min(Math.max(0, anchor.width), Math.max(0, viewport.width - insetX * 2));
|
||||
const left = Math.max(insetX, Math.min(anchor.left, viewport.width - insetX - width));
|
||||
const fullHeight = Math.min(MAX_MENU_HEIGHT, Math.max(0, optionCount) * OPTION_HEIGHT + MENU_PADDING);
|
||||
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 };
|
||||
}
|
||||
Reference in new issue
Block a user