fix(audio): keep device menus within viewport

This commit is contained in:
illusia committed 2026-10-11 05:13:13 +08:00
1 parent 1951884d12
commit fbcbe994f0
5 files changed
+88 -12

No files matched your search

@@ -58,6 +58,7 @@
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch, type CSSProperties } from "vue";
import { placeAudioDeviceMenu } from "../../services/audio-device-menu-placement.js";
export interface AudioDeviceOption {
value: string;
@@ -92,19 +93,13 @@ function updateMenuPosition(): void {
const rect = trigger.value?.getBoundingClientRect();
if (!rect || typeof window === "undefined") return;
const fullHeight = Math.min(240, props.options.length * 32 + 8);
const below = Math.max(0, window.innerHeight - rect.bottom - 8);
const above = Math.max(0, rect.top - 8);
const opensAbove = below < fullHeight && above > below;
const available = opensAbove ? above : below;
const height = Math.min(fullHeight, Math.max(80, available));
const top = opensAbove ? Math.max(8, rect.top - height - 4) : Math.min(rect.bottom + 4, window.innerHeight - height - 8);
const placement = placeAudioDeviceMenu(rect, { width: window.innerWidth, height: window.innerHeight }, props.options.length);
menuStyle.value = {
top: `${top}px`,
left: `${rect.left}px`,
width: `${rect.width}px`,
maxHeight: `${height}px`,
top: `${placement.top}px`,
left: `${placement.left}px`,
width: `${placement.width}px`,
maxHeight: `${placement.maxHeight}px`,
};
}
@@ -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 };
}