mirror of
https://github.com/vector-im/element-call.git
synced 2026-11-19 01:32:29 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
186e1bdc9f |
@@ -0,0 +1,610 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { fn, userEvent, waitFor, within, expect } from "storybook/test";
|
||||
import { useEffect, type FC, type JSX, type ReactNode } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import {
|
||||
AudioContextMenu,
|
||||
type AudioContextMenuProps,
|
||||
} from "./AudioContextMenu";
|
||||
import { MediaButtonWithContextMenu } from "./MediaButtonWithContextMenu";
|
||||
import { MicButton } from "../button";
|
||||
import deviceMenuStyles from "./DeviceMenu.module.css";
|
||||
import meterStyles from "./MicrophoneLevelMeter.module.css";
|
||||
import { outlineWidth, WithACallArea } from "./DeviceMenuStoryFixtures";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { MediaDevices } from "../state/MediaDevices";
|
||||
import { globalScope } from "../state/ObservableScope";
|
||||
|
||||
const mediaDevices = new MediaDevices(globalScope, {
|
||||
controlledAudioDevices: false,
|
||||
});
|
||||
|
||||
/** Supplies a microphone, a wavering tone: a story's invented device ids match no hardware. */
|
||||
const WithAMicrophone: FC<{ children: ReactNode }> = ({ children }) => {
|
||||
useEffect(() => {
|
||||
const context = new AudioContext();
|
||||
const microphone = context.createMediaStreamDestination();
|
||||
const tone = context.createOscillator();
|
||||
const loudness = context.createGain();
|
||||
// Wavers between a third and two thirds of full scale, so it reads as live.
|
||||
const swing = context.createOscillator();
|
||||
const depth = context.createGain();
|
||||
loudness.gain.value = 0.25;
|
||||
depth.gain.value = 0.2;
|
||||
swing.frequency.value = 0.6;
|
||||
tone.frequency.value = 220;
|
||||
swing.connect(depth).connect(loudness.gain);
|
||||
tone.connect(loudness).connect(microphone);
|
||||
tone.start();
|
||||
swing.start();
|
||||
|
||||
const devices = navigator.mediaDevices;
|
||||
const openedForReal = devices.getUserMedia.bind(devices);
|
||||
const opened = Promise.resolve(microphone.stream);
|
||||
// A fresh clone each time, so a caller stopping its tracks doesn't end the next.
|
||||
devices.getUserMedia = async (): Promise<MediaStream> =>
|
||||
(await opened).clone();
|
||||
|
||||
return (): void => {
|
||||
devices.getUserMedia = openedForReal;
|
||||
tone.stop();
|
||||
swing.stop();
|
||||
void context.close();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return <>{children}</>;
|
||||
};
|
||||
|
||||
/** The menu as the microphone's button opens it, beside its mute button. */
|
||||
function AudioMenu({
|
||||
enabled = true,
|
||||
onMuteClick,
|
||||
...menu
|
||||
}: AudioContextMenuProps & {
|
||||
enabled?: boolean;
|
||||
onMuteClick?: () => void;
|
||||
}): JSX.Element {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={
|
||||
<MicButton
|
||||
enabled={enabled}
|
||||
onClick={onMuteClick}
|
||||
data-testid="incall_mute"
|
||||
/>
|
||||
}
|
||||
label={t("settings.devices.microphone")}
|
||||
title={t("settings.devices.mic_source")}
|
||||
>
|
||||
<AudioContextMenu {...menu} />
|
||||
</MediaButtonWithContextMenu>
|
||||
);
|
||||
}
|
||||
|
||||
const meta = {
|
||||
component: AudioMenu,
|
||||
decorators: [
|
||||
(Story): JSX.Element => (
|
||||
<MediaDevicesContext value={mediaDevices}>
|
||||
<WithACallArea>
|
||||
<WithAMicrophone>
|
||||
<Story />
|
||||
</WithAMicrophone>
|
||||
</WithACallArea>
|
||||
</MediaDevicesContext>
|
||||
),
|
||||
],
|
||||
} satisfies Meta<typeof AudioMenu>;
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
// By place: Compound's section heading is aria-hidden, as its group carries
|
||||
// the name.
|
||||
const SECTION_HEADING = "[role='group'] > [aria-hidden]";
|
||||
|
||||
export const Default: Story = {
|
||||
args: {
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Option 1" }, id: "1" },
|
||||
{ label: { type: "name", name: "Option 2" }, id: "2" },
|
||||
],
|
||||
selectedOption: "1",
|
||||
// The footer always passes an output list to the audio menu.
|
||||
outputOptions: [
|
||||
{ label: { type: "default", name: "Built-in Output" }, id: "default" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "default",
|
||||
onSelectOutput: fn(),
|
||||
onMuteClick: fn(),
|
||||
onSelect: fn(),
|
||||
},
|
||||
};
|
||||
|
||||
export const AudioMute: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: false,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "2" },
|
||||
],
|
||||
selectedOption: "2",
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
// Both the mute button and the chevron trigger currently share the aria-label "Edit"
|
||||
// (both are TODO placeholders in the component). The mute button is first in the DOM.
|
||||
const muteButton = canvas.getByTestId("incall_mute");
|
||||
await userEvent.click(muteButton);
|
||||
await expect(args.onMuteClick).toHaveBeenCalled();
|
||||
},
|
||||
};
|
||||
|
||||
export const AudioUnmute: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "2" },
|
||||
],
|
||||
selectedOption: "2",
|
||||
},
|
||||
};
|
||||
|
||||
export const SpeakerAndMicrophoneSections: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [
|
||||
{ label: { type: "default", name: "Built-in Output" }, id: "default" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "default",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const headset = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Headset",
|
||||
});
|
||||
await userEvent.click(headset);
|
||||
await expect(args.onSelectOutput).toHaveBeenCalledWith("spk2");
|
||||
|
||||
// A few devices fit, so the list doesn't scroll.
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
await expect(list.scrollHeight).toBe(list.clientHeight);
|
||||
|
||||
// Each section is headed by its own edge-to-edge rule, with no separator.
|
||||
const menu = document.body.querySelector("[role='menu']")!;
|
||||
await expect(
|
||||
document.body.querySelectorAll("[role='separator']"),
|
||||
).toHaveLength(0);
|
||||
const headings =
|
||||
document.body.querySelectorAll<HTMLElement>(SECTION_HEADING);
|
||||
await expect(headings).toHaveLength(2);
|
||||
const frame = menu.getBoundingClientRect();
|
||||
for (const heading of headings) {
|
||||
const rule = heading.querySelector("h3")!;
|
||||
await expect(
|
||||
Number.parseFloat(getComputedStyle(rule).borderBottomWidth),
|
||||
).toBeGreaterThan(0);
|
||||
// Edge to edge, within the frame.
|
||||
const box = rule.getBoundingClientRect();
|
||||
await expect(box.left - frame.left).toBeLessThanOrEqual(2);
|
||||
await expect(frame.right - box.right).toBeLessThanOrEqual(2);
|
||||
}
|
||||
|
||||
// The first device sits further below the rule than from the menu's edge.
|
||||
// Asserted as a relationship, not pixels: the asymmetry is the design.
|
||||
const control = document.body.querySelector("input[type='radio']")!;
|
||||
const ruleBottom = headings[0]
|
||||
.querySelector("h3")!
|
||||
.getBoundingClientRect().bottom;
|
||||
const box = control.getBoundingClientRect();
|
||||
await expect(box.top - ruleBottom).toBeGreaterThan(box.left - frame.left);
|
||||
|
||||
// And a section stands further from the one above than from its own first
|
||||
// device.
|
||||
const groups = document.body.querySelectorAll("[role='group']");
|
||||
const speakers = groups[0].querySelectorAll("input[type='radio']");
|
||||
const lastSpeaker = speakers[speakers.length - 1].getBoundingClientRect();
|
||||
const nextHeading = groups[1]!.querySelector("h3")!.getBoundingClientRect();
|
||||
await expect(nextHeading.top - lastSpeaker.bottom).toBeGreaterThan(
|
||||
box.top - ruleBottom,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
/** A long name wraps rather than widening the menu, which design sets. */
|
||||
export const LongDeviceNameWraps: Story = {
|
||||
args: {
|
||||
...SpeakerAndMicrophoneSections.args,
|
||||
outputOptions: [
|
||||
{
|
||||
label: {
|
||||
type: "name",
|
||||
name: "Logitech BRIO 4K Ultra HD Pro Business Webcam Speakers (046d:085e)",
|
||||
},
|
||||
id: "long",
|
||||
},
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "long",
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const body = within(document.body);
|
||||
const long = await body.findByRole("menuitemradio", { name: /Logitech/ });
|
||||
const short = await body.findByRole("menuitemradio", { name: "Headset" });
|
||||
|
||||
const menu = document.body.querySelector("[role='menu']")!;
|
||||
await expect(Math.round(menu.getBoundingClientRect().width)).toBe(296);
|
||||
await expect(long.getBoundingClientRect().height).toBeGreaterThan(
|
||||
short.getBoundingClientRect().height,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export const OutputCannotBeChosen: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [
|
||||
{ label: { type: "name", name: "Speakers" }, id: "spk1" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "spk1",
|
||||
// No callback: the speakers are listed, but none can be picked.
|
||||
onSelectOutput: undefined,
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const speakers = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Speakers",
|
||||
});
|
||||
await expect(speakers).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
export const OnlyOneDevice: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [{ label: { type: "name", name: "Microphone 1" }, id: "mic1" }],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [{ label: { type: "name", name: "Speakers" }, id: "spk1" }],
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
// Shown, disabled, rather than hidden.
|
||||
const only = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Microphone 1",
|
||||
});
|
||||
await expect(only).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
/** A requested device hasn't arrived: nothing in either section can be picked. */
|
||||
export const SelectionSettling: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [
|
||||
{ label: { type: "name", name: "Speakers" }, id: "spk1" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
|
||||
// selectedOption never changes, so the request stays in flight.
|
||||
await userEvent.click(
|
||||
await menu.findByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
|
||||
await expect(await menu.findByLabelText("Activating…")).toBeVisible();
|
||||
for (const item of menu.getAllByRole("menuitemradio"))
|
||||
await expect(item).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* The ring shows for keyboard focus only. Asserted on the painted outline:
|
||||
* asserting data-focus-source would still pass with the CSS rule deleted.
|
||||
*/
|
||||
export const KeyboardFocusRing: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
const first = await menu.findByRole("menuitemradio", {
|
||||
name: "Microphone 1",
|
||||
});
|
||||
|
||||
// Opened by pointer: no ring, even though Radix has moved focus.
|
||||
await expect(outlineWidth(first)).toBe(0);
|
||||
|
||||
await userEvent.keyboard("{ArrowDown}");
|
||||
const focused = document.activeElement as HTMLElement;
|
||||
await expect(focused).toHaveRole("menuitemradio");
|
||||
await expect(outlineWidth(focused)).toBeGreaterThan(0);
|
||||
|
||||
// And the pointer takes it away again.
|
||||
await userEvent.hover(first);
|
||||
await expect(outlineWidth(document.activeElement as HTMLElement)).toBe(0);
|
||||
},
|
||||
};
|
||||
|
||||
/** The list scrolls; the meter stays at the foot of the microphone section. */
|
||||
export const ManyDevices: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: Array.from({ length: 20 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Microphone ${i + 1}` },
|
||||
id: `mic${i + 1}`,
|
||||
})),
|
||||
selectedOption: "mic1",
|
||||
outputOptions: Array.from({ length: 6 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Speaker ${i + 1}` },
|
||||
id: `spk${i + 1}`,
|
||||
})),
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
|
||||
// The meter's box rather than its level: without a fake microphone (as on
|
||||
// WebKit) the meter shows a message instead of a level.
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
const sticky = await waitFor(() => {
|
||||
const element = document.body.querySelector<HTMLElement>(
|
||||
`.${meterStyles.meter}`,
|
||||
);
|
||||
if (element === null) throw new Error("the meter has not rendered yet");
|
||||
return element;
|
||||
});
|
||||
await expect(list.scrollHeight).toBeGreaterThan(list.clientHeight);
|
||||
|
||||
// Scrolled so the microphone section starts at the top: only there does a
|
||||
// pinned meter differ from one that is simply last.
|
||||
const group = await menu.findByRole("group", { name: "Microphone" });
|
||||
list.scrollTop +=
|
||||
group.getBoundingClientRect().top - list.getBoundingClientRect().top;
|
||||
await expect(list.scrollTop + list.clientHeight).toBeLessThan(
|
||||
list.scrollHeight,
|
||||
);
|
||||
|
||||
const scrollport = list.getBoundingClientRect();
|
||||
const pinned = sticky.getBoundingClientRect();
|
||||
await expect(pinned.bottom).toBeLessThanOrEqual(scrollport.bottom + 1);
|
||||
await expect(pinned.top).toBeGreaterThanOrEqual(scrollport.top - 1);
|
||||
|
||||
// The whole menu is on screen, not bounded by the document.
|
||||
const frame = document.body
|
||||
.querySelector("[role='menu']")!
|
||||
.getBoundingClientRect();
|
||||
await expect(frame.top).toBeGreaterThanOrEqual(0);
|
||||
await expect(frame.bottom).toBeLessThanOrEqual(window.innerHeight + 1);
|
||||
},
|
||||
};
|
||||
|
||||
/** Safari lists no outputs: a disabled, selected Default stands in. */
|
||||
export const OutputNotEnumerated: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [],
|
||||
selectedOutputOption: undefined,
|
||||
onSelectOutput: undefined,
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const speakers = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Default",
|
||||
});
|
||||
await expect(speakers).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
/** The meter's icon shares the radio controls' centre line; only a real browser lays this out. */
|
||||
export const MeterAlignsWithTheDeviceRows: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const menu = document.body.querySelector("[role='menu']")!;
|
||||
const radio = menu.querySelector("input[type='radio']")!;
|
||||
const icon = await waitFor(() => {
|
||||
const element = document.body.querySelector(`.${meterStyles.icon}`);
|
||||
if (element === null) throw new Error("the meter has not rendered yet");
|
||||
return element;
|
||||
});
|
||||
|
||||
// A pixel of slack, for subpixel layout.
|
||||
await expect(Math.abs(centre(icon) - centre(radio))).toBeLessThanOrEqual(1);
|
||||
},
|
||||
};
|
||||
|
||||
/** Inline-axis centre of an element. */
|
||||
function centre(element: Element): number {
|
||||
const box = element.getBoundingClientRect();
|
||||
return box.left + box.width / 2;
|
||||
}
|
||||
|
||||
/** Every row the keyboard reaches is fully visible, not under a heading or the meter. */
|
||||
export const KeyboardReachesEveryDevice: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
// Enough to scroll both ways, so either end can hide a row.
|
||||
options: Array.from({ length: 20 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Microphone ${i + 1}` },
|
||||
id: `mic${i + 1}`,
|
||||
})),
|
||||
selectedOption: "mic1",
|
||||
outputOptions: Array.from({ length: 4 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Speaker ${i + 1}` },
|
||||
id: `spk${i + 1}`,
|
||||
})),
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const meter = await waitFor(() => {
|
||||
const element = document.body.querySelector<HTMLElement>(
|
||||
`.${meterStyles.meter}`,
|
||||
);
|
||||
if (element === null) throw new Error("the meter has not rendered yet");
|
||||
return element;
|
||||
});
|
||||
|
||||
// Drawn over the list: the sticky headings and the meter.
|
||||
const overlays = [
|
||||
meter,
|
||||
...document.body.querySelectorAll<HTMLElement>(SECTION_HEADING),
|
||||
];
|
||||
const items = within(document.body).getAllByRole("menuitemradio");
|
||||
|
||||
// Down to the last device and back up.
|
||||
for (const key of ["{ArrowDown}", "{ArrowUp}"])
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
await userEvent.keyboard(key);
|
||||
const focused = document.activeElement as HTMLElement;
|
||||
await expect(focused).toHaveRole("menuitemradio");
|
||||
// Checked as overlap: a heading only covers rows while its section is on
|
||||
// screen.
|
||||
await expect(overlapping(focused, overlays)).toBeLessThanOrEqual(1);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/** A section's heading stays at the top while the section is in view, and leaves with it. */
|
||||
export const HeadingsStayWhileScrolling: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
enabled: true,
|
||||
options: Array.from({ length: 20 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Microphone ${i + 1}` },
|
||||
id: `mic${i + 1}`,
|
||||
})),
|
||||
selectedOption: "mic1",
|
||||
outputOptions: Array.from({ length: 4 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Speaker ${i + 1}` },
|
||||
id: `spk${i + 1}`,
|
||||
})),
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
const group = await menu.findByRole("group", { name: "Microphone" });
|
||||
const heading = group.querySelector<HTMLElement>(":scope > [aria-hidden]")!;
|
||||
|
||||
// Far enough that the heading is only on screen if it is stuck there.
|
||||
list.scrollTop +=
|
||||
group.getBoundingClientRect().top - list.getBoundingClientRect().top + 80;
|
||||
|
||||
const scrollport = list.getBoundingClientRect();
|
||||
await expect(heading.getBoundingClientRect().top).toBeLessThanOrEqual(
|
||||
scrollport.top + 2,
|
||||
);
|
||||
await expect(heading.getBoundingClientRect().bottom).toBeGreaterThan(
|
||||
scrollport.top,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
/** How far the most overlapping of `overlays` covers `element`, in px. */
|
||||
function overlapping(element: Element, overlays: Element[]): number {
|
||||
const box = element.getBoundingClientRect();
|
||||
return overlays.reduce((worst, overlay) => {
|
||||
const over = overlay.getBoundingClientRect();
|
||||
const shared =
|
||||
Math.min(box.bottom, over.bottom) - Math.max(box.top, over.top);
|
||||
return Math.max(worst, shared);
|
||||
}, 0);
|
||||
}
|
||||
@@ -0,0 +1,537 @@
|
||||
/*
|
||||
Copyright 2023, 2024 New Vector Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
import { axe } from "vitest-axe";
|
||||
import {
|
||||
act,
|
||||
render,
|
||||
screen,
|
||||
within,
|
||||
type RenderResult,
|
||||
} from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { Profiler, type JSX, useState, type ReactNode } from "react";
|
||||
import { TooltipProvider } from "@vector-im/compound-web";
|
||||
|
||||
import {
|
||||
AudioContextMenu,
|
||||
type AudioContextMenuProps,
|
||||
} from "./AudioContextMenu";
|
||||
import { MediaButtonWithContextMenu } from "./MediaButtonWithContextMenu";
|
||||
import { type MenuOptions } from "./DeviceMenu";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { type MediaDevices } from "../state/MediaDevices";
|
||||
import { restoreAudioCapture, stubAudioCapture } from "../utils/test";
|
||||
|
||||
function withProviders(component: ReactNode): JSX.Element {
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<MediaDevicesContext
|
||||
value={{ requestDeviceNames: () => {} } as unknown as MediaDevices}
|
||||
>
|
||||
{component}
|
||||
</MediaDevicesContext>
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
|
||||
/** The menu as the microphone's button opens it. */
|
||||
function AudioMenu(props: AudioContextMenuProps): JSX.Element {
|
||||
return (
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={null}
|
||||
label="Microphone"
|
||||
title="Microphone source"
|
||||
>
|
||||
<AudioContextMenu {...props} />
|
||||
</MediaButtonWithContextMenu>
|
||||
);
|
||||
}
|
||||
|
||||
function renderMenu(component: ReactNode): RenderResult {
|
||||
return render(withProviders(component));
|
||||
}
|
||||
|
||||
async function open(): Promise<void> {
|
||||
await userEvent.click(screen.getByRole("button", { name: "Microphone" }));
|
||||
}
|
||||
|
||||
const mics: MenuOptions[] = [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
];
|
||||
|
||||
const speakers: MenuOptions[] = [
|
||||
{ label: { type: "name", name: "Speakers" }, id: "spk1" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
];
|
||||
|
||||
describe("AudioContextMenu", () => {
|
||||
// Only one test stubs the capture; don't let it leak into the rest.
|
||||
afterEach(restoreAudioCapture);
|
||||
|
||||
test("shows numbered devices correctly", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={[
|
||||
{ label: { type: "number", number: 1 }, id: "mic1" },
|
||||
{ label: { type: "number", number: 2 }, id: "mic2" },
|
||||
]}
|
||||
selectedOption="mic1"
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 1" });
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" });
|
||||
});
|
||||
|
||||
test("calls select callback on menu click", async () => {
|
||||
const onSelect = vi.fn();
|
||||
renderMenu(
|
||||
<AudioMenu options={mics} selectedOption="mic1" onSelect={onSelect} />,
|
||||
);
|
||||
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
|
||||
expect(onSelect).toHaveBeenCalledWith("mic2");
|
||||
});
|
||||
|
||||
test("does not call select callback on already selected menu click", async () => {
|
||||
const onSelect = vi.fn();
|
||||
renderMenu(
|
||||
<AudioMenu options={mics} selectedOption="mic1" onSelect={onSelect} />,
|
||||
);
|
||||
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 1" }),
|
||||
);
|
||||
|
||||
expect(onSelect).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("renders menu spinner until selection updates for the component", async () => {
|
||||
const { promise, resolve } = Promise.withResolvers<void>();
|
||||
const onSelectPressed = vi.fn();
|
||||
const onOptionUpdated = vi.fn();
|
||||
function Wrapper(): JSX.Element {
|
||||
const [selectedOption, setSelectedOption] = useState("mic1");
|
||||
return (
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption={selectedOption}
|
||||
onSelect={(id) => {
|
||||
onSelectPressed();
|
||||
void promise.then(() => {
|
||||
setSelectedOption(id);
|
||||
onOptionUpdated();
|
||||
});
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
renderMenu(<Wrapper />);
|
||||
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
|
||||
expect(onSelectPressed).toHaveBeenCalled();
|
||||
expect(onOptionUpdated).not.toHaveBeenCalled();
|
||||
// Requested but not yet selected, so mic2 is activating
|
||||
screen.getByRole("menuitemradio", {
|
||||
name: "Microphone 2 Activating…",
|
||||
checked: false,
|
||||
});
|
||||
|
||||
// The currently-selected mic1 item should not be activating
|
||||
screen.getByRole("menuitemradio", {
|
||||
name: "Microphone 1",
|
||||
checked: true,
|
||||
});
|
||||
await act(async () => {
|
||||
// resolve the promise that acutally updates the select option.
|
||||
resolve();
|
||||
await promise;
|
||||
});
|
||||
|
||||
expect(onOptionUpdated).toHaveBeenCalled();
|
||||
// Spinner should now be gone since the selection has caught up
|
||||
const mic2ItemAfter = screen.getByRole("menuitemradio", {
|
||||
name: "Microphone 2",
|
||||
});
|
||||
expect(mic2ItemAfter.querySelector(".rotate")).toBeNull();
|
||||
});
|
||||
|
||||
test("marks the selected menu item as checked", async () => {
|
||||
renderMenu(<AudioMenu options={mics} selectedOption="mic2" />);
|
||||
|
||||
await open();
|
||||
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2", checked: true });
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 1", checked: false });
|
||||
});
|
||||
|
||||
test("disables every device while a selection is settling", async () => {
|
||||
const { promise, resolve } = Promise.withResolvers<void>();
|
||||
function Wrapper(): JSX.Element {
|
||||
const [selectedOption, setSelectedOption] = useState("mic1");
|
||||
return (
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption={selectedOption}
|
||||
onSelect={(id) => {
|
||||
void promise.then(() => setSelectedOption(id));
|
||||
}}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
renderMenu(<Wrapper />);
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
|
||||
// In flight: nothing else can be picked, in either section.
|
||||
for (const name of ["Microphone 1", "Speakers", "Headset"]) {
|
||||
expect(screen.getByRole("menuitemradio", { name })).toHaveAttribute(
|
||||
"aria-disabled",
|
||||
"true",
|
||||
);
|
||||
}
|
||||
|
||||
await act(async () => {
|
||||
resolve();
|
||||
await promise;
|
||||
});
|
||||
|
||||
// Settled: selectable again.
|
||||
expect(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 1" }),
|
||||
).not.toHaveAttribute("aria-disabled", "true");
|
||||
expect(
|
||||
screen.getByRole("menuitemradio", { name: "Headset" }),
|
||||
).not.toHaveAttribute("aria-disabled", "true");
|
||||
});
|
||||
|
||||
test("lets go of a device switch that never arrives", async () => {
|
||||
// onSelect never reports back, as when a device is removed mid-switch.
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
await userEvent.keyboard("{Escape}");
|
||||
await open();
|
||||
|
||||
for (const name of ["Microphone 1", "Speakers", "Headset"]) {
|
||||
expect(screen.getByRole("menuitemradio", { name })).not.toHaveAttribute(
|
||||
"aria-disabled",
|
||||
"true",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("lets go of a device switch whose device is unplugged", async () => {
|
||||
const menu = (options: MenuOptions[]): JSX.Element =>
|
||||
withProviders(
|
||||
<AudioMenu
|
||||
options={options}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
const { rerender } = render(menu(mics));
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
|
||||
// The second microphone is unplugged before the switch lands.
|
||||
rerender(menu(mics.slice(0, 1)));
|
||||
|
||||
// Selectable again without closing the menu.
|
||||
expect(
|
||||
screen.getByRole("menuitemradio", { name: "Headset" }),
|
||||
).not.toHaveAttribute("aria-disabled", "true");
|
||||
});
|
||||
|
||||
test("moves the level without re-rendering anything", async () => {
|
||||
// The level is drawn into the DOM, so a moving level commits nothing.
|
||||
const capture = stubAudioCapture();
|
||||
let commits = 0;
|
||||
|
||||
renderMenu(
|
||||
<Profiler
|
||||
id="menu"
|
||||
onRender={(): void => {
|
||||
commits++;
|
||||
}}
|
||||
>
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>
|
||||
</Profiler>,
|
||||
);
|
||||
|
||||
await open();
|
||||
capture.grant();
|
||||
await vi.waitFor(() => expect(capture.contexts).toHaveLength(1));
|
||||
const meter = await screen.findByRole("meter");
|
||||
// The capture's arrival is one render: the idle level swapped for its own.
|
||||
await act(async () => {});
|
||||
const settled = commits;
|
||||
|
||||
// The meter smooths by elapsed time, so hand-driven frames need a clock.
|
||||
let elapsed = performance.now();
|
||||
const clock = vi
|
||||
.spyOn(performance, "now")
|
||||
.mockImplementation(() => (elapsed += 16));
|
||||
|
||||
// One frame per task, as a browser delivers them: one act() would batch them.
|
||||
for (let step = 1; step <= 8; step++) {
|
||||
capture.speak(step / 8);
|
||||
await act(async () => {
|
||||
capture.drawFrames(1);
|
||||
await Promise.resolve();
|
||||
});
|
||||
}
|
||||
clock.mockRestore();
|
||||
|
||||
expect(meter.getAttribute("aria-valuenow")).not.toBe("0");
|
||||
expect(commits - settled).toBe(0);
|
||||
});
|
||||
|
||||
test("marks focus as keyboard-driven only when the keyboard moved it", async () => {
|
||||
const user = userEvent.setup();
|
||||
renderMenu(
|
||||
<AudioMenu options={mics} selectedOption="mic1" onSelect={vi.fn()} />,
|
||||
);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Microphone" }));
|
||||
const list = screen
|
||||
.getByRole("menuitemradio", { name: "Microphone 1" })
|
||||
.closest("[data-focus-source]");
|
||||
|
||||
// The menu focuses whatever the pointer is over, so focus alone says nothing.
|
||||
expect(list).toHaveAttribute("data-focus-source", "pointer");
|
||||
|
||||
await user.keyboard("{ArrowDown}");
|
||||
expect(list).toHaveAttribute("data-focus-source", "keyboard");
|
||||
|
||||
await user.pointer({
|
||||
target: screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
coords: { clientX: 10, clientY: 10 },
|
||||
});
|
||||
expect(list).toHaveAttribute("data-focus-source", "pointer");
|
||||
});
|
||||
|
||||
test("marks the selected device with the accent fill", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu options={mics} selectedOption="mic2" onSelect={vi.fn()} />,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
const selected = screen
|
||||
.getByRole("menuitemradio", { name: "Microphone 2" })
|
||||
.querySelector("input[type=radio]");
|
||||
expect(selected).toBeChecked();
|
||||
// readOnly would paint the selected radio muted.
|
||||
expect(selected).not.toHaveAttribute("readonly");
|
||||
});
|
||||
|
||||
test("the open menu has no accessibility violations", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
const menu = document.querySelector('[role="menu"]');
|
||||
expect(await axe(menu as HTMLElement)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
test("puts the speaker section above the microphone section", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
const speaker = screen.getByRole("menuitemradio", { name: "Speakers" });
|
||||
const microphone = screen.getByRole("menuitemradio", {
|
||||
name: "Microphone 1",
|
||||
});
|
||||
expect(
|
||||
speaker.compareDocumentPosition(microphone) &
|
||||
Node.DOCUMENT_POSITION_FOLLOWING,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
test("lists speaker and microphone sections", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={[
|
||||
{
|
||||
label: { type: "default", name: "Built-in Output" },
|
||||
id: "default",
|
||||
},
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
]}
|
||||
selectedOutputOption="default"
|
||||
onSelectOutput={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
screen.getByRole("menuitemradio", {
|
||||
name: "Default (Built-in Output)",
|
||||
checked: true,
|
||||
});
|
||||
screen.getByRole("menuitemradio", { name: "Headset", checked: false });
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 1", checked: true });
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2", checked: false });
|
||||
});
|
||||
|
||||
test("calls the output select callback on speaker click", async () => {
|
||||
const onSelectOutput = vi.fn();
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={onSelectOutput}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
await userEvent.click(
|
||||
screen.getByRole("menuitemradio", { name: "Headset" }),
|
||||
);
|
||||
|
||||
expect(onSelectOutput).toHaveBeenCalledWith("spk2");
|
||||
});
|
||||
|
||||
test("shows a single device entry disabled", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics.slice(0, 1)}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
// Shown, but not selectable.
|
||||
const only = screen.getByRole("menuitemradio", { name: "Microphone 1" });
|
||||
expect(only).toHaveAttribute("aria-disabled", "true");
|
||||
});
|
||||
|
||||
test("shows a default speaker where the platform lists none", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
// As Safari: no outputs listed.
|
||||
outputOptions={[]}
|
||||
selectedOutputOption={undefined}
|
||||
onSelectOutput={undefined}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
const speakerGroup = screen
|
||||
.getAllByRole("group")
|
||||
.find((group) => group.getAttribute("aria-label") === "Speaker")!;
|
||||
const entries = within(speakerGroup).getAllByRole("menuitemradio");
|
||||
expect(entries).toHaveLength(1);
|
||||
expect(entries[0]).toHaveAccessibleName("Default");
|
||||
expect(entries[0]).toHaveAttribute("aria-disabled", "true");
|
||||
expect(entries[0]).toHaveAttribute("aria-checked", "true");
|
||||
expect(
|
||||
within(entries[0]).getByRole("radio", { hidden: true }),
|
||||
).toBeChecked();
|
||||
});
|
||||
|
||||
test("shows the speaker section disabled when output selection is unsupported", async () => {
|
||||
renderMenu(
|
||||
<AudioMenu
|
||||
options={mics}
|
||||
selectedOption="mic1"
|
||||
onSelect={vi.fn()}
|
||||
outputOptions={speakers}
|
||||
selectedOutputOption="spk1"
|
||||
onSelectOutput={undefined}
|
||||
/>,
|
||||
);
|
||||
|
||||
await open();
|
||||
|
||||
expect(
|
||||
screen.getByRole("menuitemradio", { name: "Speakers" }),
|
||||
).toHaveAttribute("aria-disabled", "true");
|
||||
expect(
|
||||
screen.getByRole("menuitemradio", { name: "Headset" }),
|
||||
).toHaveAttribute("aria-disabled", "true");
|
||||
expect(
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
).not.toHaveAttribute("aria-disabled", "true");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type FC } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import {
|
||||
DeviceMenuList,
|
||||
DeviceMenuSection,
|
||||
MIN_LIST_HEIGHT,
|
||||
type MenuOptions,
|
||||
useCallAreaShare,
|
||||
usePlannedSelection,
|
||||
} from "./DeviceMenu";
|
||||
import { LiveMicrophoneLevelMeter } from "./MicrophoneLevelMeter";
|
||||
|
||||
/** Id of the placeholder "Default" row, shown when the platform lists no outputs. */
|
||||
const DEFAULT_OUTPUT_ID = "default";
|
||||
|
||||
export interface AudioContextMenuProps {
|
||||
options?: MenuOptions[];
|
||||
selectedOption?: string;
|
||||
onSelect?: (id: string) => void;
|
||||
/** Output (speaker) devices. None leaves the speaker section out. */
|
||||
outputOptions?: MenuOptions[];
|
||||
selectedOutputOption?: string;
|
||||
/** Undefined disables the speaker section. */
|
||||
onSelectOutput?: (id: string) => void;
|
||||
}
|
||||
|
||||
/** The microphone's menu: the speakers, then the microphones with a level meter. */
|
||||
export const AudioContextMenu: FC<AudioContextMenuProps> = ({
|
||||
options = [],
|
||||
selectedOption,
|
||||
onSelect,
|
||||
outputOptions,
|
||||
selectedOutputOption,
|
||||
onSelectOutput,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const share = useCallAreaShare();
|
||||
|
||||
// Safari lists no outputs: show a disabled, selected Default rather than an
|
||||
// empty section.
|
||||
const noOutputsListed = outputOptions?.length === 0;
|
||||
const speakerOptions: MenuOptions[] | undefined = noOutputsListed
|
||||
? [{ id: DEFAULT_OUTPUT_ID, label: { type: "default", name: null } }]
|
||||
: outputOptions;
|
||||
const selectedSpeaker = noOutputsListed
|
||||
? DEFAULT_OUTPUT_ID
|
||||
: selectedOutputOption;
|
||||
|
||||
const { requested, plan, settling } = usePlannedSelection({
|
||||
input: { options, selected: selectedOption },
|
||||
output: { options: speakerOptions ?? [], selected: selectedSpeaker },
|
||||
});
|
||||
|
||||
return (
|
||||
<DeviceMenuList maxHeight={Math.max(MIN_LIST_HEIGHT, share.listHeight)}>
|
||||
{speakerOptions && (
|
||||
<DeviceMenuSection
|
||||
title={t("settings.devices.speaker")}
|
||||
options={speakerOptions}
|
||||
selected={selectedSpeaker}
|
||||
onSelect={
|
||||
onSelectOutput &&
|
||||
((id): void => {
|
||||
plan("output", id);
|
||||
onSelectOutput(id);
|
||||
})
|
||||
}
|
||||
numbered={(n) => t("settings.devices.speaker_numbered", { n })}
|
||||
requested={requested("output")}
|
||||
locked={settling}
|
||||
/>
|
||||
)}
|
||||
<DeviceMenuSection
|
||||
title={t("settings.devices.microphone")}
|
||||
options={options}
|
||||
selected={selectedOption}
|
||||
onSelect={
|
||||
onSelect &&
|
||||
((id): void => {
|
||||
plan("input", id);
|
||||
onSelect(id);
|
||||
})
|
||||
}
|
||||
numbered={(n) => t("settings.devices.microphone_numbered", { n })}
|
||||
requested={requested("input")}
|
||||
locked={settling}
|
||||
// Drawn only while the menu is open, so the microphone isn't held all
|
||||
// call.
|
||||
stickyBottomItem={
|
||||
<LiveMicrophoneLevelMeter deviceId={selectedOption} active />
|
||||
}
|
||||
/>
|
||||
</DeviceMenuList>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { expect, fn, userEvent, within } from "storybook/test";
|
||||
import { type JSX, type ReactNode } from "react";
|
||||
|
||||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import { AudioMenuButton } from "./AudioMenuButton";
|
||||
import {
|
||||
type DeviceMenuSnapshot,
|
||||
type OutputMenuSnapshot,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { useStaticViewModel } from "../state/ViewModel";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { MediaDevices } from "../state/MediaDevices";
|
||||
import { globalScope } from "../state/ObservableScope";
|
||||
|
||||
const mediaDevices = new MediaDevices(globalScope, {
|
||||
controlledAudioDevices: false,
|
||||
});
|
||||
|
||||
/** Builds the view model from the story's args, so each field is a control. */
|
||||
function AudioMenuButtonStoryWrapper({
|
||||
output,
|
||||
...device
|
||||
}: DeviceMenuSnapshot & { output: OutputMenuSnapshot }): ReactNode {
|
||||
const deviceVm = useStaticViewModel(device);
|
||||
const outputVm = useStaticViewModel(output);
|
||||
return (
|
||||
<AudioMenuButton
|
||||
vm={{ ...deviceVm, kind: "audio", output: outputVm }}
|
||||
size="lg"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const meta = {
|
||||
component: AudioMenuButtonStoryWrapper,
|
||||
decorators: [
|
||||
(Story): JSX.Element => (
|
||||
<MediaDevicesContext value={mediaDevices}>
|
||||
<Story />
|
||||
</MediaDevicesContext>
|
||||
),
|
||||
],
|
||||
} satisfies Meta<typeof AudioMenuButtonStoryWrapper>;
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
export const Microphone: Story = {
|
||||
args: {
|
||||
enabled: true,
|
||||
busy: false,
|
||||
toggle: fn(),
|
||||
options: [
|
||||
{ id: "mic1", label: { type: "name", name: "Microphone 1" } },
|
||||
{ id: "mic2", label: { type: "name", name: "Microphone 2" } },
|
||||
],
|
||||
selected: "mic1",
|
||||
select: fn(),
|
||||
output: {
|
||||
options: [
|
||||
{
|
||||
id: "default",
|
||||
label: { type: "default", name: "Built-in Output" },
|
||||
},
|
||||
],
|
||||
selected: "default",
|
||||
select: fn(),
|
||||
},
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
await userEvent.click(
|
||||
within(canvasElement).getByRole("switch", { name: "Mute microphone" }),
|
||||
);
|
||||
await expect(args.toggle).toHaveBeenCalled();
|
||||
},
|
||||
};
|
||||
|
||||
/** No microphones to pick and none to mute: a plain, disabled mute button. */
|
||||
export const WithoutDevices: Story = {
|
||||
args: {
|
||||
...Microphone.args,
|
||||
enabled: false,
|
||||
toggle: undefined,
|
||||
options: [],
|
||||
output: { options: [], selected: undefined, select: undefined },
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await expect(
|
||||
canvas.queryByRole("button", { name: "Microphone" }),
|
||||
).not.toBeInTheDocument();
|
||||
await expect(canvas.getByTestId("incall_mute")).toHaveAttribute(
|
||||
"aria-disabled",
|
||||
"true",
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { TooltipProvider } from "@vector-im/compound-web";
|
||||
|
||||
import { AudioMenuButton } from "./AudioMenuButton";
|
||||
import {
|
||||
type AudioMenuViewModel,
|
||||
type DeviceMenuSnapshot,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { createStaticViewModel } from "../state/ViewModel";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { type MediaDevices } from "../state/MediaDevices";
|
||||
|
||||
function audioMenu(device: Partial<DeviceMenuSnapshot>): AudioMenuViewModel {
|
||||
return {
|
||||
...createStaticViewModel<DeviceMenuSnapshot>({
|
||||
enabled: true,
|
||||
busy: false,
|
||||
toggle: vi.fn(),
|
||||
options: [{ id: "mic1", label: { type: "name", name: "Microphone 1" } }],
|
||||
selected: "mic1",
|
||||
select: vi.fn(),
|
||||
...device,
|
||||
}),
|
||||
kind: "audio",
|
||||
output: createStaticViewModel({
|
||||
options: [],
|
||||
selected: undefined,
|
||||
select: undefined,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function renderButton(vm: AudioMenuViewModel): void {
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<MediaDevicesContext
|
||||
value={{ requestDeviceNames: () => {} } as unknown as MediaDevices}
|
||||
>
|
||||
<AudioMenuButton vm={vm} size="lg" />
|
||||
</MediaDevicesContext>
|
||||
</TooltipProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("AudioMenuButton", () => {
|
||||
test.each([
|
||||
[true, "Mute microphone"],
|
||||
[false, "Unmute microphone"],
|
||||
])("labels the mute button by its state (%s)", (enabled, name) => {
|
||||
renderButton(audioMenu({ enabled }));
|
||||
screen.getByRole("switch", { name });
|
||||
});
|
||||
|
||||
test("mutes from the mute button, not the menu", async () => {
|
||||
const toggle = vi.fn();
|
||||
renderButton(audioMenu({ toggle }));
|
||||
await userEvent.click(
|
||||
screen.getByRole("switch", { name: "Mute microphone" }),
|
||||
);
|
||||
expect(toggle).toHaveBeenCalled();
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
});
|
||||
|
||||
test("disables the mute button while busy", () => {
|
||||
renderButton(audioMenu({ busy: true }));
|
||||
const mute = screen.getByRole("switch", { name: "Mute microphone" });
|
||||
expect(mute).toHaveAttribute("aria-disabled", "true");
|
||||
expect(mute).toHaveAttribute("aria-busy", "true");
|
||||
});
|
||||
|
||||
test("opens the microphone's menu", async () => {
|
||||
renderButton(audioMenu({}));
|
||||
await userEvent.click(screen.getByRole("button", { name: "Microphone" }));
|
||||
screen.getByRole("menuitemradio", { name: "Microphone 1", checked: true });
|
||||
});
|
||||
|
||||
test("is a plain mute button without microphones to offer", () => {
|
||||
renderButton(audioMenu({ options: [], toggle: undefined }));
|
||||
expect(screen.queryByRole("button", { name: "Microphone" })).toBeNull();
|
||||
expect(screen.getByTestId("incall_mute")).toHaveAttribute(
|
||||
"aria-disabled",
|
||||
"true",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type FC } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { MicButton } from "../button";
|
||||
import { MediaButtonWithContextMenu } from "./MediaButtonWithContextMenu";
|
||||
import { AudioContextMenu } from "./AudioContextMenu";
|
||||
import { type AudioMenuViewModel } from "./MediaMenuViewModel";
|
||||
import { useBehavior } from "../useBehavior";
|
||||
|
||||
interface Props {
|
||||
vm: AudioMenuViewModel;
|
||||
size: "md" | "lg";
|
||||
}
|
||||
|
||||
/**
|
||||
* The microphone's mute button: with a menu to pick the microphone and the
|
||||
* speaker while there are microphones to offer, a plain mute button otherwise.
|
||||
*/
|
||||
export const AudioMenuButton: FC<Props> = ({ vm, size }) => {
|
||||
const { t } = useTranslation();
|
||||
const enabled = useBehavior(vm.enabled$);
|
||||
const busy = useBehavior(vm.busy$);
|
||||
const toggle = useBehavior(vm.toggle$);
|
||||
const options = useBehavior(vm.options$);
|
||||
const selected = useBehavior(vm.selected$);
|
||||
const select = useBehavior(vm.select$);
|
||||
const outputOptions = useBehavior(vm.output.options$);
|
||||
const selectedOutput = useBehavior(vm.output.selected$);
|
||||
const selectOutput = useBehavior(vm.output.select$);
|
||||
|
||||
if (options.length === 0)
|
||||
return (
|
||||
<MicButton
|
||||
size={size}
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onClick={toggle}
|
||||
disabled={busy || toggle === undefined}
|
||||
data-testid="incall_mute"
|
||||
/>
|
||||
);
|
||||
|
||||
return (
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={
|
||||
<MicButton
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onClick={(e) => {
|
||||
toggle?.();
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}}
|
||||
disabled={busy || toggle === undefined}
|
||||
data-testid="incall_mute"
|
||||
/>
|
||||
}
|
||||
label={t("settings.devices.microphone")}
|
||||
title={t("settings.devices.mic_source")}
|
||||
>
|
||||
<AudioContextMenu
|
||||
options={options}
|
||||
selectedOption={selected}
|
||||
onSelect={select}
|
||||
outputOptions={outputOptions}
|
||||
selectedOutputOption={selectedOutput}
|
||||
onSelectOutput={selectOutput}
|
||||
/>
|
||||
</MediaButtonWithContextMenu>
|
||||
);
|
||||
};
|
||||
@@ -22,7 +22,11 @@ import { MediaDevices } from "../state/MediaDevices";
|
||||
import { globalScope } from "../state/ObservableScope";
|
||||
import { constant } from "../state/Behavior";
|
||||
import { type LayoutMode } from "../state/LayoutSwitchViewModel";
|
||||
import { type MediaMenuSnapshot } from "./MediaMenuViewModel";
|
||||
import {
|
||||
type AudioMenuViewModel,
|
||||
type CameraMenuViewModel,
|
||||
type DeviceMenuSnapshot,
|
||||
} from "./MediaMenuViewModel";
|
||||
|
||||
// consts for tests
|
||||
const reactionIdentifier = "@user:example.com:DEVICE";
|
||||
@@ -35,11 +39,10 @@ const mediaDevices = new MediaDevices(globalScope, {
|
||||
controlledAudioDevices: false,
|
||||
});
|
||||
|
||||
function menu(
|
||||
kind: "audio" | "video",
|
||||
function deviceMenu(
|
||||
device: "Microphone" | "Camera",
|
||||
withDevices: boolean,
|
||||
): MediaMenuSnapshot {
|
||||
const device = kind === "audio" ? "Microphone" : "Camera";
|
||||
): DeviceMenuSnapshot {
|
||||
return {
|
||||
enabled: true,
|
||||
busy: false,
|
||||
@@ -52,32 +55,46 @@ function menu(
|
||||
: [],
|
||||
selected: "1",
|
||||
select: fn(),
|
||||
outputOptions:
|
||||
withDevices && kind === "audio"
|
||||
? [
|
||||
{
|
||||
id: "default",
|
||||
label: { type: "default", name: "Built-in Output" },
|
||||
},
|
||||
]
|
||||
: [],
|
||||
selectedOutput: "default",
|
||||
selectOutput: fn(),
|
||||
backgroundEffect: "none",
|
||||
backgroundEffects:
|
||||
kind === "video"
|
||||
? [
|
||||
{ id: "none", kind: "none" },
|
||||
{ id: "blur", kind: "blur" },
|
||||
]
|
||||
: [],
|
||||
backgroundEffectNotice: undefined,
|
||||
backgroundEffectSettling: false,
|
||||
backgroundImageRefusal: undefined,
|
||||
selectBackgroundEffect: fn(),
|
||||
addBackgroundImage: undefined,
|
||||
removeBackgroundEffect: undefined,
|
||||
cameraTrack: null,
|
||||
};
|
||||
}
|
||||
|
||||
function useAudioMenuVm(withDevices: boolean): AudioMenuViewModel {
|
||||
const device = useStaticViewModel(deviceMenu("Microphone", withDevices));
|
||||
const output = useStaticViewModel({
|
||||
options: withDevices
|
||||
? [
|
||||
{
|
||||
id: "default",
|
||||
label: { type: "default", name: "Built-in Output" } as const,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
selected: "default",
|
||||
select: fn(),
|
||||
});
|
||||
return { ...device, kind: "audio", output };
|
||||
}
|
||||
|
||||
function useCameraMenuVm(withDevices: boolean): CameraMenuViewModel {
|
||||
const device = useStaticViewModel(deviceMenu("Camera", withDevices));
|
||||
const backgroundEffects = useStaticViewModel({
|
||||
selected: "none",
|
||||
available: [
|
||||
{ id: "none", kind: "none" as const },
|
||||
{ id: "blur", kind: "blur" as const },
|
||||
],
|
||||
notice: undefined,
|
||||
settling: false,
|
||||
imageRefusal: undefined,
|
||||
select: fn(),
|
||||
addImage: undefined,
|
||||
remove: undefined,
|
||||
});
|
||||
return {
|
||||
...device,
|
||||
kind: "video",
|
||||
backgroundEffects,
|
||||
cameraTrack$: constant(null),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -85,7 +102,7 @@ function menu(
|
||||
* A wrapper component that is used for:
|
||||
* - exposing the snapshot via props so the storybook documents the snapshot properties (basically unpack them form the vm)
|
||||
* - constructing the layout switch and the menus' view models; the menus'
|
||||
* own states are in the MediaMenuButton stories
|
||||
* own states are in the AudioMenuButton and CameraMenuButton stories
|
||||
* - Add additional react context
|
||||
* The paraeters are all params from the FooterSnapshot,
|
||||
* the Snapshot of the vm, the wrapper will create a mocked vm from it and pass it to the CallFooter.
|
||||
@@ -105,8 +122,8 @@ function CallFooterStoryWrapper({
|
||||
/** Whether the menus have devices to pick, or are plain mute buttons. */
|
||||
withDevices: boolean;
|
||||
}): ReactNode {
|
||||
const audioMenuVm = useStaticViewModel(menu("audio", withDevices));
|
||||
const cameraMenuVm = useStaticViewModel(menu("video", withDevices));
|
||||
const audioMenuVm = useAudioMenuVm(withDevices);
|
||||
const cameraMenuVm = useCameraMenuVm(withDevices);
|
||||
const vm = useStaticViewModel({
|
||||
...vmSnapshot,
|
||||
layoutSwitchVm: layout && { layout$: constant(layout), setLayout },
|
||||
|
||||
@@ -25,8 +25,12 @@ import { type ViewModel } from "../state/ViewModel";
|
||||
import { useBehavior } from "../useBehavior";
|
||||
import { type LayoutSwitchViewModel } from "../state/LayoutSwitchViewModel";
|
||||
import { LayoutSwitch } from "../room/LayoutSwitch";
|
||||
import { type MediaMenuViewModel } from "./MediaMenuViewModel";
|
||||
import { MediaMenuButton } from "./MediaMenuButton";
|
||||
import {
|
||||
type AudioMenuViewModel,
|
||||
type CameraMenuViewModel,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { AudioMenuButton } from "./AudioMenuButton";
|
||||
import { CameraMenuButton } from "./CameraMenuButton";
|
||||
|
||||
export interface AudioOutputSwitcher {
|
||||
targetOutput: string;
|
||||
@@ -57,8 +61,8 @@ export interface FooterActions {
|
||||
|
||||
// we do not use any ? optional properties so that the vm type is including all fields.
|
||||
export interface FooterState {
|
||||
audioMenuVm: MediaMenuViewModel;
|
||||
cameraMenuVm: MediaMenuViewModel;
|
||||
audioMenuVm: AudioMenuViewModel;
|
||||
cameraMenuVm: CameraMenuViewModel;
|
||||
showFooter: boolean;
|
||||
|
||||
/* This is needed for WindowMode = "flat" */
|
||||
@@ -135,18 +139,8 @@ export const CallFooter: FC<FooterProps> = ({
|
||||
}
|
||||
|
||||
buttons.push(
|
||||
<MediaMenuButton
|
||||
key="audio"
|
||||
kind="audio"
|
||||
vm={audioMenuVm}
|
||||
size={buttonSize}
|
||||
/>,
|
||||
<MediaMenuButton
|
||||
key="video"
|
||||
kind="video"
|
||||
vm={cameraMenuVm}
|
||||
size={buttonSize}
|
||||
/>,
|
||||
<AudioMenuButton key="audio" vm={audioMenuVm} size={buttonSize} />,
|
||||
<CameraMenuButton key="video" vm={cameraMenuVm} size={buttonSize} />,
|
||||
);
|
||||
|
||||
if (toggleScreenSharing !== undefined) {
|
||||
|
||||
@@ -111,7 +111,9 @@ describe("createCallFooterViewModel", () => {
|
||||
const camera = vm.cameraMenuVm$.value;
|
||||
expect(audio.options$.value.length > 0).toBe(offered);
|
||||
expect(camera.options$.value.length > 0).toBe(offered);
|
||||
expect(camera.selectBackgroundEffect$.value !== undefined).toBe(offered);
|
||||
expect(camera.backgroundEffects.select$.value !== undefined).toBe(
|
||||
offered,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -147,7 +149,7 @@ describe("createLobbyFooterViewModel", () => {
|
||||
const camera = vm.cameraMenuVm$.value;
|
||||
expect(vm.audioMenuVm$.value.options$.value).not.toEqual([]);
|
||||
expect(camera.options$.value).not.toEqual([]);
|
||||
expect(camera.selectBackgroundEffect$.value).toBeDefined();
|
||||
expect(camera.backgroundEffects.select$.value).toBeDefined();
|
||||
expect(camera.cameraTrack$.value).toBe(track);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
.notice {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: var(--cpd-space-3x);
|
||||
margin: var(--cpd-space-2x) var(--cpd-space-4x) 0;
|
||||
padding: var(--cpd-space-3x);
|
||||
border-radius: var(--cpd-space-2x);
|
||||
background: var(--cpd-color-bg-subtle-secondary);
|
||||
color: var(--cpd-color-text-primary);
|
||||
font: var(--cpd-font-body-md-regular);
|
||||
}
|
||||
|
||||
.notice > svg {
|
||||
flex-shrink: 0;
|
||||
color: var(--cpd-color-icon-secondary);
|
||||
}
|
||||
|
||||
.refusal {
|
||||
margin: var(--cpd-space-2x) var(--cpd-space-4x) 0;
|
||||
}
|
||||
|
||||
.selfPreview {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
aspect-ratio: 16 / 9;
|
||||
margin-block-end: var(--cpd-space-2x);
|
||||
overflow: clip;
|
||||
border-start-start-radius: var(--cpd-space-3x);
|
||||
border-start-end-radius: var(--cpd-space-3x);
|
||||
background: var(--cpd-color-bg-subtle-secondary);
|
||||
color: var(--cpd-color-icon-secondary);
|
||||
}
|
||||
|
||||
.selfPreview > video {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
inline-size: 100%;
|
||||
block-size: 100%;
|
||||
object-fit: cover;
|
||||
/* Rounded itself too: a video can be drawn on a layer of its own, which the
|
||||
frame's rounded clip doesn't always reach. */
|
||||
border-radius: inherit;
|
||||
}
|
||||
|
||||
/* Up to the menu's frame, over the padding at its top. */
|
||||
.selfPreviewPinned,
|
||||
.listLeadsWithPreview {
|
||||
margin-block-start: calc(-1 * var(--cpd-space-5x));
|
||||
}
|
||||
+43
-597
@@ -6,17 +6,23 @@ Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { fn, userEvent, waitFor, within, expect } from "storybook/test";
|
||||
import { useEffect, useState, type FC, type JSX, type ReactNode } from "react";
|
||||
import { useEffect, useState, type FC, type JSX } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import { MediaMuteAndSwitchButton } from "./MediaMuteAndSwitchButton";
|
||||
import styles from "./MediaMuteAndSwitchButton.module.css";
|
||||
import {
|
||||
CameraContextMenu,
|
||||
type CameraContextMenuProps,
|
||||
} from "./CameraContextMenu";
|
||||
import { MediaButtonWithContextMenu } from "./MediaButtonWithContextMenu";
|
||||
import { VideoButton } from "../button";
|
||||
import styles from "./CameraContextMenu.module.css";
|
||||
import deviceMenuStyles from "./DeviceMenu.module.css";
|
||||
import { type BackgroundEffectGridItem } from "./BackgroundEffectGrid";
|
||||
import { SelfPreview } from "./SelfPreview";
|
||||
import { shippedBackgrounds } from "../tracks/backgroundEffectCatalog";
|
||||
import meterStyles from "./MicrophoneLevelMeter.module.css";
|
||||
import { outlineWidth, WithACallArea } from "./DeviceMenuStoryFixtures";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { RootElementProvider } from "../RootElementContext";
|
||||
import { MediaDevices } from "../state/MediaDevices";
|
||||
import { globalScope } from "../state/ObservableScope";
|
||||
|
||||
@@ -24,43 +30,6 @@ const mediaDevices = new MediaDevices(globalScope, {
|
||||
controlledAudioDevices: false,
|
||||
});
|
||||
|
||||
/** Supplies a microphone, a wavering tone: a story's invented device ids match no hardware. */
|
||||
const WithAMicrophone: FC<{ children: ReactNode }> = ({ children }) => {
|
||||
useEffect(() => {
|
||||
const context = new AudioContext();
|
||||
const microphone = context.createMediaStreamDestination();
|
||||
const tone = context.createOscillator();
|
||||
const loudness = context.createGain();
|
||||
// Wavers between a third and two thirds of full scale, so it reads as live.
|
||||
const swing = context.createOscillator();
|
||||
const depth = context.createGain();
|
||||
loudness.gain.value = 0.25;
|
||||
depth.gain.value = 0.2;
|
||||
swing.frequency.value = 0.6;
|
||||
tone.frequency.value = 220;
|
||||
swing.connect(depth).connect(loudness.gain);
|
||||
tone.connect(loudness).connect(microphone);
|
||||
tone.start();
|
||||
swing.start();
|
||||
|
||||
const devices = navigator.mediaDevices;
|
||||
const openedForReal = devices.getUserMedia.bind(devices);
|
||||
const opened = Promise.resolve(microphone.stream);
|
||||
// A fresh clone each time, so a caller stopping its tracks doesn't end the next.
|
||||
devices.getUserMedia = async (): Promise<MediaStream> =>
|
||||
(await opened).clone();
|
||||
|
||||
return (): void => {
|
||||
devices.getUserMedia = openedForReal;
|
||||
tone.stop();
|
||||
swing.stop();
|
||||
void context.close();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return <>{children}</>;
|
||||
};
|
||||
|
||||
/** A camera for the preview: a figure against a room, drawn to a stream. */
|
||||
const FakeCamera: FC = () => {
|
||||
const [stream, setStream] = useState<MediaStream>();
|
||||
@@ -105,112 +74,51 @@ const FakeCamera: FC = () => {
|
||||
);
|
||||
};
|
||||
|
||||
/** Supplies a call-sized root. Without one the list is bounded by the whole Storybook frame. */
|
||||
const WithACallArea: FC<{ children: ReactNode; blockSize?: number }> = ({
|
||||
children,
|
||||
// A call's size: anything smaller makes a short device list scroll, and a
|
||||
// narrower one narrows the menu.
|
||||
blockSize = 720,
|
||||
}) => {
|
||||
const [callArea, setCallArea] = useState<HTMLElement | null>(null);
|
||||
/** The menu as the camera's button opens it, beside its mute button. */
|
||||
function CameraMenu({
|
||||
enabled = true,
|
||||
onMuteClick,
|
||||
...menu
|
||||
}: CameraContextMenuProps & {
|
||||
enabled?: boolean;
|
||||
onMuteClick?: () => void;
|
||||
}): JSX.Element {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div
|
||||
ref={setCallArea}
|
||||
style={{
|
||||
blockSize,
|
||||
inlineSize: 1024,
|
||||
display: "flex",
|
||||
alignItems: "flex-end",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={
|
||||
<VideoButton
|
||||
enabled={enabled}
|
||||
onClick={onMuteClick}
|
||||
data-testid="incall_videomute"
|
||||
/>
|
||||
}
|
||||
label={t("settings.devices.camera")}
|
||||
title={t("settings.devices.camera_source")}
|
||||
>
|
||||
{callArea !== null && (
|
||||
<RootElementProvider value={callArea}>{children}</RootElementProvider>
|
||||
)}
|
||||
</div>
|
||||
<CameraContextMenu {...menu} />
|
||||
</MediaButtonWithContextMenu>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
const meta = {
|
||||
component: MediaMuteAndSwitchButton,
|
||||
component: CameraMenu,
|
||||
decorators: [
|
||||
(Story): JSX.Element => (
|
||||
<MediaDevicesContext value={mediaDevices}>
|
||||
<WithACallArea>
|
||||
<WithAMicrophone>
|
||||
<Story />
|
||||
</WithAMicrophone>
|
||||
<Story />
|
||||
</WithACallArea>
|
||||
</MediaDevicesContext>
|
||||
),
|
||||
],
|
||||
} satisfies Meta<typeof MediaMuteAndSwitchButton>;
|
||||
} satisfies Meta<typeof CameraMenu>;
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
// By place: Compound's section heading is aria-hidden, as its group carries
|
||||
// the name.
|
||||
const SECTION_HEADING = "[role='group'] > [aria-hidden]";
|
||||
|
||||
export const Default: Story = {
|
||||
args: {
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Option 1" }, id: "1" },
|
||||
{ label: { type: "name", name: "Option 2" }, id: "2" },
|
||||
],
|
||||
selectedOption: "1",
|
||||
// The footer always passes an output list to the audio menu.
|
||||
outputOptions: [
|
||||
{ label: { type: "default", name: "Built-in Output" }, id: "default" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "default",
|
||||
onSelectOutput: fn(),
|
||||
onMuteClick: fn(),
|
||||
onSelect: fn(),
|
||||
},
|
||||
};
|
||||
|
||||
export const AudioMute: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: false,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "2" },
|
||||
],
|
||||
selectedOption: "2",
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
// Both the mute button and the chevron trigger currently share the aria-label "Edit"
|
||||
// (both are TODO placeholders in the component). The mute button is first in the DOM.
|
||||
const muteButton = canvas.getByTestId("incall_mute");
|
||||
await userEvent.click(muteButton);
|
||||
await expect(args.onMuteClick).toHaveBeenCalled();
|
||||
},
|
||||
};
|
||||
|
||||
export const AudioUnmute: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "2" },
|
||||
],
|
||||
selectedOption: "2",
|
||||
},
|
||||
};
|
||||
|
||||
export const VideoMute: Story = {
|
||||
args: {
|
||||
iconsAndLabels: "video",
|
||||
enabled: false,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Camera 1" }, id: "1" },
|
||||
@@ -233,7 +141,6 @@ const effects: BackgroundEffectGridItem[] = [
|
||||
|
||||
export const VideoUnmute: Story = {
|
||||
args: {
|
||||
iconsAndLabels: "video",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Camera 1" }, id: "1" },
|
||||
@@ -404,7 +311,7 @@ export const BackgroundImageRefused: Story = {
|
||||
selectedBackgroundEffect: "blur",
|
||||
backgroundEffectNotice:
|
||||
"Background effects run slowly on this platform, which may cause your video to stutter.",
|
||||
backgroundImageRefusal: { text: "That file is not a supported image" },
|
||||
backgroundImageRefusal: "That file is not a supported image",
|
||||
},
|
||||
decorators: [
|
||||
(Story): JSX.Element => (
|
||||
@@ -419,7 +326,7 @@ export const BackgroundImageRefused: Story = {
|
||||
const menu = within(document.body);
|
||||
const message = await menu.findByText("That file is not a supported image");
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
await expect(list.scrollHeight).toBeGreaterThan(list.clientHeight);
|
||||
const scrollport = list.getBoundingClientRect();
|
||||
@@ -514,7 +421,7 @@ export const RemovingWithLiveSelection: Story = {
|
||||
const [selected, setSelected] = useState("none");
|
||||
const [offered, setOffered] = useState(args.backgroundEffects ?? []);
|
||||
return (
|
||||
<MediaMuteAndSwitchButton
|
||||
<CameraMenu
|
||||
{...args}
|
||||
backgroundEffects={offered}
|
||||
selectedBackgroundEffect={selected}
|
||||
@@ -574,7 +481,7 @@ export const BackgroundEffectsWithPreview: Story = {
|
||||
const picture = await within(document.body).findByTestId("preview-camera");
|
||||
const preview = picture.parentElement!;
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
const frame = document.body
|
||||
.querySelector("[role='menu']")!
|
||||
@@ -640,7 +547,7 @@ export const BackgroundEffectsPreviewScrollsWithTheList: Story = {
|
||||
const picture = await within(document.body).findByTestId("preview-camera");
|
||||
const preview = picture.parentElement!;
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
await expect(list.contains(preview)).toBe(true);
|
||||
const cameras = within(document.body).getByRole("group", {
|
||||
@@ -681,7 +588,7 @@ export const BackgroundEffectsScrollWhenTheyDoNotFit: Story = {
|
||||
name: "Background 2",
|
||||
});
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
`.${deviceMenuStyles.deviceList}`,
|
||||
)!;
|
||||
await expect(list.scrollHeight).toBeGreaterThan(list.clientHeight);
|
||||
await expect(last.getBoundingClientRect().top).toBeGreaterThan(
|
||||
@@ -722,467 +629,6 @@ export const BackgroundEffectsScrollWhenTheyDoNotFit: Story = {
|
||||
},
|
||||
};
|
||||
|
||||
export const SpeakerAndMicrophoneSections: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [
|
||||
{ label: { type: "default", name: "Built-in Output" }, id: "default" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "default",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const headset = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Headset",
|
||||
});
|
||||
await userEvent.click(headset);
|
||||
await expect(args.onSelectOutput).toHaveBeenCalledWith("spk2");
|
||||
|
||||
// A few devices fit, so the list doesn't scroll.
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
)!;
|
||||
await expect(list.scrollHeight).toBe(list.clientHeight);
|
||||
|
||||
// Each section is headed by its own edge-to-edge rule, with no separator.
|
||||
const menu = document.body.querySelector("[role='menu']")!;
|
||||
await expect(
|
||||
document.body.querySelectorAll("[role='separator']"),
|
||||
).toHaveLength(0);
|
||||
const headings =
|
||||
document.body.querySelectorAll<HTMLElement>(SECTION_HEADING);
|
||||
await expect(headings).toHaveLength(2);
|
||||
const frame = menu.getBoundingClientRect();
|
||||
for (const heading of headings) {
|
||||
const rule = heading.querySelector("h3")!;
|
||||
await expect(
|
||||
Number.parseFloat(getComputedStyle(rule).borderBottomWidth),
|
||||
).toBeGreaterThan(0);
|
||||
// Edge to edge, within the frame.
|
||||
const box = rule.getBoundingClientRect();
|
||||
await expect(box.left - frame.left).toBeLessThanOrEqual(2);
|
||||
await expect(frame.right - box.right).toBeLessThanOrEqual(2);
|
||||
}
|
||||
|
||||
// The first device sits further below the rule than from the menu's edge.
|
||||
// Asserted as a relationship, not pixels: the asymmetry is the design.
|
||||
const control = document.body.querySelector("input[type='radio']")!;
|
||||
const ruleBottom = headings[0]
|
||||
.querySelector("h3")!
|
||||
.getBoundingClientRect().bottom;
|
||||
const box = control.getBoundingClientRect();
|
||||
await expect(box.top - ruleBottom).toBeGreaterThan(box.left - frame.left);
|
||||
|
||||
// And a section stands further from the one above than from its own first
|
||||
// device.
|
||||
const groups = document.body.querySelectorAll("[role='group']");
|
||||
const speakers = groups[0].querySelectorAll("input[type='radio']");
|
||||
const lastSpeaker = speakers[speakers.length - 1].getBoundingClientRect();
|
||||
const nextHeading = groups[1]!.querySelector("h3")!.getBoundingClientRect();
|
||||
await expect(nextHeading.top - lastSpeaker.bottom).toBeGreaterThan(
|
||||
box.top - ruleBottom,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
/** A long name wraps rather than widening the menu, which design sets. */
|
||||
export const LongDeviceNameWraps: Story = {
|
||||
args: {
|
||||
...SpeakerAndMicrophoneSections.args,
|
||||
outputOptions: [
|
||||
{
|
||||
label: {
|
||||
type: "name",
|
||||
name: "Logitech BRIO 4K Ultra HD Pro Business Webcam Speakers (046d:085e)",
|
||||
},
|
||||
id: "long",
|
||||
},
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "long",
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const body = within(document.body);
|
||||
const long = await body.findByRole("menuitemradio", { name: /Logitech/ });
|
||||
const short = await body.findByRole("menuitemradio", { name: "Headset" });
|
||||
|
||||
const menu = document.body.querySelector("[role='menu']")!;
|
||||
await expect(Math.round(menu.getBoundingClientRect().width)).toBe(296);
|
||||
await expect(long.getBoundingClientRect().height).toBeGreaterThan(
|
||||
short.getBoundingClientRect().height,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export const OutputCannotBeChosen: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [
|
||||
{ label: { type: "name", name: "Speakers" }, id: "spk1" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "spk1",
|
||||
// No callback: the speakers are listed, but none can be picked.
|
||||
onSelectOutput: undefined,
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const speakers = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Speakers",
|
||||
});
|
||||
await expect(speakers).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
export const OnlyOneDevice: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [{ label: { type: "name", name: "Microphone 1" }, id: "mic1" }],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [{ label: { type: "name", name: "Speakers" }, id: "spk1" }],
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
// Shown, disabled, rather than hidden.
|
||||
const only = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Microphone 1",
|
||||
});
|
||||
await expect(only).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
/** A requested device hasn't arrived: nothing in either section can be picked. */
|
||||
export const SelectionSettling: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [
|
||||
{ label: { type: "name", name: "Speakers" }, id: "spk1" },
|
||||
{ label: { type: "name", name: "Headset" }, id: "spk2" },
|
||||
],
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
|
||||
// selectedOption never changes, so the request stays in flight.
|
||||
await userEvent.click(
|
||||
await menu.findByRole("menuitemradio", { name: "Microphone 2" }),
|
||||
);
|
||||
|
||||
await expect(await menu.findByLabelText("Activating…")).toBeVisible();
|
||||
for (const item of menu.getAllByRole("menuitemradio"))
|
||||
await expect(item).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* The ring shows for keyboard focus only. Asserted on the painted outline:
|
||||
* asserting data-focus-source would still pass with the CSS rule deleted.
|
||||
*/
|
||||
export const KeyboardFocusRing: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
const first = await menu.findByRole("menuitemradio", {
|
||||
name: "Microphone 1",
|
||||
});
|
||||
|
||||
// Opened by pointer: no ring, even though Radix has moved focus.
|
||||
await expect(outlineWidth(first)).toBe(0);
|
||||
|
||||
await userEvent.keyboard("{ArrowDown}");
|
||||
const focused = document.activeElement as HTMLElement;
|
||||
await expect(focused).toHaveRole("menuitemradio");
|
||||
await expect(outlineWidth(focused)).toBeGreaterThan(0);
|
||||
|
||||
// And the pointer takes it away again.
|
||||
await userEvent.hover(first);
|
||||
await expect(outlineWidth(document.activeElement as HTMLElement)).toBe(0);
|
||||
},
|
||||
};
|
||||
|
||||
/** The list scrolls; the meter stays at the foot of the microphone section. */
|
||||
export const ManyDevices: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: Array.from({ length: 20 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Microphone ${i + 1}` },
|
||||
id: `mic${i + 1}`,
|
||||
})),
|
||||
selectedOption: "mic1",
|
||||
outputOptions: Array.from({ length: 6 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Speaker ${i + 1}` },
|
||||
id: `spk${i + 1}`,
|
||||
})),
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
|
||||
// The meter's box rather than its level: without a fake microphone (as on
|
||||
// WebKit) the meter shows a message instead of a level.
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
)!;
|
||||
const sticky = await waitFor(() => {
|
||||
const element = document.body.querySelector<HTMLElement>(
|
||||
`.${meterStyles.meter}`,
|
||||
);
|
||||
if (element === null) throw new Error("the meter has not rendered yet");
|
||||
return element;
|
||||
});
|
||||
await expect(list.scrollHeight).toBeGreaterThan(list.clientHeight);
|
||||
|
||||
// Scrolled so the microphone section starts at the top: only there does a
|
||||
// pinned meter differ from one that is simply last.
|
||||
const group = await menu.findByRole("group", { name: "Microphone" });
|
||||
list.scrollTop +=
|
||||
group.getBoundingClientRect().top - list.getBoundingClientRect().top;
|
||||
await expect(list.scrollTop + list.clientHeight).toBeLessThan(
|
||||
list.scrollHeight,
|
||||
);
|
||||
|
||||
const scrollport = list.getBoundingClientRect();
|
||||
const pinned = sticky.getBoundingClientRect();
|
||||
await expect(pinned.bottom).toBeLessThanOrEqual(scrollport.bottom + 1);
|
||||
await expect(pinned.top).toBeGreaterThanOrEqual(scrollport.top - 1);
|
||||
|
||||
// The whole menu is on screen, not bounded by the document.
|
||||
const frame = document.body
|
||||
.querySelector("[role='menu']")!
|
||||
.getBoundingClientRect();
|
||||
await expect(frame.top).toBeGreaterThanOrEqual(0);
|
||||
await expect(frame.bottom).toBeLessThanOrEqual(window.innerHeight + 1);
|
||||
},
|
||||
};
|
||||
|
||||
/** Painted outline width, in px. */
|
||||
function outlineWidth(element: HTMLElement): number {
|
||||
const { outlineStyle, outlineWidth } = getComputedStyle(element);
|
||||
if (outlineStyle === "none") return 0;
|
||||
return Number.parseFloat(outlineWidth) || 0;
|
||||
}
|
||||
|
||||
/** Safari lists no outputs: a disabled, selected Default stands in. */
|
||||
export const OutputNotEnumerated: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
outputOptions: [],
|
||||
selectedOutputOption: undefined,
|
||||
onSelectOutput: undefined,
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const speakers = await within(document.body).findByRole("menuitemradio", {
|
||||
name: "Default",
|
||||
});
|
||||
await expect(speakers).toHaveAttribute("aria-disabled", "true");
|
||||
},
|
||||
};
|
||||
|
||||
/** The meter's icon shares the radio controls' centre line; only a real browser lays this out. */
|
||||
export const MeterAlignsWithTheDeviceRows: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: [
|
||||
{ label: { type: "name", name: "Microphone 1" }, id: "mic1" },
|
||||
{ label: { type: "name", name: "Microphone 2" }, id: "mic2" },
|
||||
],
|
||||
selectedOption: "mic1",
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const menu = document.body.querySelector("[role='menu']")!;
|
||||
const radio = menu.querySelector("input[type='radio']")!;
|
||||
const icon = await waitFor(() => {
|
||||
const element = document.body.querySelector(`.${meterStyles.icon}`);
|
||||
if (element === null) throw new Error("the meter has not rendered yet");
|
||||
return element;
|
||||
});
|
||||
|
||||
// A pixel of slack, for subpixel layout.
|
||||
await expect(Math.abs(centre(icon) - centre(radio))).toBeLessThanOrEqual(1);
|
||||
},
|
||||
};
|
||||
|
||||
/** Inline-axis centre of an element. */
|
||||
function centre(element: Element): number {
|
||||
const box = element.getBoundingClientRect();
|
||||
return box.left + box.width / 2;
|
||||
}
|
||||
|
||||
/** Every row the keyboard reaches is fully visible, not under a heading or the meter. */
|
||||
export const KeyboardReachesEveryDevice: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
// Enough to scroll both ways, so either end can hide a row.
|
||||
options: Array.from({ length: 20 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Microphone ${i + 1}` },
|
||||
id: `mic${i + 1}`,
|
||||
})),
|
||||
selectedOption: "mic1",
|
||||
outputOptions: Array.from({ length: 4 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Speaker ${i + 1}` },
|
||||
id: `spk${i + 1}`,
|
||||
})),
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
|
||||
const meter = await waitFor(() => {
|
||||
const element = document.body.querySelector<HTMLElement>(
|
||||
`.${meterStyles.meter}`,
|
||||
);
|
||||
if (element === null) throw new Error("the meter has not rendered yet");
|
||||
return element;
|
||||
});
|
||||
|
||||
// Drawn over the list: the sticky headings and the meter.
|
||||
const overlays = [
|
||||
meter,
|
||||
...document.body.querySelectorAll<HTMLElement>(SECTION_HEADING),
|
||||
];
|
||||
const items = within(document.body).getAllByRole("menuitemradio");
|
||||
|
||||
// Down to the last device and back up.
|
||||
for (const key of ["{ArrowDown}", "{ArrowUp}"])
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
await userEvent.keyboard(key);
|
||||
const focused = document.activeElement as HTMLElement;
|
||||
await expect(focused).toHaveRole("menuitemradio");
|
||||
// Checked as overlap: a heading only covers rows while its section is on
|
||||
// screen.
|
||||
await expect(overlapping(focused, overlays)).toBeLessThanOrEqual(1);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/** A section's heading stays at the top while the section is in view, and leaves with it. */
|
||||
export const HeadingsStayWhileScrolling: Story = {
|
||||
args: {
|
||||
...Default.args,
|
||||
iconsAndLabels: "audio",
|
||||
enabled: true,
|
||||
options: Array.from({ length: 20 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Microphone ${i + 1}` },
|
||||
id: `mic${i + 1}`,
|
||||
})),
|
||||
selectedOption: "mic1",
|
||||
outputOptions: Array.from({ length: 4 }, (_, i) => ({
|
||||
label: { type: "name" as const, name: `Speaker ${i + 1}` },
|
||||
id: `spk${i + 1}`,
|
||||
})),
|
||||
selectedOutputOption: "spk1",
|
||||
onSelectOutput: fn(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole("button", { name: "Microphone" }));
|
||||
const menu = within(document.body);
|
||||
|
||||
const list = document.body.querySelector<HTMLElement>(
|
||||
`.${styles.deviceList}`,
|
||||
)!;
|
||||
const group = await menu.findByRole("group", { name: "Microphone" });
|
||||
const heading = group.querySelector<HTMLElement>(":scope > [aria-hidden]")!;
|
||||
|
||||
// Far enough that the heading is only on screen if it is stuck there.
|
||||
list.scrollTop +=
|
||||
group.getBoundingClientRect().top - list.getBoundingClientRect().top + 80;
|
||||
|
||||
const scrollport = list.getBoundingClientRect();
|
||||
await expect(heading.getBoundingClientRect().top).toBeLessThanOrEqual(
|
||||
scrollport.top + 2,
|
||||
);
|
||||
await expect(heading.getBoundingClientRect().bottom).toBeGreaterThan(
|
||||
scrollport.top,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
/** How far the most overlapping of `overlays` covers `element`, in px. */
|
||||
function overlapping(element: Element, overlays: Element[]): number {
|
||||
const box = element.getBoundingClientRect();
|
||||
return overlays.reduce((worst, overlay) => {
|
||||
const over = overlay.getBoundingClientRect();
|
||||
const shared =
|
||||
Math.min(box.bottom, over.bottom) - Math.max(box.top, over.top);
|
||||
return Math.max(worst, shared);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
/** The effect tiles get the keyboard ring the device rows get. */
|
||||
export const FocusRingCoversTheEffectTiles: Story = {
|
||||
args: VideoUnmute.args,
|
||||
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { describe, expect, onTestFinished, test, vi } from "vitest";
|
||||
import {
|
||||
fireEvent,
|
||||
render,
|
||||
screen,
|
||||
within,
|
||||
type RenderResult,
|
||||
} from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { type JSX } from "react";
|
||||
import { TooltipProvider } from "@vector-im/compound-web";
|
||||
|
||||
import {
|
||||
CameraContextMenu,
|
||||
type CameraContextMenuProps,
|
||||
} from "./CameraContextMenu";
|
||||
import { MediaButtonWithContextMenu } from "./MediaButtonWithContextMenu";
|
||||
import { type BackgroundEffectGridItem } from "./BackgroundEffectGrid";
|
||||
import { type MenuOptions } from "./DeviceMenu";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { type MediaDevices } from "../state/MediaDevices";
|
||||
|
||||
const effects: BackgroundEffectGridItem[] = [
|
||||
{ id: "none", kind: "none" },
|
||||
{ id: "blur", kind: "blur" },
|
||||
{ id: "image:arc", kind: "image", imageUrl: "" },
|
||||
{ id: "image:glow", kind: "image", imageUrl: "" },
|
||||
];
|
||||
|
||||
const cameras: MenuOptions[] = [
|
||||
{ label: { type: "name", name: "Camera 1" }, id: "cam1" },
|
||||
];
|
||||
|
||||
/** The menu as the camera's button opens it. */
|
||||
function CameraMenu(props: CameraContextMenuProps): JSX.Element {
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<MediaDevicesContext
|
||||
value={{ requestDeviceNames: () => {} } as unknown as MediaDevices}
|
||||
>
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={null}
|
||||
label="Camera"
|
||||
title="Camera source"
|
||||
>
|
||||
<CameraContextMenu {...props} />
|
||||
</MediaButtonWithContextMenu>
|
||||
</MediaDevicesContext>
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
|
||||
function renderMenu(props: CameraContextMenuProps): RenderResult {
|
||||
return render(<CameraMenu {...props} />);
|
||||
}
|
||||
|
||||
async function open(): Promise<void> {
|
||||
await userEvent.click(screen.getByRole("button", { name: "Camera" }));
|
||||
}
|
||||
|
||||
describe("CameraContextMenu", () => {
|
||||
test("shows numbered devices correctly", async () => {
|
||||
renderMenu({
|
||||
options: [
|
||||
{ label: { type: "number", number: 1 }, id: "cam1" },
|
||||
{ label: { type: "number", number: 2 }, id: "cam2" },
|
||||
],
|
||||
selectedOption: "cam1",
|
||||
});
|
||||
|
||||
await open();
|
||||
screen.getByRole("menuitemradio", { name: "Camera 1" });
|
||||
screen.getByRole("menuitemradio", { name: "Camera 2" });
|
||||
});
|
||||
|
||||
test("uses the same selection pattern as the microphone and keeps the blur toggle", async () => {
|
||||
renderMenu({
|
||||
options: [
|
||||
{ label: { type: "name", name: "Camera 1" }, id: "cam1" },
|
||||
{ label: { type: "name", name: "Camera 2" }, id: "cam2" },
|
||||
],
|
||||
selectedOption: "cam1",
|
||||
onSelect: vi.fn(),
|
||||
backgroundEffects: effects,
|
||||
selectedBackgroundEffect: "none",
|
||||
onSelectBackgroundEffect: vi.fn(),
|
||||
});
|
||||
|
||||
await open();
|
||||
|
||||
screen.getByRole("menuitemradio", { name: "Camera 1", checked: true });
|
||||
screen.getByRole("menuitemradio", { name: "Camera 2", checked: false });
|
||||
within(screen.getByRole("group", { name: "Background effects" })).getByRole(
|
||||
"menuitemradio",
|
||||
{ name: "Blur" },
|
||||
);
|
||||
});
|
||||
|
||||
test("background effects are one keyboard-operable labelled choice", async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSelectBackgroundEffect = vi.fn();
|
||||
renderMenu({
|
||||
options: cameras,
|
||||
selectedOption: "cam1",
|
||||
onSelect: vi.fn(),
|
||||
backgroundEffects: effects,
|
||||
selectedBackgroundEffect: "none",
|
||||
onSelectBackgroundEffect,
|
||||
});
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Camera" }));
|
||||
const section = screen.getByRole("group", { name: "Background effects" });
|
||||
within(section).getByRole("menuitemradio", { name: "None", checked: true });
|
||||
within(section).getByRole("menuitemradio", {
|
||||
name: "Background 1",
|
||||
checked: false,
|
||||
});
|
||||
|
||||
const blur = within(section).getByRole("menuitemradio", { name: "Blur" });
|
||||
for (let i = 0; i < 6 && document.activeElement !== blur; i++)
|
||||
await user.keyboard("{ArrowDown}");
|
||||
expect(document.activeElement).toBe(blur);
|
||||
await user.keyboard("{Enter}");
|
||||
expect(onSelectBackgroundEffect).toHaveBeenCalledWith("blur");
|
||||
expect(screen.getByRole("menu")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("does not choose the effect already in force again", async () => {
|
||||
const onSelectBackgroundEffect = vi.fn();
|
||||
renderMenu({
|
||||
options: cameras,
|
||||
selectedOption: "cam1",
|
||||
backgroundEffects: effects,
|
||||
selectedBackgroundEffect: "blur",
|
||||
onSelectBackgroundEffect,
|
||||
});
|
||||
|
||||
await open();
|
||||
await userEvent.click(screen.getByRole("menuitemradio", { name: "Blur" }));
|
||||
expect(onSelectBackgroundEffect).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("added images are chosen like shipped ones", async () => {
|
||||
const onSelectBackgroundEffect = vi.fn();
|
||||
renderMenu({
|
||||
options: cameras,
|
||||
selectedOption: "cam1",
|
||||
backgroundEffects: [
|
||||
...effects,
|
||||
{ id: "added:mine", kind: "image", imageUrl: "" },
|
||||
],
|
||||
selectedBackgroundEffect: "none",
|
||||
onSelectBackgroundEffect,
|
||||
});
|
||||
|
||||
await open();
|
||||
const section = screen.getByRole("group", { name: "Background effects" });
|
||||
await userEvent.click(
|
||||
within(section).getByRole("menuitemradio", { name: "Background 3" }),
|
||||
);
|
||||
expect(onSelectBackgroundEffect).toHaveBeenCalledWith("added:mine");
|
||||
});
|
||||
|
||||
test("adds the file chosen from the add tile, and stays open", async () => {
|
||||
const user = userEvent.setup();
|
||||
const onAddBackgroundImage = vi.fn();
|
||||
const pick = vi
|
||||
.spyOn(HTMLInputElement.prototype, "click")
|
||||
.mockImplementation(() => {});
|
||||
onTestFinished(() => pick.mockRestore());
|
||||
renderMenu({
|
||||
options: cameras,
|
||||
selectedOption: "cam1",
|
||||
backgroundEffects: effects,
|
||||
selectedBackgroundEffect: "none",
|
||||
onSelectBackgroundEffect: vi.fn(),
|
||||
onAddBackgroundImage,
|
||||
});
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Camera" }));
|
||||
await user.click(screen.getByRole("menuitem", { name: "Add image" }));
|
||||
expect(pick).toHaveBeenCalled();
|
||||
// The picker takes the focus, which would otherwise close the menu.
|
||||
await user.keyboard("{Escape}");
|
||||
expect(screen.getByRole("menu")).toBeInTheDocument();
|
||||
|
||||
const file = new File(["x"], "mine.png", { type: "image/png" });
|
||||
// What the picker hands back; the open menu blocks pointer events.
|
||||
fireEvent.change(
|
||||
document.querySelector<HTMLInputElement>("input[type=file]")!,
|
||||
{ target: { files: [file] } },
|
||||
);
|
||||
expect(onAddBackgroundImage).toHaveBeenCalledWith(file);
|
||||
screen.getByRole("menuitemradio", { name: "None", checked: true });
|
||||
|
||||
// The picker is done, so the menu closes as usual again.
|
||||
await user.keyboard("{Escape}");
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
});
|
||||
|
||||
test("shows a refusal until it is dismissed", async () => {
|
||||
// jsdom lays nothing out, so has nothing to scroll.
|
||||
const scroll = Element.prototype.scrollIntoView;
|
||||
Element.prototype.scrollIntoView = vi.fn();
|
||||
onTestFinished(() => {
|
||||
Element.prototype.scrollIntoView = scroll;
|
||||
});
|
||||
const onDismiss = vi.fn();
|
||||
renderMenu({
|
||||
options: cameras,
|
||||
selectedOption: "cam1",
|
||||
backgroundEffects: effects,
|
||||
selectedBackgroundEffect: "blur",
|
||||
onSelectBackgroundEffect: vi.fn(),
|
||||
onAddBackgroundImage: vi.fn(),
|
||||
backgroundEffectNotice: "Background effects run slowly",
|
||||
backgroundImageRefusal: "That file is not a supported image",
|
||||
onDismissBackgroundImageRefusal: onDismiss,
|
||||
});
|
||||
|
||||
await open();
|
||||
expect(
|
||||
screen.getByText("That file is not a supported image"),
|
||||
).toBeVisible();
|
||||
// In place of the notice, and nothing else changes.
|
||||
expect(screen.queryByText("Background effects run slowly")).toBeNull();
|
||||
screen.getByRole("menuitemradio", { name: "Blur", checked: true });
|
||||
await userEvent.click(screen.getByRole("button", { name: "Close" }));
|
||||
expect(onDismiss).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("offers the background effects in a phone's drawer", async () => {
|
||||
const userAgent = vi
|
||||
.spyOn(navigator, "userAgent", "get")
|
||||
.mockReturnValue("Mozilla/5.0 (Linux; Android 14)");
|
||||
onTestFinished(() => userAgent.mockRestore());
|
||||
const onSelectBackgroundEffect = vi.fn();
|
||||
renderMenu({
|
||||
options: cameras,
|
||||
selectedOption: "cam1",
|
||||
backgroundEffects: effects,
|
||||
selectedBackgroundEffect: "none",
|
||||
onSelectBackgroundEffect,
|
||||
});
|
||||
|
||||
await open();
|
||||
const section = screen.getByRole("group", { name: "Background effects" });
|
||||
// A click alone: the drawer's drag handling reads layout jsdom lacks.
|
||||
fireEvent.click(
|
||||
within(section).getByRole("menuitemradio", { name: "Background 2" }),
|
||||
);
|
||||
expect(onSelectBackgroundEffect).toHaveBeenCalledWith("image:glow");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { useCallback, useId, useRef, type FC, type ReactNode } from "react";
|
||||
import {
|
||||
Alert,
|
||||
InlineSpinner,
|
||||
MenuScrollSection,
|
||||
} from "@vector-im/compound-web";
|
||||
import { InfoIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
|
||||
import classNames from "classnames";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import styles from "./CameraContextMenu.module.css";
|
||||
import {
|
||||
DeviceMenuList,
|
||||
DeviceMenuSection,
|
||||
MIN_LIST_HEIGHT,
|
||||
type MenuOptions,
|
||||
useCallAreaShare,
|
||||
usePlannedSelection,
|
||||
} from "./DeviceMenu";
|
||||
import { useHoldMenuOpen } from "./MediaButtonWithContextMenu";
|
||||
import {
|
||||
BackgroundEffectGrid,
|
||||
type BackgroundEffectGridItem,
|
||||
} from "./BackgroundEffectGrid";
|
||||
|
||||
/** Below the preview, as its stylesheet draws it. */
|
||||
const PREVIEW_GAP = 12;
|
||||
|
||||
export interface CameraContextMenuProps {
|
||||
options?: MenuOptions[];
|
||||
selectedOption?: string;
|
||||
onSelect?: (id: string) => void;
|
||||
/** None or an empty list leaves the section out. */
|
||||
backgroundEffects?: BackgroundEffectGridItem[];
|
||||
selectedBackgroundEffect?: string;
|
||||
/** Undefined where effects can't run: all but no effect are disabled. */
|
||||
onSelectBackgroundEffect?: (id: string) => void;
|
||||
/** What the user should know before choosing an effect, if anything. */
|
||||
backgroundEffectNotice?: string;
|
||||
/** Whether the first effect chosen is still being prepared. */
|
||||
backgroundEffectSettling?: boolean;
|
||||
/** Called with the file chosen from the add tile. Omit to leave it out. */
|
||||
onAddBackgroundImage?: (file: File) => void;
|
||||
/** Removes a removable effect, unless it is the selected one. */
|
||||
onRemoveBackgroundEffect?: (id: string) => void;
|
||||
/** Why the last file offered couldn't be used, if it couldn't. */
|
||||
backgroundImageRefusal?: string;
|
||||
onDismissBackgroundImageRefusal?: () => void;
|
||||
/** The user's own camera, shown at the top. */
|
||||
selfPreview?: ReactNode;
|
||||
}
|
||||
|
||||
/** The camera's menu: a preview, the cameras, then the background effects. */
|
||||
export const CameraContextMenu: FC<CameraContextMenuProps> = ({
|
||||
options = [],
|
||||
selectedOption,
|
||||
onSelect,
|
||||
backgroundEffects = [],
|
||||
selectedBackgroundEffect,
|
||||
onSelectBackgroundEffect,
|
||||
backgroundEffectNotice,
|
||||
backgroundEffectSettling,
|
||||
onAddBackgroundImage,
|
||||
onRemoveBackgroundEffect,
|
||||
backgroundImageRefusal,
|
||||
onDismissBackgroundImageRefusal,
|
||||
selfPreview,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const noticeId = useId();
|
||||
const { requested, plan, settling } = usePlannedSelection({
|
||||
input: { options, selected: selectedOption },
|
||||
});
|
||||
|
||||
const holdOpen = useHoldMenuOpen();
|
||||
const fileInput = useRef<HTMLInputElement | null>(null);
|
||||
const watchFileInput = useCallback(
|
||||
(input: HTMLInputElement | null): (() => void) | undefined => {
|
||||
fileInput.current = input;
|
||||
if (input === null) return;
|
||||
// Dismissing the picker fires cancel, which React doesn't type.
|
||||
const done = (): void => holdOpen(false);
|
||||
input.addEventListener("cancel", done);
|
||||
return (): void => input.removeEventListener("cancel", done);
|
||||
},
|
||||
[holdOpen],
|
||||
);
|
||||
// Scrolled to as it appears, as the list may be scrolled away from it.
|
||||
const scrollIntoView = useCallback((element: HTMLElement | null): void => {
|
||||
element?.scrollIntoView({ block: "nearest" });
|
||||
}, []);
|
||||
|
||||
// Paid for out of the list's share, so a preview makes the menu no taller.
|
||||
const share = useCallAreaShare();
|
||||
const previewBlock = Math.round((share.menuWidth * 9) / 16) + PREVIEW_GAP;
|
||||
const hasPreview = selfPreview !== undefined;
|
||||
const previewPinned =
|
||||
hasPreview && share.listHeight - previewBlock >= MIN_LIST_HEIGHT;
|
||||
const listMaxHeight = Math.max(
|
||||
MIN_LIST_HEIGHT,
|
||||
share.listHeight - (previewPinned ? previewBlock : 0),
|
||||
);
|
||||
const preview = hasPreview && (
|
||||
<div
|
||||
aria-hidden
|
||||
className={classNames(styles.selfPreview, {
|
||||
[styles.selfPreviewPinned]: previewPinned,
|
||||
})}
|
||||
>
|
||||
{backgroundEffectSettling ? <InlineSpinner size={32} /> : selfPreview}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
{onAddBackgroundImage !== undefined && (
|
||||
<input
|
||||
ref={watchFileInput}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
hidden
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
// Cleared, so the same file can be chosen twice in a row.
|
||||
e.target.value = "";
|
||||
holdOpen(false);
|
||||
if (file) onAddBackgroundImage(file);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{previewPinned && preview}
|
||||
<DeviceMenuList
|
||||
maxHeight={listMaxHeight}
|
||||
className={classNames({
|
||||
[styles.listLeadsWithPreview]: hasPreview && !previewPinned,
|
||||
})}
|
||||
>
|
||||
{hasPreview && !previewPinned && preview}
|
||||
<DeviceMenuSection
|
||||
title={t("settings.devices.camera")}
|
||||
options={options}
|
||||
selected={selectedOption}
|
||||
onSelect={
|
||||
onSelect &&
|
||||
((id): void => {
|
||||
plan("input", id);
|
||||
onSelect(id);
|
||||
})
|
||||
}
|
||||
numbered={(n) => t("settings.devices.camera_numbered", { n })}
|
||||
requested={requested("input")}
|
||||
locked={settling}
|
||||
/>
|
||||
{backgroundEffects.length > 0 && (
|
||||
<MenuScrollSection title={t("settings.background_effects_header")}>
|
||||
<BackgroundEffectGrid
|
||||
effects={backgroundEffects}
|
||||
selected={selectedBackgroundEffect}
|
||||
onSelect={onSelectBackgroundEffect}
|
||||
settling={backgroundEffectSettling}
|
||||
describedBy={
|
||||
backgroundEffectNotice === undefined ? undefined : noticeId
|
||||
}
|
||||
onAdd={
|
||||
onAddBackgroundImage === undefined
|
||||
? undefined
|
||||
: (): void => {
|
||||
holdOpen(true);
|
||||
fileInput.current?.click();
|
||||
}
|
||||
}
|
||||
addLabel={t("action.add_background_image")}
|
||||
onRemove={onRemoveBackgroundEffect}
|
||||
removeLabel={t("action.remove")}
|
||||
/>
|
||||
{/* In the list, so the menu grows no taller for it. */}
|
||||
{backgroundImageRefusal !== undefined && (
|
||||
<div ref={scrollIntoView} role="none" className={styles.refusal}>
|
||||
<Alert
|
||||
type="critical"
|
||||
title={backgroundImageRefusal}
|
||||
onClose={onDismissBackgroundImageRefusal}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{backgroundImageRefusal === undefined &&
|
||||
backgroundEffectNotice !== undefined && (
|
||||
<div id={noticeId} role="none" className={styles.notice}>
|
||||
<InfoIcon width={20} height={20} />
|
||||
<span>{backgroundEffectNotice}</span>
|
||||
</div>
|
||||
)}
|
||||
</MenuScrollSection>
|
||||
)}
|
||||
</DeviceMenuList>
|
||||
</>
|
||||
);
|
||||
};
|
||||
+62
-62
@@ -7,10 +7,14 @@ Please see LICENSE in the repository root for full details.
|
||||
|
||||
import { expect, fn, userEvent, within } from "storybook/test";
|
||||
import { type JSX, type ReactNode } from "react";
|
||||
import { type LocalVideoMediaTrack } from "@element-hq/matrixrtc-sdk";
|
||||
|
||||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import { MediaMenuButton } from "./MediaMenuButton";
|
||||
import { type MediaMenuSnapshot } from "./MediaMenuViewModel";
|
||||
import { CameraMenuButton } from "./CameraMenuButton";
|
||||
import {
|
||||
type BackgroundEffectsMenuSnapshot,
|
||||
type DeviceMenuSnapshot,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { useStaticViewModel } from "../state/ViewModel";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { MediaDevices } from "../state/MediaDevices";
|
||||
@@ -20,17 +24,35 @@ const mediaDevices = new MediaDevices(globalScope, {
|
||||
controlledAudioDevices: false,
|
||||
});
|
||||
|
||||
type StoryArgs = DeviceMenuSnapshot & {
|
||||
backgroundEffects: BackgroundEffectsMenuSnapshot;
|
||||
cameraTrack: LocalVideoMediaTrack | null;
|
||||
};
|
||||
|
||||
/** Builds the view model from the story's args, so each field is a control. */
|
||||
function MediaMenuButtonStoryWrapper({
|
||||
kind,
|
||||
...snapshot
|
||||
}: MediaMenuSnapshot & { kind: "audio" | "video" }): ReactNode {
|
||||
const vm = useStaticViewModel(snapshot);
|
||||
return <MediaMenuButton kind={kind} vm={vm} size="lg" />;
|
||||
function CameraMenuButtonStoryWrapper({
|
||||
backgroundEffects,
|
||||
cameraTrack,
|
||||
...device
|
||||
}: StoryArgs): ReactNode {
|
||||
const deviceVm = useStaticViewModel(device);
|
||||
const backgroundEffectsVm = useStaticViewModel(backgroundEffects);
|
||||
const { cameraTrack$ } = useStaticViewModel({ cameraTrack });
|
||||
return (
|
||||
<CameraMenuButton
|
||||
vm={{
|
||||
...deviceVm,
|
||||
kind: "video",
|
||||
backgroundEffects: backgroundEffectsVm,
|
||||
cameraTrack$,
|
||||
}}
|
||||
size="lg"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const meta = {
|
||||
component: MediaMenuButtonStoryWrapper,
|
||||
component: CameraMenuButtonStoryWrapper,
|
||||
decorators: [
|
||||
(Story): JSX.Element => (
|
||||
<MediaDevicesContext value={mediaDevices}>
|
||||
@@ -38,69 +60,41 @@ const meta = {
|
||||
</MediaDevicesContext>
|
||||
),
|
||||
],
|
||||
} satisfies Meta<typeof MediaMenuButtonStoryWrapper>;
|
||||
} satisfies Meta<typeof CameraMenuButtonStoryWrapper>;
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
const empty = {
|
||||
outputOptions: [],
|
||||
selectedOutput: undefined,
|
||||
selectOutput: undefined,
|
||||
backgroundEffect: undefined,
|
||||
backgroundEffects: [],
|
||||
backgroundEffectNotice: undefined,
|
||||
backgroundEffectSettling: false,
|
||||
backgroundImageRefusal: undefined,
|
||||
selectBackgroundEffect: undefined,
|
||||
addBackgroundImage: undefined,
|
||||
removeBackgroundEffect: undefined,
|
||||
cameraTrack: null,
|
||||
} satisfies Partial<MediaMenuSnapshot>;
|
||||
const noBackgroundEffects: BackgroundEffectsMenuSnapshot = {
|
||||
selected: "none",
|
||||
available: [],
|
||||
notice: undefined,
|
||||
settling: false,
|
||||
imageRefusal: undefined,
|
||||
select: undefined,
|
||||
addImage: undefined,
|
||||
remove: undefined,
|
||||
};
|
||||
|
||||
export const Microphone: Story = {
|
||||
args: {
|
||||
...empty,
|
||||
kind: "audio",
|
||||
enabled: true,
|
||||
busy: false,
|
||||
toggle: fn(),
|
||||
options: [
|
||||
{ id: "mic1", label: { type: "name", name: "Microphone 1" } },
|
||||
{ id: "mic2", label: { type: "name", name: "Microphone 2" } },
|
||||
],
|
||||
selected: "mic1",
|
||||
select: fn(),
|
||||
outputOptions: [
|
||||
{ id: "default", label: { type: "default", name: "Built-in Output" } },
|
||||
],
|
||||
selectedOutput: "default",
|
||||
selectOutput: fn(),
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
await userEvent.click(
|
||||
within(canvasElement).getByRole("switch", { name: "Mute microphone" }),
|
||||
);
|
||||
await expect(args.toggle).toHaveBeenCalled();
|
||||
},
|
||||
const cameraEffects: BackgroundEffectsMenuSnapshot = {
|
||||
...noBackgroundEffects,
|
||||
available: [
|
||||
{ id: "none", kind: "none" },
|
||||
{ id: "blur", kind: "blur" },
|
||||
],
|
||||
select: fn(),
|
||||
};
|
||||
|
||||
export const Camera: Story = {
|
||||
args: {
|
||||
...empty,
|
||||
kind: "video",
|
||||
cameraTrack: null,
|
||||
enabled: true,
|
||||
busy: false,
|
||||
toggle: fn(),
|
||||
options: [{ id: "cam1", label: { type: "name", name: "Camera 1" } }],
|
||||
selected: "cam1",
|
||||
select: fn(),
|
||||
backgroundEffect: "none",
|
||||
backgroundEffects: [
|
||||
{ id: "none", kind: "none" },
|
||||
{ id: "blur", kind: "blur" },
|
||||
],
|
||||
selectBackgroundEffect: fn(),
|
||||
backgroundEffects: cameraEffects,
|
||||
},
|
||||
play: async ({ args, canvasElement }) => {
|
||||
await userEvent.click(
|
||||
@@ -128,8 +122,11 @@ async function openEffects(canvasElement: HTMLElement): Promise<HTMLElement> {
|
||||
export const CameraEffectsUnavailable: Story = {
|
||||
args: {
|
||||
...Camera.args,
|
||||
selectBackgroundEffect: undefined,
|
||||
backgroundEffectNotice: "unavailable",
|
||||
backgroundEffects: {
|
||||
...cameraEffects,
|
||||
select: undefined,
|
||||
notice: "unavailable",
|
||||
},
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const section = await openEffects(canvasElement);
|
||||
@@ -144,7 +141,7 @@ export const CameraEffectsUnavailable: Story = {
|
||||
export const CameraEffectsSlow: Story = {
|
||||
args: {
|
||||
...Camera.args,
|
||||
backgroundEffectNotice: "slow",
|
||||
backgroundEffects: { ...cameraEffects, notice: "slow" },
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const section = await openEffects(canvasElement);
|
||||
@@ -159,8 +156,11 @@ export const CameraEffectsSlow: Story = {
|
||||
export const CameraFileRefused: Story = {
|
||||
args: {
|
||||
...Camera.args,
|
||||
addBackgroundImage: fn(),
|
||||
backgroundImageRefusal: { reason: "not-an-image" },
|
||||
backgroundEffects: {
|
||||
...cameraEffects,
|
||||
addImage: fn(),
|
||||
imageRefusal: { reason: "not-an-image" },
|
||||
},
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
await openEffects(canvasElement);
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { describe, expect, onTestFinished, test, vi } from "vitest";
|
||||
import { act, render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { TooltipProvider } from "@vector-im/compound-web";
|
||||
import { type BehaviorSubject } from "rxjs";
|
||||
|
||||
import { CameraMenuButton } from "./CameraMenuButton";
|
||||
import {
|
||||
type BackgroundEffectsMenuSnapshot,
|
||||
type BackgroundImageRefusal,
|
||||
type CameraMenuViewModel,
|
||||
type DeviceMenuSnapshot,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { createStaticViewModel } from "../state/ViewModel";
|
||||
import { constant } from "../state/Behavior";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { type MediaDevices } from "../state/MediaDevices";
|
||||
|
||||
function cameraMenu(
|
||||
device: Partial<DeviceMenuSnapshot> = {},
|
||||
effects: Partial<BackgroundEffectsMenuSnapshot> = {},
|
||||
): CameraMenuViewModel {
|
||||
return {
|
||||
...createStaticViewModel<DeviceMenuSnapshot>({
|
||||
enabled: true,
|
||||
busy: false,
|
||||
toggle: vi.fn(),
|
||||
options: [{ id: "cam1", label: { type: "name", name: "Camera 1" } }],
|
||||
selected: "cam1",
|
||||
select: vi.fn(),
|
||||
...device,
|
||||
}),
|
||||
kind: "video",
|
||||
backgroundEffects: createStaticViewModel<BackgroundEffectsMenuSnapshot>({
|
||||
selected: "none",
|
||||
available: [
|
||||
{ id: "none", kind: "none" },
|
||||
{ id: "blur", kind: "blur" },
|
||||
],
|
||||
notice: undefined,
|
||||
settling: false,
|
||||
imageRefusal: undefined,
|
||||
select: vi.fn(),
|
||||
addImage: vi.fn(),
|
||||
remove: undefined,
|
||||
...effects,
|
||||
}),
|
||||
cameraTrack$: constant(null),
|
||||
};
|
||||
}
|
||||
|
||||
function renderButton(vm: CameraMenuViewModel): void {
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<MediaDevicesContext
|
||||
value={{ requestDeviceNames: () => {} } as unknown as MediaDevices}
|
||||
>
|
||||
<CameraMenuButton vm={vm} size="lg" />
|
||||
</MediaDevicesContext>
|
||||
</TooltipProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
async function open(): Promise<void> {
|
||||
await userEvent.click(screen.getByRole("button", { name: "Camera" }));
|
||||
}
|
||||
|
||||
describe("CameraMenuButton", () => {
|
||||
test.each([
|
||||
[true, "Stop video"],
|
||||
[false, "Start video"],
|
||||
])("labels the mute button by its state (%s)", (enabled, name) => {
|
||||
renderButton(cameraMenu({ enabled }));
|
||||
screen.getByRole("switch", { name });
|
||||
});
|
||||
|
||||
test("disables the mute button while busy", async () => {
|
||||
const toggle = vi.fn();
|
||||
renderButton(cameraMenu({ busy: true, toggle }));
|
||||
const mute = screen.getByRole("switch", { name: "Stop video" });
|
||||
expect(mute).toHaveAttribute("aria-disabled", "true");
|
||||
expect(mute).toHaveAttribute("aria-busy", "true");
|
||||
await userEvent.click(mute);
|
||||
expect(toggle).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("says in words why effects can't run here", async () => {
|
||||
renderButton(cameraMenu({}, { notice: "unavailable", select: undefined }));
|
||||
await open();
|
||||
screen.getByText("Background effects are not supported on this platform.");
|
||||
});
|
||||
|
||||
test("a refusal can be dismissed and does not outlive the menu", async () => {
|
||||
// jsdom lays nothing out, so has nothing to scroll.
|
||||
const scroll = Element.prototype.scrollIntoView;
|
||||
Element.prototype.scrollIntoView = vi.fn();
|
||||
onTestFinished(() => {
|
||||
Element.prototype.scrollIntoView = scroll;
|
||||
});
|
||||
const vm = cameraMenu();
|
||||
const refusal$ = vm.backgroundEffects.imageRefusal$ as BehaviorSubject<
|
||||
BackgroundImageRefusal | undefined
|
||||
>;
|
||||
renderButton(vm);
|
||||
await open();
|
||||
|
||||
act(() => refusal$.next({ reason: "not-an-image" }));
|
||||
expect(
|
||||
screen.getByText("That file is not a supported image"),
|
||||
).toBeVisible();
|
||||
await userEvent.click(screen.getByRole("button", { name: "Close" }));
|
||||
expect(screen.queryByText("That file is not a supported image")).toBeNull();
|
||||
|
||||
// The next refusal is shown, even if it says the same.
|
||||
act(() => refusal$.next({ reason: "not-an-image" }));
|
||||
expect(
|
||||
screen.getByText("That file is not a supported image"),
|
||||
).toBeVisible();
|
||||
|
||||
await userEvent.keyboard("{Escape}");
|
||||
await open();
|
||||
expect(screen.queryByText("That file is not a supported image")).toBeNull();
|
||||
});
|
||||
|
||||
test("is a plain mute button without cameras to offer", () => {
|
||||
renderButton(cameraMenu({ options: [], toggle: undefined }));
|
||||
expect(screen.queryByRole("button", { name: "Camera" })).toBeNull();
|
||||
expect(screen.getByTestId("incall_videomute")).toHaveAttribute(
|
||||
"aria-disabled",
|
||||
"true",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type FC, useState } from "react";
|
||||
import { type TFunction } from "i18next";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { VideoButton } from "../button";
|
||||
import { MediaButtonWithContextMenu } from "./MediaButtonWithContextMenu";
|
||||
import { CameraContextMenu } from "./CameraContextMenu";
|
||||
import {
|
||||
type BackgroundImageRefusal,
|
||||
type CameraMenuViewModel,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { SelfPreview } from "./SelfPreview";
|
||||
import { useBehavior } from "../useBehavior";
|
||||
|
||||
interface Props {
|
||||
vm: CameraMenuViewModel;
|
||||
size: "md" | "lg";
|
||||
}
|
||||
|
||||
/**
|
||||
* The camera's mute button: with a menu to pick the camera and the background
|
||||
* effect while there are cameras to offer, a plain mute button otherwise.
|
||||
*/
|
||||
export const CameraMenuButton: FC<Props> = ({ vm, size }) => {
|
||||
const { t } = useTranslation();
|
||||
const enabled = useBehavior(vm.enabled$);
|
||||
const busy = useBehavior(vm.busy$);
|
||||
const toggle = useBehavior(vm.toggle$);
|
||||
const options = useBehavior(vm.options$);
|
||||
const selected = useBehavior(vm.selected$);
|
||||
const select = useBehavior(vm.select$);
|
||||
const effects = vm.backgroundEffects;
|
||||
const backgroundEffect = useBehavior(effects.selected$);
|
||||
const backgroundEffects = useBehavior(effects.available$);
|
||||
const notice = useBehavior(effects.notice$);
|
||||
const settling = useBehavior(effects.settling$);
|
||||
const refusal = useBehavior(effects.imageRefusal$);
|
||||
const selectBackgroundEffect = useBehavior(effects.select$);
|
||||
const addBackgroundImage = useBehavior(effects.addImage$);
|
||||
const removeBackgroundEffect = useBehavior(effects.remove$);
|
||||
const cameraTrack = useBehavior(vm.cameraTrack$);
|
||||
|
||||
// A refusal is about the attempt that caused it: shown until dismissed or
|
||||
// until the menu closes, and again for the next one.
|
||||
const [dismissedRefusal, setDismissedRefusal] =
|
||||
useState<BackgroundImageRefusal>();
|
||||
const shownRefusal = refusal !== dismissedRefusal ? refusal : undefined;
|
||||
|
||||
if (options.length === 0)
|
||||
return (
|
||||
<VideoButton
|
||||
size={size}
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onClick={toggle}
|
||||
disabled={busy || toggle === undefined}
|
||||
data-testid="incall_videomute"
|
||||
/>
|
||||
);
|
||||
|
||||
return (
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={
|
||||
<VideoButton
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onClick={(e) => {
|
||||
toggle?.();
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}}
|
||||
disabled={busy || toggle === undefined}
|
||||
data-testid="incall_videomute"
|
||||
/>
|
||||
}
|
||||
label={t("settings.devices.camera")}
|
||||
title={t("settings.devices.camera_source")}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) setDismissedRefusal(refusal);
|
||||
}}
|
||||
>
|
||||
<CameraContextMenu
|
||||
options={options}
|
||||
selectedOption={selected}
|
||||
onSelect={select}
|
||||
backgroundEffects={backgroundEffects}
|
||||
selectedBackgroundEffect={backgroundEffect}
|
||||
onSelectBackgroundEffect={selectBackgroundEffect}
|
||||
backgroundEffectNotice={noticeText(notice, t)}
|
||||
backgroundEffectSettling={settling}
|
||||
onAddBackgroundImage={addBackgroundImage}
|
||||
onRemoveBackgroundEffect={removeBackgroundEffect}
|
||||
backgroundImageRefusal={refusalText(shownRefusal, t)}
|
||||
onDismissBackgroundImageRefusal={() => setDismissedRefusal(refusal)}
|
||||
selfPreview={<SelfPreview track={cameraTrack} />}
|
||||
/>
|
||||
</MediaButtonWithContextMenu>
|
||||
);
|
||||
};
|
||||
|
||||
function noticeText(
|
||||
notice: "unavailable" | "slow" | undefined,
|
||||
t: TFunction,
|
||||
): string | undefined {
|
||||
switch (notice) {
|
||||
case undefined:
|
||||
return undefined;
|
||||
case "unavailable":
|
||||
return t("background_effects.unavailable");
|
||||
case "slow":
|
||||
return t("background_effects.slow");
|
||||
}
|
||||
}
|
||||
|
||||
function refusalText(
|
||||
refusal: BackgroundImageRefusal | undefined,
|
||||
t: TFunction,
|
||||
): string | undefined {
|
||||
switch (refusal?.reason) {
|
||||
case undefined:
|
||||
return undefined;
|
||||
case "not-an-image":
|
||||
return t("error.background_not_an_image");
|
||||
case "animated":
|
||||
return t("error.background_animated");
|
||||
case "undecodable":
|
||||
return t("error.background_undecodable");
|
||||
case "not-kept":
|
||||
return t("error.background_not_kept");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
.rotate {
|
||||
animation: spinner 1.5s linear infinite;
|
||||
}
|
||||
@keyframes spinner {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
/* Its share of the measured call area: the menu is portalled out of the root,
|
||||
so CSS can't size it against the call. */
|
||||
.deviceList {
|
||||
max-block-size: var(--device-list-max-height);
|
||||
/* The raised headings stay raised within the list, under the menu's frame. */
|
||||
isolation: isolate;
|
||||
}
|
||||
|
||||
/* Compound's heading rule heads each section; the design adds space below it. */
|
||||
.deviceList h3 {
|
||||
margin-block-end: var(--cpd-space-5x);
|
||||
}
|
||||
|
||||
/* And more above a heading that follows another section. */
|
||||
.deviceList [role="group"] + [role="group"] h3 {
|
||||
margin-block-start: var(--cpd-space-7x);
|
||||
}
|
||||
|
||||
/* Raised, or the positioned effect tiles scroll over the heading they sit
|
||||
under. Compound's sticky heading is the section's aria-hidden first child. */
|
||||
.deviceList [role="group"] > [aria-hidden="true"]:first-child {
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
/* Radix focuses whatever the pointer is over, so the browser's ring would
|
||||
follow the mouse. Replaced by one shown only for keyboard focus. */
|
||||
.deviceList [role^="menuitem"]:focus,
|
||||
.deviceList [role^="menuitem"]:focus-visible {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
[data-focus-source="keyboard"] .deviceList [role^="menuitem"]:focus {
|
||||
outline: var(--cpd-border-width-2) solid var(--cpd-color-border-focused);
|
||||
outline-offset: calc(-1 * var(--cpd-border-width-2));
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useState,
|
||||
type CSSProperties,
|
||||
type FC,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import {
|
||||
MenuItem,
|
||||
MenuScrollArea,
|
||||
MenuScrollSection,
|
||||
RadioInput,
|
||||
} from "@vector-im/compound-web";
|
||||
import { SpinnerIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
|
||||
import classNames from "classnames";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { distinctUntilChanged, map } from "rxjs";
|
||||
|
||||
import styles from "./DeviceMenu.module.css";
|
||||
import {
|
||||
type AudioOutputDeviceLabel,
|
||||
type DeviceLabel,
|
||||
} from "../state/MediaDevices";
|
||||
import { useRootElement } from "../RootElementContext";
|
||||
import { type ElementSize, observeElementSize$ } from "../utils/elementSize";
|
||||
import { MENU_MARGIN, MENU_WIDTH } from "./MediaButtonWithContextMenu";
|
||||
|
||||
export interface MenuOptions {
|
||||
label: DeviceLabel | AudioOutputDeviceLabel;
|
||||
id: string;
|
||||
}
|
||||
|
||||
/** Largest share of the call area's height the device list may take. */
|
||||
const LIST_SHARE_OF_CALL = 0.6;
|
||||
|
||||
/** Smallest device list height in px, so a short call still shows more than one device. */
|
||||
export const MIN_LIST_HEIGHT = 160;
|
||||
|
||||
interface CallAreaShare {
|
||||
listHeight: number;
|
||||
menuWidth: number;
|
||||
}
|
||||
|
||||
function shareOf({ width, height }: ElementSize): CallAreaShare {
|
||||
return {
|
||||
listHeight: Math.round(height * LIST_SHARE_OF_CALL),
|
||||
menuWidth: Math.min(MENU_WIDTH, Math.round(width - 2 * MENU_MARGIN)),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The height the device list may take and the width the menu gets, from the
|
||||
* call area: CSS can't size the portalled menu against it.
|
||||
*/
|
||||
export function useCallAreaShare(): CallAreaShare {
|
||||
const rootElement = useRootElement();
|
||||
// Measured as the menu is first drawn, so what scrolls into view as it
|
||||
// appears does so in a list of its final height.
|
||||
const [share, setShare] = useState(() =>
|
||||
shareOf({
|
||||
width: rootElement.clientWidth,
|
||||
height: rootElement.clientHeight,
|
||||
}),
|
||||
);
|
||||
useEffect(() => {
|
||||
// Followed, since a host can resize the call while the menu is open.
|
||||
const subscription = observeElementSize$(rootElement)
|
||||
.pipe(
|
||||
map(shareOf),
|
||||
distinctUntilChanged(
|
||||
(a, b) =>
|
||||
a.listHeight === b.listHeight && a.menuWidth === b.menuWidth,
|
||||
),
|
||||
)
|
||||
.subscribe(setShare);
|
||||
return (): void => subscription.unsubscribe();
|
||||
}, [rootElement]);
|
||||
return share;
|
||||
}
|
||||
|
||||
/**
|
||||
* The device last asked for, until it is selected. Nothing can be picked
|
||||
* while it is on offer but not yet selected, so a second request can't
|
||||
* overtake it. Keyed by list too, since Chrome uses "default" for both an
|
||||
* input and an output. Held as long as the menu is open.
|
||||
*/
|
||||
export function usePlannedSelection<List extends string>(
|
||||
lists: Record<List, { options: MenuOptions[]; selected: string | undefined }>,
|
||||
): {
|
||||
requested: (list: List) => string | undefined;
|
||||
plan: (list: List, id: string) => void;
|
||||
settling: boolean;
|
||||
} {
|
||||
const [planned, setPlanned] = useState<{ list: List; id: string } | null>(
|
||||
null,
|
||||
);
|
||||
const plannedList = planned && lists[planned.list];
|
||||
const settling =
|
||||
planned !== null &&
|
||||
planned.id !== plannedList?.selected &&
|
||||
plannedList?.options.some(({ id }) => id === planned.id) === true;
|
||||
return {
|
||||
requested: (list) => (planned?.list === list ? planned.id : undefined),
|
||||
plan: (list, id) => setPlanned({ list, id }),
|
||||
settling,
|
||||
};
|
||||
}
|
||||
|
||||
interface DeviceMenuListProps {
|
||||
maxHeight: number;
|
||||
className?: string;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
/** The scrolling part of a device menu, holding its sections. */
|
||||
export const DeviceMenuList: FC<DeviceMenuListProps> = ({
|
||||
maxHeight,
|
||||
className,
|
||||
children,
|
||||
}) => {
|
||||
/**
|
||||
* Records on the menu whether the keyboard or the pointer moved focus, for
|
||||
* the focus ring. `:focus-visible` can't tell: Radix focuses whatever the
|
||||
* pointer is over. Listened for on the menu, not the document, so a second
|
||||
* Element Call on the page doesn't answer for this one.
|
||||
*/
|
||||
const trackFocusSource = useCallback(
|
||||
(list: HTMLDivElement | null): (() => void) | undefined => {
|
||||
const menu = list?.closest<HTMLElement>('[role="menu"]');
|
||||
if (menu === null || menu === undefined) return;
|
||||
const record = (source: "keyboard" | "pointer"): void => {
|
||||
menu.dataset.focusSource = source;
|
||||
};
|
||||
record("pointer");
|
||||
const usedKeyboard = (): void => record("keyboard");
|
||||
const usedPointer = (): void => record("pointer");
|
||||
menu.addEventListener("keydown", usedKeyboard, true);
|
||||
menu.addEventListener("pointermove", usedPointer, true);
|
||||
return (): void => {
|
||||
menu.removeEventListener("keydown", usedKeyboard, true);
|
||||
menu.removeEventListener("pointermove", usedPointer, true);
|
||||
};
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
return (
|
||||
<MenuScrollArea
|
||||
ref={trackFocusSource}
|
||||
className={classNames(styles.deviceList, className)}
|
||||
data-testid="device-list"
|
||||
style={
|
||||
{
|
||||
"--device-list-max-height": `${maxHeight}px`,
|
||||
} as CSSProperties
|
||||
}
|
||||
>
|
||||
{children}
|
||||
</MenuScrollArea>
|
||||
);
|
||||
};
|
||||
|
||||
interface DeviceMenuSectionProps {
|
||||
title: string;
|
||||
options: MenuOptions[];
|
||||
selected: string | undefined;
|
||||
/** Undefined disables the rows. */
|
||||
onSelect: ((id: string) => void) | undefined;
|
||||
/** Names a device known only by its number. */
|
||||
numbered: (n: number) => string;
|
||||
/** The device asked for and not yet selected, marked as activating. */
|
||||
requested: string | undefined;
|
||||
/** Disables the rows while a request anywhere in the menu is outstanding. */
|
||||
locked: boolean;
|
||||
stickyBottomItem?: ReactNode;
|
||||
}
|
||||
|
||||
/** One list of devices to pick from, as radio rows. */
|
||||
export const DeviceMenuSection: FC<DeviceMenuSectionProps> = ({
|
||||
title,
|
||||
options,
|
||||
selected,
|
||||
onSelect,
|
||||
numbered,
|
||||
requested,
|
||||
locked,
|
||||
stickyBottomItem,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const labelText = (label: MenuOptions["label"]): string => {
|
||||
switch (label.type) {
|
||||
case "name":
|
||||
return label.name;
|
||||
case "number":
|
||||
return numbered(label.number);
|
||||
case "default":
|
||||
return label.name === null
|
||||
? t("settings.devices.default")
|
||||
: t("settings.devices.default_named_plain", { name: label.name });
|
||||
case "speaker":
|
||||
return t("settings.devices.loudspeaker");
|
||||
case "earpiece":
|
||||
return t("settings.devices.handset");
|
||||
}
|
||||
};
|
||||
|
||||
// Disabled rather than hidden, so the menu keeps its shape.
|
||||
const disabled = onSelect === undefined || options.length <= 1 || locked;
|
||||
return (
|
||||
<MenuScrollSection title={title} stickyBottomItem={stickyBottomItem}>
|
||||
{options.map(({ label, id }) => (
|
||||
<MenuItem
|
||||
// A radio input may not sit inside a button.
|
||||
as="div"
|
||||
hideChevron
|
||||
disabled={disabled}
|
||||
label={labelText(label)}
|
||||
Icon={
|
||||
// Inert, not aria-hidden: aria-hidden alone leaves it focusable.
|
||||
<span inert>
|
||||
<RadioInput
|
||||
checked={selected === id}
|
||||
disabled={disabled}
|
||||
// Not readOnly, which mutes the selected fill. The item handles
|
||||
// the click.
|
||||
onChange={(): void => {}}
|
||||
/>
|
||||
</span>
|
||||
}
|
||||
onSelect={(e) => {
|
||||
e.preventDefault();
|
||||
if (id === selected) return;
|
||||
onSelect?.(id);
|
||||
}}
|
||||
key={id}
|
||||
role="menuitemradio"
|
||||
aria-checked={selected === id}
|
||||
>
|
||||
{selected !== id && requested === id && (
|
||||
<SpinnerIcon
|
||||
width={24}
|
||||
height={24}
|
||||
className={styles.rotate}
|
||||
aria-label={t("settings.devices.activating")}
|
||||
/>
|
||||
)}
|
||||
</MenuItem>
|
||||
))}
|
||||
</MenuScrollSection>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { useState, type FC, type ReactNode } from "react";
|
||||
|
||||
import { RootElementProvider } from "../RootElementContext";
|
||||
|
||||
/** Supplies a call-sized root. Without one the list is bounded by the whole Storybook frame. */
|
||||
export const WithACallArea: FC<{ children: ReactNode; blockSize?: number }> = ({
|
||||
children,
|
||||
// A call's size: anything smaller makes a short device list scroll, and a
|
||||
// narrower one narrows the menu.
|
||||
blockSize = 720,
|
||||
}) => {
|
||||
const [callArea, setCallArea] = useState<HTMLElement | null>(null);
|
||||
return (
|
||||
<div
|
||||
ref={setCallArea}
|
||||
style={{
|
||||
blockSize,
|
||||
inlineSize: 1024,
|
||||
display: "flex",
|
||||
alignItems: "flex-end",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
{callArea !== null && (
|
||||
<RootElementProvider value={callArea}>{children}</RootElementProvider>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
/** Painted outline width, in px. */
|
||||
export function outlineWidth(element: HTMLElement): number {
|
||||
const { outlineStyle, outlineWidth } = getComputedStyle(element);
|
||||
if (outlineStyle === "none") return 0;
|
||||
return Number.parseFloat(outlineWidth) || 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
background-color: var(--cpd-color-bg-subtle-secondary);
|
||||
border-radius: 32px;
|
||||
transition: background-color 0.2s ease-in-out;
|
||||
}
|
||||
.containerOpen {
|
||||
background-color: var(--cpd-color-bg-action-primary-pressed);
|
||||
}
|
||||
.chevronIconOpen > svg {
|
||||
color: var(--cpd-color-icon-on-solid-primary);
|
||||
}
|
||||
.menuButton {
|
||||
width: 40px;
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
/* The width design sets: a long device name wraps. */
|
||||
.menu {
|
||||
inline-size: 296px;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { type JSX } from "react";
|
||||
import { TooltipProvider } from "@vector-im/compound-web";
|
||||
|
||||
import {
|
||||
MediaButtonWithContextMenu,
|
||||
useHoldMenuOpen,
|
||||
} from "./MediaButtonWithContextMenu";
|
||||
import { MediaDevicesContext } from "../MediaDevicesContext";
|
||||
import { type MediaDevices } from "../state/MediaDevices";
|
||||
|
||||
function Held(): JSX.Element {
|
||||
const hold = useHoldMenuOpen();
|
||||
return (
|
||||
<>
|
||||
<button onClick={() => hold(true)}>Hold</button>
|
||||
<button onClick={() => hold(false)}>Release</button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function renderButton({
|
||||
requestDeviceNames = vi.fn(),
|
||||
onOpenChange,
|
||||
}: {
|
||||
requestDeviceNames?: () => void;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
} = {}): void {
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<MediaDevicesContext
|
||||
value={{ requestDeviceNames } as unknown as MediaDevices}
|
||||
>
|
||||
<MediaButtonWithContextMenu
|
||||
muteButton={<button>Mute</button>}
|
||||
label="Options"
|
||||
title="Options menu"
|
||||
onOpenChange={onOpenChange}
|
||||
>
|
||||
<Held />
|
||||
</MediaButtonWithContextMenu>
|
||||
</MediaDevicesContext>
|
||||
</TooltipProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("MediaButtonWithContextMenu", () => {
|
||||
test("draws the menu's content only while it is open", async () => {
|
||||
const onOpenChange = vi.fn();
|
||||
renderButton({ onOpenChange });
|
||||
screen.getByRole("button", { name: "Mute" });
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Options" }));
|
||||
expect(screen.getByRole("menu")).toBeVisible();
|
||||
expect(onOpenChange).toHaveBeenLastCalledWith(true);
|
||||
|
||||
await userEvent.keyboard("{Escape}");
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
expect(onOpenChange).toHaveBeenLastCalledWith(false);
|
||||
});
|
||||
|
||||
test("requests device names when opened", async () => {
|
||||
const requestDeviceNames = vi.fn();
|
||||
renderButton({ requestDeviceNames });
|
||||
|
||||
expect(requestDeviceNames).not.toHaveBeenCalled();
|
||||
await userEvent.click(screen.getByRole("button", { name: "Options" }));
|
||||
expect(requestDeviceNames).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("stays open while held", async () => {
|
||||
renderButton();
|
||||
await userEvent.click(screen.getByRole("button", { name: "Options" }));
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Hold" }));
|
||||
await userEvent.keyboard("{Escape}");
|
||||
expect(screen.getByRole("menu")).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Release" }));
|
||||
await userEvent.keyboard("{Escape}");
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
type FC,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { Button, Menu } from "@vector-im/compound-web";
|
||||
import {
|
||||
ChevronDownIcon,
|
||||
ChevronUpIcon,
|
||||
} from "@vector-im/compound-design-tokens/assets/web/icons";
|
||||
import classNames from "classnames";
|
||||
|
||||
import styles from "./MediaButtonWithContextMenu.module.css";
|
||||
import { useMediaDevices } from "../MediaDevicesContext";
|
||||
import { useRootElement } from "../RootElementContext";
|
||||
|
||||
/** The menu's width as its stylesheet sets it. */
|
||||
export const MENU_WIDTH = 296;
|
||||
|
||||
/** Space kept between the menu and the call area's sides. */
|
||||
export const MENU_MARGIN = 16;
|
||||
|
||||
const HoldOpenContext = createContext<(held: boolean) => void>(() => {});
|
||||
|
||||
/**
|
||||
* Keeps the menu open while held, though focus leaves it: a file picker takes
|
||||
* the focus, which the menu reads as a click elsewhere.
|
||||
*/
|
||||
export function useHoldMenuOpen(): (held: boolean) => void {
|
||||
return useContext(HoldOpenContext);
|
||||
}
|
||||
|
||||
interface Props {
|
||||
/** Drawn as it is, before the chevron. */
|
||||
muteButton: ReactNode;
|
||||
/** The chevron's label. */
|
||||
label: string;
|
||||
/** Names the menu for screen readers. */
|
||||
title: string;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
/** The menu's content, drawn only while it is open. */
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
/** A mute button with a chevron beside it that opens a menu. */
|
||||
export const MediaButtonWithContextMenu: FC<Props> = ({
|
||||
muteButton,
|
||||
label,
|
||||
title,
|
||||
onOpenChange,
|
||||
children,
|
||||
}) => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const held = useRef(false);
|
||||
const hold = useCallback((h: boolean): void => {
|
||||
held.current = h;
|
||||
}, []);
|
||||
const changeOpen = useCallback(
|
||||
(next: boolean): void => {
|
||||
if (!next && held.current) return;
|
||||
setOpen(next);
|
||||
onOpenChange?.(next);
|
||||
},
|
||||
[onOpenChange],
|
||||
);
|
||||
|
||||
const devices = useMediaDevices();
|
||||
useEffect(() => {
|
||||
if (open) devices.requestDeviceNames(); // No-op after the first call
|
||||
}, [open, devices]);
|
||||
|
||||
const rootElement = useRootElement();
|
||||
return (
|
||||
<div
|
||||
className={classNames(styles.container, {
|
||||
[styles.containerOpen]: open,
|
||||
})}
|
||||
>
|
||||
{muteButton}
|
||||
<Menu
|
||||
className={styles.menu}
|
||||
// Named for screen readers only: each section has its own heading.
|
||||
title={title}
|
||||
showTitle={false}
|
||||
open={open}
|
||||
onOpenChange={changeOpen}
|
||||
side="top"
|
||||
collisionBoundary={rootElement}
|
||||
collisionPadding={MENU_MARGIN}
|
||||
trigger={
|
||||
<Button
|
||||
iconOnly
|
||||
className={classNames(styles.menuButton, {
|
||||
[styles.chevronIconOpen]: open,
|
||||
})}
|
||||
Icon={open ? ChevronUpIcon : ChevronDownIcon}
|
||||
kind="tertiary"
|
||||
size="lg"
|
||||
aria-label={label}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<HoldOpenContext value={hold}>{children}</HoldOpenContext>
|
||||
</Menu>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type FC, useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { MicButton, VideoButton } from "../button";
|
||||
import { MediaMuteAndSwitchButton } from "./MediaMuteAndSwitchButton";
|
||||
import { type MediaMenuViewModel } from "./MediaMenuViewModel";
|
||||
import { SelfPreview } from "./SelfPreview";
|
||||
import { useBehavior } from "../useBehavior";
|
||||
|
||||
interface Props {
|
||||
vm: MediaMenuViewModel;
|
||||
kind: "audio" | "video";
|
||||
size: "md" | "lg";
|
||||
}
|
||||
|
||||
/**
|
||||
* The microphone's or the camera's mute button: with a menu to pick the
|
||||
* device while there are devices to offer, a plain mute button otherwise.
|
||||
*/
|
||||
export const MediaMenuButton: FC<Props> = ({ vm, kind, size }) => {
|
||||
const { t } = useTranslation();
|
||||
const enabled = useBehavior(vm.enabled$);
|
||||
const busy = useBehavior(vm.busy$);
|
||||
const toggle = useBehavior(vm.toggle$);
|
||||
const options = useBehavior(vm.options$);
|
||||
const selected = useBehavior(vm.selected$);
|
||||
const select = useBehavior(vm.select$);
|
||||
const outputOptions = useBehavior(vm.outputOptions$);
|
||||
const selectedOutput = useBehavior(vm.selectedOutput$);
|
||||
const selectOutput = useBehavior(vm.selectOutput$);
|
||||
const backgroundEffect = useBehavior(vm.backgroundEffect$);
|
||||
const backgroundEffects = useBehavior(vm.backgroundEffects$);
|
||||
const notice = useBehavior(vm.backgroundEffectNotice$);
|
||||
const settling = useBehavior(vm.backgroundEffectSettling$);
|
||||
const refusal = useBehavior(vm.backgroundImageRefusal$);
|
||||
const selectBackgroundEffect = useBehavior(vm.selectBackgroundEffect$);
|
||||
const addBackgroundImage = useBehavior(vm.addBackgroundImage$);
|
||||
const removeBackgroundEffect = useBehavior(vm.removeBackgroundEffect$);
|
||||
const cameraTrack = useBehavior(vm.cameraTrack$);
|
||||
|
||||
const testId = kind === "audio" ? "incall_mute" : "incall_videomute";
|
||||
|
||||
const noticeText = useMemo(() => {
|
||||
switch (notice) {
|
||||
case undefined:
|
||||
return undefined;
|
||||
case "unavailable":
|
||||
return t("background_effects.unavailable");
|
||||
case "slow":
|
||||
return t("background_effects.slow");
|
||||
}
|
||||
}, [notice, t]);
|
||||
// A new object for each refusal, so the same one twice is shown twice.
|
||||
const refusalMessage = useMemo(() => {
|
||||
switch (refusal?.reason) {
|
||||
case undefined:
|
||||
return undefined;
|
||||
case "not-an-image":
|
||||
return { text: t("error.background_not_an_image") };
|
||||
case "animated":
|
||||
return { text: t("error.background_animated") };
|
||||
case "undecodable":
|
||||
return { text: t("error.background_undecodable") };
|
||||
case "not-kept":
|
||||
return { text: t("error.background_not_kept") };
|
||||
}
|
||||
}, [refusal, t]);
|
||||
|
||||
if (options.length === 0) {
|
||||
const PlainButton = kind === "audio" ? MicButton : VideoButton;
|
||||
return (
|
||||
<PlainButton
|
||||
size={size}
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onClick={toggle}
|
||||
disabled={busy || toggle === undefined}
|
||||
data-testid={testId}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return kind === "audio" ? (
|
||||
<MediaMuteAndSwitchButton
|
||||
iconsAndLabels="audio"
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onMuteClick={toggle}
|
||||
data-testid={testId}
|
||||
options={options}
|
||||
selectedOption={selected}
|
||||
onSelect={select}
|
||||
outputOptions={outputOptions}
|
||||
selectedOutputOption={selectedOutput}
|
||||
onSelectOutput={selectOutput}
|
||||
/>
|
||||
) : (
|
||||
<MediaMuteAndSwitchButton
|
||||
iconsAndLabels="video"
|
||||
enabled={enabled}
|
||||
busy={busy}
|
||||
onMuteClick={toggle}
|
||||
options={options}
|
||||
selectedOption={selected}
|
||||
onSelect={select}
|
||||
backgroundEffects={backgroundEffects}
|
||||
selectedBackgroundEffect={backgroundEffect}
|
||||
onSelectBackgroundEffect={selectBackgroundEffect}
|
||||
backgroundEffectSettling={settling}
|
||||
onAddBackgroundImage={addBackgroundImage}
|
||||
onRemoveBackgroundEffect={removeBackgroundEffect}
|
||||
selfPreview={<SelfPreview track={cameraTrack} />}
|
||||
backgroundImageRefusal={refusalMessage}
|
||||
backgroundEffectNotice={noticeText}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -22,7 +22,8 @@ import type { DeviceLabel } from "../state/MediaDevices";
|
||||
import {
|
||||
createAudioMenuViewModel,
|
||||
createCameraMenuViewModel,
|
||||
type MediaMenuViewModel,
|
||||
type AudioMenuViewModel,
|
||||
type CameraMenuViewModel,
|
||||
} from "./MediaMenuViewModel";
|
||||
import { backgroundEffect as backgroundEffectSetting } from "../settings/settings";
|
||||
import { shippedBackgrounds } from "../tracks/backgroundEffectCatalog";
|
||||
@@ -83,7 +84,7 @@ const devices = mockMediaDevices({
|
||||
},
|
||||
});
|
||||
|
||||
function audioMenu(disableSwitcher = false): MediaMenuViewModel {
|
||||
function audioMenu(disableSwitcher = false): AudioMenuViewModel {
|
||||
return createAudioMenuViewModel(
|
||||
testScope(),
|
||||
mockMuteStates(),
|
||||
@@ -102,7 +103,7 @@ function cameraMenu({
|
||||
disableSwitcher?: boolean;
|
||||
pipeline$?: Observable<BackgroundEffectsState>;
|
||||
camera$?: Observable<LocalVideoMediaTrack | null>;
|
||||
} = {}): MediaMenuViewModel {
|
||||
} = {}): CameraMenuViewModel {
|
||||
return createCameraMenuViewModel(
|
||||
testScope(),
|
||||
mockMuteStates(),
|
||||
@@ -123,14 +124,14 @@ describe("createAudioMenuViewModel", () => {
|
||||
});
|
||||
|
||||
it("lists no outputs as an empty list, which keeps the section", () => {
|
||||
expect(audioMenu().outputOptions$.value).toEqual([]);
|
||||
expect(audioMenu().output.options$.value).toEqual([]);
|
||||
});
|
||||
|
||||
it.each([true, false])(
|
||||
"offers to pick an output only where the platform can route to it (%s)",
|
||||
(canRoute) => {
|
||||
outputSelectionMock.mockReturnValue(canRoute);
|
||||
expect(audioMenu().selectOutput$.value !== undefined).toBe(canRoute);
|
||||
expect(audioMenu().output.select$.value !== undefined).toBe(canRoute);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -144,22 +145,22 @@ describe("createCameraMenuViewModel", () => {
|
||||
it("offers effects where they run, saying where they run slowly", () => {
|
||||
modernRouteMock.mockReturnValue(true);
|
||||
const vm = cameraMenu();
|
||||
expect(vm.selectBackgroundEffect$.value).toBeDefined();
|
||||
expect(vm.addBackgroundImage$.value).toBeDefined();
|
||||
expect(vm.backgroundEffectNotice$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.select$.value).toBeDefined();
|
||||
expect(vm.backgroundEffects.addImage$.value).toBeDefined();
|
||||
expect(vm.backgroundEffects.notice$.value).toBeUndefined();
|
||||
|
||||
modernRouteMock.mockReturnValue(false);
|
||||
const slow = cameraMenu();
|
||||
expect(slow.selectBackgroundEffect$.value).toBeDefined();
|
||||
expect(slow.backgroundEffectNotice$.value).toBe("slow");
|
||||
expect(slow.backgroundEffects.select$.value).toBeDefined();
|
||||
expect(slow.backgroundEffects.notice$.value).toBe("slow");
|
||||
modernRouteMock.mockReturnValue(true);
|
||||
});
|
||||
|
||||
it("offers nothing while the switcher is withheld", () => {
|
||||
const vm = cameraMenu({ disableSwitcher: true });
|
||||
expect(vm.options$.value).toEqual([]);
|
||||
expect(vm.selectBackgroundEffect$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffectNotice$.value).toBe("unavailable");
|
||||
expect(vm.backgroundEffects.select$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.notice$.value).toBe("unavailable");
|
||||
});
|
||||
|
||||
it("offers nothing once the pipeline fails to build, keeping the choice", () => {
|
||||
@@ -169,28 +170,28 @@ describe("createCameraMenuViewModel", () => {
|
||||
processor: undefined,
|
||||
});
|
||||
const vm = cameraMenu({ pipeline$ });
|
||||
expect(vm.backgroundEffect$.value).toBe("blur");
|
||||
expect(vm.backgroundEffects.selected$.value).toBe("blur");
|
||||
|
||||
pipeline$.next({ supported: false, processor: undefined });
|
||||
expect(vm.selectBackgroundEffect$.value).toBeUndefined();
|
||||
expect(vm.addBackgroundImage$.value).toBeUndefined();
|
||||
expect(vm.removeBackgroundEffect$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffectNotice$.value).toBe("unavailable");
|
||||
expect(vm.backgroundEffect$.value).toBe("none");
|
||||
expect(vm.backgroundEffects.select$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.addImage$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.remove$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.notice$.value).toBe("unavailable");
|
||||
expect(vm.backgroundEffects.selected$.value).toBe("none");
|
||||
expect(backgroundEffectSetting.getValue()).toBe("blur");
|
||||
});
|
||||
|
||||
it("saves the effect chosen, and none for one not on offer", () => {
|
||||
const vm = cameraMenu();
|
||||
vm.selectBackgroundEffect$.value!("image:arc");
|
||||
expect(vm.backgroundEffect$.value).toBe("image:arc");
|
||||
vm.selectBackgroundEffect$.value!("image:gone");
|
||||
vm.backgroundEffects.select$.value!("image:arc");
|
||||
expect(vm.backgroundEffects.selected$.value).toBe("image:arc");
|
||||
vm.backgroundEffects.select$.value!("image:gone");
|
||||
expect(backgroundEffectSetting.getValue()).toBe("none");
|
||||
});
|
||||
|
||||
it("offers every effect in order, the added ones removable", () => {
|
||||
added$.next([{ id: "mine", url: "blob:mine" }]);
|
||||
expect(cameraMenu().backgroundEffects$.value).toEqual([
|
||||
expect(cameraMenu().backgroundEffects.available$.value).toEqual([
|
||||
{ id: "none", kind: "none", removable: false },
|
||||
{ id: "blur", kind: "blur", removable: false },
|
||||
...shippedBackgrounds.map((background) => ({
|
||||
@@ -215,9 +216,9 @@ describe("createCameraMenuViewModel", () => {
|
||||
wait: "settling",
|
||||
});
|
||||
const vm = cameraMenu({ pipeline$ });
|
||||
expect(vm.backgroundEffectSettling$.value).toBe(true);
|
||||
expect(vm.backgroundEffects.settling$.value).toBe(true);
|
||||
pipeline$.next({ ...pipeline$.value, wait: undefined });
|
||||
expect(vm.backgroundEffectSettling$.value).toBe(false);
|
||||
expect(vm.backgroundEffects.settling$.value).toBe(false);
|
||||
});
|
||||
|
||||
it("offers adding only below the limit", () => {
|
||||
@@ -228,43 +229,47 @@ describe("createCameraMenuViewModel", () => {
|
||||
url: `blob:${i}`,
|
||||
})),
|
||||
);
|
||||
expect(vm.addBackgroundImage$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.addImage$.value).toBeUndefined();
|
||||
added$.next(added$.value!.slice(1));
|
||||
expect(vm.addBackgroundImage$.value).toBeDefined();
|
||||
expect(vm.backgroundEffects.addImage$.value).toBeDefined();
|
||||
});
|
||||
|
||||
it("says why each refused file was refused", async () => {
|
||||
const vm = cameraMenu();
|
||||
store.add.mockRejectedValue(new UnusableImage("animated"));
|
||||
vm.addBackgroundImage$.value!(new File(["x"], "a.gif"));
|
||||
vm.backgroundEffects.addImage$.value!(new File(["x"], "a.gif"));
|
||||
await flushPromises();
|
||||
const first = vm.backgroundImageRefusal$.value;
|
||||
const first = vm.backgroundEffects.imageRefusal$.value;
|
||||
expect(first).toEqual({ reason: "animated" });
|
||||
|
||||
// The same again is a refusal of its own, so it is shown again.
|
||||
vm.addBackgroundImage$.value!(new File(["x"], "a.gif"));
|
||||
vm.backgroundEffects.addImage$.value!(new File(["x"], "a.gif"));
|
||||
await flushPromises();
|
||||
expect(vm.backgroundImageRefusal$.value).toEqual({ reason: "animated" });
|
||||
expect(vm.backgroundImageRefusal$.value).not.toBe(first);
|
||||
expect(vm.backgroundEffects.imageRefusal$.value).toEqual({
|
||||
reason: "animated",
|
||||
});
|
||||
expect(vm.backgroundEffects.imageRefusal$.value).not.toBe(first);
|
||||
|
||||
store.add.mockRejectedValue(new Error("quota"));
|
||||
vm.addBackgroundImage$.value!(new File(["x"], "b.png"));
|
||||
vm.backgroundEffects.addImage$.value!(new File(["x"], "b.png"));
|
||||
await flushPromises();
|
||||
expect(vm.backgroundImageRefusal$.value).toEqual({ reason: "not-kept" });
|
||||
expect(vm.backgroundEffects.imageRefusal$.value).toEqual({
|
||||
reason: "not-kept",
|
||||
});
|
||||
|
||||
store.add.mockResolvedValue("kept");
|
||||
vm.addBackgroundImage$.value!(new File(["x"], "c.png"));
|
||||
vm.backgroundEffects.addImage$.value!(new File(["x"], "c.png"));
|
||||
await flushPromises();
|
||||
expect(vm.backgroundImageRefusal$.value).toBeUndefined();
|
||||
expect(vm.backgroundEffects.imageRefusal$.value).toBeUndefined();
|
||||
});
|
||||
|
||||
it("never removes the selected background", () => {
|
||||
store.remove.mockResolvedValue();
|
||||
const vm = cameraMenu();
|
||||
backgroundEffectSetting.setValue("added:worn");
|
||||
vm.removeBackgroundEffect$.value!("added:worn");
|
||||
vm.backgroundEffects.remove$.value!("added:worn");
|
||||
expect(store.remove).not.toHaveBeenCalled();
|
||||
vm.removeBackgroundEffect$.value!("added:spare");
|
||||
vm.backgroundEffects.remove$.value!("added:spare");
|
||||
expect(store.remove).toHaveBeenCalledWith("spare");
|
||||
});
|
||||
|
||||
|
||||
@@ -32,13 +32,13 @@ import {
|
||||
UnusableImage,
|
||||
} from "../tracks/backgroundImages";
|
||||
import { type BackgroundEffectsState } from "../state/BackgroundEffects";
|
||||
import { type MenuOptions } from "./MediaMuteAndSwitchButton";
|
||||
import { type MenuOptions } from "./DeviceMenu";
|
||||
import { type MediaDevices } from "../state/MediaDevices";
|
||||
import { backgroundEffect as backgroundEffectSetting } from "../settings/settings";
|
||||
import { type Behavior, constant } from "../state/Behavior";
|
||||
import type { ObservableScope } from "../state/ObservableScope";
|
||||
import { type MuteStates } from "../state/MuteStates";
|
||||
import { createStaticViewModel, type ViewModel } from "../state/ViewModel";
|
||||
import { type ViewModel } from "../state/ViewModel";
|
||||
import { type BackgroundEffectGridItem } from "./BackgroundEffectGrid";
|
||||
|
||||
export interface BackgroundImageRefusal {
|
||||
@@ -47,9 +47,9 @@ export interface BackgroundImageRefusal {
|
||||
|
||||
/**
|
||||
* A mute button with a menu to pick the device, shared by the microphone and
|
||||
* the camera. Fields that belong to the other device are left empty.
|
||||
* the camera.
|
||||
*/
|
||||
export interface MediaMenuSnapshot {
|
||||
export interface DeviceMenuSnapshot {
|
||||
/** Whether the device is on. (muted/unmuted state) */
|
||||
enabled: boolean;
|
||||
busy: boolean;
|
||||
@@ -58,46 +58,47 @@ export interface MediaMenuSnapshot {
|
||||
options: MenuOptions[];
|
||||
selected: string | undefined;
|
||||
select: ((id: string) => void) | undefined;
|
||||
|
||||
/** Output (speaker) devices. Microphone only. */
|
||||
outputOptions: MenuOptions[];
|
||||
selectedOutput: string | undefined;
|
||||
/** Undefined if the output device is not configurable. */
|
||||
selectOutput: ((id: string) => void) | undefined;
|
||||
|
||||
/** The selected background effect, as its option id. Camera only. */
|
||||
backgroundEffect: string | undefined;
|
||||
/** Every effect on offer, in the order they are shown. */
|
||||
backgroundEffects: BackgroundEffectGridItem[];
|
||||
/** What the user is told about effects here, if anything. */
|
||||
backgroundEffectNotice: "unavailable" | "slow" | undefined;
|
||||
/** Whether the first effect chosen is still being prepared. */
|
||||
backgroundEffectSettling: boolean;
|
||||
/** Why the last file offered couldn't be kept, if it couldn't. */
|
||||
backgroundImageRefusal: BackgroundImageRefusal | undefined;
|
||||
/** Undefined where background effects can't be chosen. */
|
||||
selectBackgroundEffect: ((id: string) => void) | undefined;
|
||||
/** Undefined where no background can be added. */
|
||||
addBackgroundImage: ((file: File) => void) | undefined;
|
||||
/** Removes an added background by its option id, unless it is the selected one. */
|
||||
removeBackgroundEffect: ((id: string) => void) | undefined;
|
||||
/** The camera to preview, with the selected effect; null while it is off. */
|
||||
cameraTrack: LocalVideoMediaTrack | null;
|
||||
}
|
||||
|
||||
export type MediaMenuViewModel = ViewModel<MediaMenuSnapshot>;
|
||||
/** The output (speaker) devices in the microphone's menu. */
|
||||
export interface OutputMenuSnapshot {
|
||||
options: MenuOptions[];
|
||||
selected: string | undefined;
|
||||
/** Undefined if the output device is not configurable. */
|
||||
select: ((id: string) => void) | undefined;
|
||||
}
|
||||
|
||||
const noBackgroundEffects = {
|
||||
backgroundEffect: undefined,
|
||||
backgroundEffects: [],
|
||||
backgroundEffectNotice: undefined,
|
||||
backgroundEffectSettling: false,
|
||||
backgroundImageRefusal: undefined,
|
||||
selectBackgroundEffect: undefined,
|
||||
addBackgroundImage: undefined,
|
||||
removeBackgroundEffect: undefined,
|
||||
cameraTrack: null,
|
||||
} satisfies Partial<MediaMenuSnapshot>;
|
||||
/** The background effects in the camera's menu. */
|
||||
export interface BackgroundEffectsMenuSnapshot {
|
||||
/** The selected effect, as its option id. */
|
||||
selected: string;
|
||||
/** Every effect on offer, in the order they are shown. */
|
||||
available: BackgroundEffectGridItem[];
|
||||
/** What the user is told about effects here, if anything. */
|
||||
notice: "unavailable" | "slow" | undefined;
|
||||
/** Whether the first effect chosen is still being prepared. */
|
||||
settling: boolean;
|
||||
/** Why the last file offered couldn't be kept, if it couldn't. */
|
||||
imageRefusal: BackgroundImageRefusal | undefined;
|
||||
/** Undefined where background effects can't be chosen. */
|
||||
select: ((id: string) => void) | undefined;
|
||||
/** Undefined where no background can be added. */
|
||||
addImage: ((file: File) => void) | undefined;
|
||||
/** Removes an added background by its option id, unless it is the selected one. */
|
||||
remove: ((id: string) => void) | undefined;
|
||||
}
|
||||
|
||||
export type AudioMenuViewModel = ViewModel<DeviceMenuSnapshot> & {
|
||||
readonly kind: "audio";
|
||||
readonly output: ViewModel<OutputMenuSnapshot>;
|
||||
};
|
||||
|
||||
export type CameraMenuViewModel = ViewModel<DeviceMenuSnapshot> & {
|
||||
readonly kind: "video";
|
||||
readonly backgroundEffects: ViewModel<BackgroundEffectsMenuSnapshot>;
|
||||
/** The camera to preview, with the selected effect; null while it is off. */
|
||||
readonly cameraTrack$: Behavior<LocalVideoMediaTrack | null>;
|
||||
};
|
||||
|
||||
function options$(
|
||||
disableSwitcher$: Behavior<boolean>,
|
||||
@@ -126,10 +127,10 @@ export function createAudioMenuViewModel(
|
||||
muteStates: MuteStates,
|
||||
mediaDevices: MediaDevices,
|
||||
disableSwitcher$: Behavior<boolean>,
|
||||
): MediaMenuViewModel {
|
||||
): AudioMenuViewModel {
|
||||
const { audio } = muteStates;
|
||||
return {
|
||||
...createStaticViewModel(noBackgroundEffects),
|
||||
kind: "audio",
|
||||
enabled$: audio.enabled$,
|
||||
busy$: audio.syncing$,
|
||||
toggle$: scope.behavior(audio.toggle$.pipe(map((t) => t ?? undefined))),
|
||||
@@ -140,17 +141,19 @@ export function createAudioMenuViewModel(
|
||||
mediaDevices.audioInput.selected$.pipe(map((s) => s?.id)),
|
||||
),
|
||||
select$: constant(mediaDevices.audioInput.select),
|
||||
outputOptions$: scope.behavior(
|
||||
options$(disableSwitcher$, mediaDevices.audioOutput.available$),
|
||||
),
|
||||
selectedOutput$: scope.behavior(
|
||||
mediaDevices.audioOutput.selected$.pipe(map((s) => s?.id)),
|
||||
),
|
||||
selectOutput$: constant(
|
||||
supportsAudioOutputSelection()
|
||||
? mediaDevices.audioOutput.select
|
||||
: undefined,
|
||||
),
|
||||
output: {
|
||||
options$: scope.behavior(
|
||||
options$(disableSwitcher$, mediaDevices.audioOutput.available$),
|
||||
),
|
||||
selected$: scope.behavior(
|
||||
mediaDevices.audioOutput.selected$.pipe(map((s) => s?.id)),
|
||||
),
|
||||
select$: constant(
|
||||
supportsAudioOutputSelection()
|
||||
? mediaDevices.audioOutput.select
|
||||
: undefined,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -170,7 +173,7 @@ export function createCameraMenuViewModel(
|
||||
disableSwitcher$: Behavior<boolean>,
|
||||
backgroundEffectsState$: Observable<BackgroundEffectsState>,
|
||||
camera$: Observable<LocalVideoMediaTrack | null | undefined>,
|
||||
): MediaMenuViewModel {
|
||||
): CameraMenuViewModel {
|
||||
const { video } = muteStates;
|
||||
// Also false once the pipeline has failed to build this session.
|
||||
const supported$ = backgroundEffectsState$.pipe(
|
||||
@@ -186,11 +189,7 @@ export function createCameraMenuViewModel(
|
||||
undefined,
|
||||
);
|
||||
return {
|
||||
...createStaticViewModel({
|
||||
outputOptions: [],
|
||||
selectedOutput: undefined,
|
||||
selectOutput: undefined,
|
||||
}),
|
||||
kind: "video",
|
||||
enabled$: video.enabled$,
|
||||
busy$: video.syncing$,
|
||||
toggle$: scope.behavior(video.toggle$.pipe(map((t) => t ?? undefined))),
|
||||
@@ -201,63 +200,86 @@ export function createCameraMenuViewModel(
|
||||
mediaDevices.videoInput.selected$.pipe(map((s) => s?.id)),
|
||||
),
|
||||
select$: constant(mediaDevices.videoInput.select),
|
||||
backgroundEffect$: scope.behavior(
|
||||
combineLatest([backgroundEffectSetting.value$, supported$]).pipe(
|
||||
map(([raw, supported]) =>
|
||||
supported ? serializeEffect(parseEffect(raw)) : "none",
|
||||
backgroundEffects: {
|
||||
selected$: scope.behavior(
|
||||
combineLatest([backgroundEffectSetting.value$, supported$]).pipe(
|
||||
map(([raw, supported]) =>
|
||||
supported ? serializeEffect(parseEffect(raw)) : "none",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
selectBackgroundEffect$: scope.behavior(
|
||||
offered$.pipe(
|
||||
map((offered) =>
|
||||
offered
|
||||
? (id: string): void =>
|
||||
backgroundEffectSetting.setValue(
|
||||
serializeEffect(parseEffect(id)),
|
||||
)
|
||||
: undefined,
|
||||
available$: scope.behavior(
|
||||
addedBackgrounds.added$.pipe(
|
||||
map((added) => backgroundEffectChoices(added ?? [])),
|
||||
),
|
||||
),
|
||||
),
|
||||
backgroundEffects$: scope.behavior(
|
||||
addedBackgrounds.added$.pipe(
|
||||
map((added) => backgroundEffectChoices(added ?? [])),
|
||||
),
|
||||
),
|
||||
backgroundEffectNotice$: scope.behavior(
|
||||
offered$.pipe(
|
||||
map((offered) =>
|
||||
!offered ? "unavailable" : slow ? "slow" : undefined,
|
||||
notice$: scope.behavior(
|
||||
offered$.pipe(
|
||||
map((offered) =>
|
||||
!offered ? "unavailable" : slow ? "slow" : undefined,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
backgroundEffectSettling$: scope.behavior(
|
||||
backgroundEffectsState$.pipe(
|
||||
map(({ wait }) => wait !== undefined),
|
||||
distinctUntilChanged(),
|
||||
settling$: scope.behavior(
|
||||
backgroundEffectsState$.pipe(
|
||||
map(({ wait }) => wait !== undefined),
|
||||
distinctUntilChanged(),
|
||||
),
|
||||
false,
|
||||
),
|
||||
false,
|
||||
),
|
||||
// Kept and offered, not put on: that waits for the user to choose it.
|
||||
addBackgroundImage$: scope.behavior(
|
||||
combineLatest([offered$, addedBackgrounds.added$]).pipe(
|
||||
map(([offered, added]) =>
|
||||
offered && (added?.length ?? 0) < maxAddedBackgrounds
|
||||
? (file: File): void => {
|
||||
refusal$.next(undefined);
|
||||
addedBackgrounds.add(file).catch((e) => {
|
||||
logger.warn("Could not keep that background", e);
|
||||
refusal$.next({
|
||||
reason: e instanceof UnusableImage ? e.reason : "not-kept",
|
||||
imageRefusal$: refusal$,
|
||||
select$: scope.behavior(
|
||||
offered$.pipe(
|
||||
map((offered) =>
|
||||
offered
|
||||
? (id: string): void =>
|
||||
backgroundEffectSetting.setValue(
|
||||
serializeEffect(parseEffect(id)),
|
||||
)
|
||||
: undefined,
|
||||
),
|
||||
),
|
||||
),
|
||||
// Kept and offered, not put on: that waits for the user to choose it.
|
||||
addImage$: scope.behavior(
|
||||
combineLatest([offered$, addedBackgrounds.added$]).pipe(
|
||||
map(([offered, added]) =>
|
||||
offered && (added?.length ?? 0) < maxAddedBackgrounds
|
||||
? (file: File): void => {
|
||||
refusal$.next(undefined);
|
||||
addedBackgrounds.add(file).catch((e) => {
|
||||
logger.warn("Could not keep that background", e);
|
||||
refusal$.next({
|
||||
reason:
|
||||
e instanceof UnusableImage ? e.reason : "not-kept",
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
backgroundImageRefusal$: refusal$,
|
||||
remove$: scope.behavior(
|
||||
offered$.pipe(
|
||||
map((offered) =>
|
||||
offered
|
||||
? (optionId: string): void => {
|
||||
const effect = parseEffect(optionId);
|
||||
if (effect.kind !== "added") return;
|
||||
// The selected effect can't be removed: that would leave no
|
||||
// effect selected.
|
||||
if (backgroundEffectSetting.getValue() === optionId) return;
|
||||
addedBackgrounds
|
||||
.remove(effect.id)
|
||||
.catch((e) =>
|
||||
logger.warn("Could not remove that background", e),
|
||||
);
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
),
|
||||
),
|
||||
},
|
||||
// Muting stops the camera, and so its light.
|
||||
// The track outlives the mute, so its mute state is checked.
|
||||
cameraTrack$: scope.behavior(
|
||||
@@ -270,26 +292,6 @@ export function createCameraMenuViewModel(
|
||||
),
|
||||
null,
|
||||
),
|
||||
removeBackgroundEffect$: scope.behavior(
|
||||
offered$.pipe(
|
||||
map((offered) =>
|
||||
offered
|
||||
? (optionId: string): void => {
|
||||
const effect = parseEffect(optionId);
|
||||
if (effect.kind !== "added") return;
|
||||
// The selected effect can't be removed: that would leave no
|
||||
// effect selected.
|
||||
if (backgroundEffectSetting.getValue() === optionId) return;
|
||||
addedBackgrounds
|
||||
.remove(effect.id)
|
||||
.catch((e) =>
|
||||
logger.warn("Could not remove that background", e),
|
||||
);
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
background-color: var(--cpd-color-bg-subtle-secondary);
|
||||
border-radius: 32px;
|
||||
transition: background-color 0.2s ease-in-out;
|
||||
}
|
||||
.containerOpen {
|
||||
background-color: var(--cpd-color-bg-action-primary-pressed);
|
||||
}
|
||||
.chevronIconOpen > svg {
|
||||
color: var(--cpd-color-icon-on-solid-primary);
|
||||
}
|
||||
.menuButton {
|
||||
width: 40px;
|
||||
background-color: transparent !important;
|
||||
}
|
||||
.itemIcon {
|
||||
color: var(--cpd-color-text-secondary);
|
||||
}
|
||||
|
||||
.rotate {
|
||||
animation: spinner 1.5s linear infinite;
|
||||
}
|
||||
@keyframes spinner {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
/* The width design sets: a long device name wraps. */
|
||||
.menu {
|
||||
inline-size: 296px;
|
||||
}
|
||||
|
||||
/* Its share of the measured call area: the menu is portalled out of the root,
|
||||
so CSS can't size it against the call. */
|
||||
.deviceList {
|
||||
max-block-size: var(--device-list-max-height);
|
||||
/* The raised headings stay raised within the list, under the menu's frame. */
|
||||
isolation: isolate;
|
||||
}
|
||||
|
||||
/* Compound's heading rule heads each section; the design adds space below it. */
|
||||
.menu h3 {
|
||||
margin-block-end: var(--cpd-space-5x);
|
||||
}
|
||||
|
||||
/* And more above a heading that follows another section. */
|
||||
.deviceList [role="group"] + [role="group"] h3 {
|
||||
margin-block-start: var(--cpd-space-7x);
|
||||
}
|
||||
|
||||
/* Raised, or the positioned effect tiles scroll over the heading they sit
|
||||
under. Compound's sticky heading is the section's aria-hidden first child. */
|
||||
.deviceList [role="group"] > [aria-hidden="true"]:first-child {
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
/* Radix focuses whatever the pointer is over, so the browser's ring would
|
||||
follow the mouse. Replaced by one shown only for keyboard focus. */
|
||||
.menu [role^="menuitem"]:focus,
|
||||
.menu [role^="menuitem"]:focus-visible {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.menu[data-focus-source="keyboard"] [role^="menuitem"]:focus {
|
||||
outline: var(--cpd-border-width-2) solid var(--cpd-color-border-focused);
|
||||
outline-offset: calc(-1 * var(--cpd-border-width-2));
|
||||
}
|
||||
|
||||
.notice {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: var(--cpd-space-3x);
|
||||
margin: var(--cpd-space-2x) var(--cpd-space-4x) 0;
|
||||
padding: var(--cpd-space-3x);
|
||||
border-radius: var(--cpd-space-2x);
|
||||
background: var(--cpd-color-bg-subtle-secondary);
|
||||
color: var(--cpd-color-text-primary);
|
||||
font: var(--cpd-font-body-md-regular);
|
||||
}
|
||||
|
||||
.notice > svg {
|
||||
flex-shrink: 0;
|
||||
color: var(--cpd-color-icon-secondary);
|
||||
}
|
||||
|
||||
.refusal {
|
||||
margin: var(--cpd-space-2x) var(--cpd-space-4x) 0;
|
||||
}
|
||||
|
||||
.selfPreview {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
aspect-ratio: 16 / 9;
|
||||
margin-block-end: var(--cpd-space-2x);
|
||||
overflow: clip;
|
||||
border-start-start-radius: var(--cpd-space-3x);
|
||||
border-start-end-radius: var(--cpd-space-3x);
|
||||
background: var(--cpd-color-bg-subtle-secondary);
|
||||
color: var(--cpd-color-icon-secondary);
|
||||
}
|
||||
|
||||
.selfPreview > video {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
inline-size: 100%;
|
||||
block-size: 100%;
|
||||
object-fit: cover;
|
||||
/* Rounded itself too: a video can be drawn on a layer of its own, which the
|
||||
frame's rounded clip doesn't always reach. */
|
||||
border-radius: inherit;
|
||||
}
|
||||
|
||||
/* Up to the menu's frame, over the padding at its top. */
|
||||
.selfPreviewPinned,
|
||||
.deviceListLeadsWithPreview {
|
||||
margin-block-start: calc(-1 * var(--cpd-space-5x));
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,541 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import {
|
||||
useCallback,
|
||||
useId,
|
||||
useRef,
|
||||
useState,
|
||||
type CSSProperties,
|
||||
type FC,
|
||||
useEffect,
|
||||
type ReactElement,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import {
|
||||
Alert,
|
||||
Button,
|
||||
InlineSpinner,
|
||||
Menu,
|
||||
MenuItem,
|
||||
MenuScrollArea,
|
||||
MenuScrollSection,
|
||||
RadioInput,
|
||||
} from "@vector-im/compound-web";
|
||||
import {
|
||||
ChevronUpIcon,
|
||||
ChevronDownIcon,
|
||||
InfoIcon,
|
||||
SpinnerIcon,
|
||||
} from "@vector-im/compound-design-tokens/assets/web/icons";
|
||||
import classNames from "classnames";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { distinctUntilChanged, map } from "rxjs";
|
||||
|
||||
import styles from "./MediaMuteAndSwitchButton.module.css";
|
||||
import { MicButton, VideoButton } from "../button";
|
||||
import {
|
||||
type AudioOutputDeviceLabel,
|
||||
type DeviceLabel,
|
||||
} from "../state/MediaDevices";
|
||||
import { useMediaDevices } from "../MediaDevicesContext";
|
||||
import { useRootElement } from "../RootElementContext";
|
||||
import { observeElementSize$ } from "../utils/elementSize";
|
||||
import { LiveMicrophoneLevelMeter } from "./MicrophoneLevelMeter";
|
||||
import {
|
||||
BackgroundEffectGrid,
|
||||
type BackgroundEffectGridItem,
|
||||
} from "./BackgroundEffectGrid";
|
||||
|
||||
export interface MenuOptions {
|
||||
label: DeviceLabel | AudioOutputDeviceLabel;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface MediaMuteAndSwitchButtonProps {
|
||||
/** If the Mute button is enabled */
|
||||
enabled?: boolean;
|
||||
/** Callback if the mute button is clicked */
|
||||
onMuteClick?: () => void;
|
||||
/** True while mute/unmute operation is syncing. */
|
||||
busy?: boolean;
|
||||
iconsAndLabels: "video" | "audio";
|
||||
/** The options available for the media device selector modal */
|
||||
options?: MenuOptions[];
|
||||
/** The option that will currently be rendered as the selected option */
|
||||
selectedOption?: string;
|
||||
/** Output (speaker) devices. Audio menu only. */
|
||||
outputOptions?: MenuOptions[];
|
||||
/** The output option currently rendered as selected */
|
||||
selectedOutputOption?: string;
|
||||
/** Picks an output device. Undefined disables the speaker section. */
|
||||
onSelectOutput?: (id: string) => void;
|
||||
/** Camera menu only. None or an empty list leaves the section out. */
|
||||
backgroundEffects?: BackgroundEffectGridItem[];
|
||||
selectedBackgroundEffect?: string;
|
||||
/** Undefined where effects can't run: all but no effect are disabled. */
|
||||
onSelectBackgroundEffect?: (id: string) => void;
|
||||
/** What the user should know before choosing an effect, if anything. */
|
||||
backgroundEffectNotice?: string;
|
||||
/** Whether the first effect chosen is still being prepared. */
|
||||
backgroundEffectSettling?: boolean;
|
||||
/** Called with the file chosen from the add tile. Omit to leave it out. */
|
||||
onAddBackgroundImage?: (file: File) => void;
|
||||
/** Removes a removable effect, unless it is the selected one. */
|
||||
onRemoveBackgroundEffect?: (id: string) => void;
|
||||
/** Why the last file offered couldn't be used; a new object each time. */
|
||||
backgroundImageRefusal?: { text: string };
|
||||
/** The user's own camera, shown at the top of the camera menu. */
|
||||
selfPreview?: ReactNode;
|
||||
/**
|
||||
* For any toggle and option this method will be called.
|
||||
* So toggles need to be implemented by listening here and setting the right toggle item to `enabled`
|
||||
*/
|
||||
onSelect?: (id: string) => void;
|
||||
}
|
||||
|
||||
/** Id of the placeholder "Default" row, shown when the platform lists no outputs. */
|
||||
const DEFAULT_OUTPUT_ID = "default";
|
||||
|
||||
/** Largest share of the call area's height the device list may take. */
|
||||
const LIST_SHARE_OF_CALL = 0.6;
|
||||
|
||||
/** Smallest device list height in px, so a short call still shows more than one device. */
|
||||
const MIN_LIST_HEIGHT = 160;
|
||||
|
||||
/** The menu's width as its stylesheet sets it, to place the preview before
|
||||
the menu is drawn. */
|
||||
const MENU_WIDTH = 296;
|
||||
|
||||
/** Space kept between the menu and the call area's sides. */
|
||||
const MENU_MARGIN = 16;
|
||||
|
||||
/** Below the preview, as its stylesheet draws it. */
|
||||
const PREVIEW_GAP = 12;
|
||||
|
||||
export const MediaMuteAndSwitchButton: FC<MediaMuteAndSwitchButtonProps> = ({
|
||||
enabled,
|
||||
busy,
|
||||
onMuteClick,
|
||||
iconsAndLabels,
|
||||
options,
|
||||
selectedOption,
|
||||
outputOptions,
|
||||
selectedOutputOption,
|
||||
onSelectOutput,
|
||||
backgroundEffects,
|
||||
selectedBackgroundEffect,
|
||||
onSelectBackgroundEffect,
|
||||
backgroundEffectNotice,
|
||||
backgroundEffectSettling,
|
||||
onAddBackgroundImage,
|
||||
onRemoveBackgroundEffect,
|
||||
backgroundImageRefusal,
|
||||
selfPreview,
|
||||
onSelect,
|
||||
}) => {
|
||||
// Requested but not yet selected. Keyed by kind too, since Chrome uses
|
||||
// "default" for both an input and an output.
|
||||
const [plannedSelection, setPlannedSelection] = useState<{
|
||||
kind: "input" | "output";
|
||||
id: string;
|
||||
} | null>(null);
|
||||
const [menuOpen, setMenuOpen] = useState(false);
|
||||
// The file picker takes the focus, which the menu reads as a click
|
||||
// elsewhere; it is held open until the picker is done.
|
||||
const choosingFile = useRef(false);
|
||||
const fileInput = useRef<HTMLInputElement | null>(null);
|
||||
// A refusal is about the attempt that caused it: shown until dismissed or
|
||||
// until the menu closes, and again for the next one.
|
||||
const [seenRefusal, setSeenRefusal] = useState<object>();
|
||||
const refusal =
|
||||
backgroundImageRefusal !== seenRefusal ? backgroundImageRefusal : undefined;
|
||||
const onOpenChange = useCallback(
|
||||
(open: boolean): void => {
|
||||
if (!open && choosingFile.current) return;
|
||||
setMenuOpen(open);
|
||||
// Drop a request that never arrived.
|
||||
if (!open) setPlannedSelection(null);
|
||||
if (!open) setSeenRefusal(backgroundImageRefusal);
|
||||
},
|
||||
[backgroundImageRefusal],
|
||||
);
|
||||
// Scrolled to as it appears, as the list may be scrolled away from it.
|
||||
const scrollIntoView = useCallback((element: HTMLElement | null): void => {
|
||||
element?.scrollIntoView({ block: "nearest" });
|
||||
}, []);
|
||||
const watchFileInput = useCallback(
|
||||
(input: HTMLInputElement | null): (() => void) | undefined => {
|
||||
fileInput.current = input;
|
||||
if (input === null) return;
|
||||
// Dismissing the picker fires cancel, which React doesn't type.
|
||||
const done = (): void => {
|
||||
choosingFile.current = false;
|
||||
};
|
||||
input.addEventListener("cancel", done);
|
||||
return (): void => input.removeEventListener("cancel", done);
|
||||
},
|
||||
[],
|
||||
);
|
||||
const isBusy = busy ?? false;
|
||||
const { t } = useTranslation();
|
||||
const devices = useMediaDevices();
|
||||
const noticeId = useId();
|
||||
|
||||
/**
|
||||
* Records on the menu whether the keyboard or the pointer moved focus, for
|
||||
* the focus ring. `:focus-visible` can't tell: Radix focuses whatever the
|
||||
* pointer is over. Listened for on the menu, not the document, so a second
|
||||
* Element Call on the page doesn't answer for this one.
|
||||
*/
|
||||
const trackFocusSource = useCallback(
|
||||
(list: HTMLDivElement | null): (() => void) | undefined => {
|
||||
const menu = list?.closest<HTMLElement>('[role="menu"]');
|
||||
if (menu === null || menu === undefined) return;
|
||||
const record = (source: "keyboard" | "pointer"): void => {
|
||||
menu.dataset.focusSource = source;
|
||||
};
|
||||
record("pointer");
|
||||
const usedKeyboard = (): void => record("keyboard");
|
||||
const usedPointer = (): void => record("pointer");
|
||||
menu.addEventListener("keydown", usedKeyboard, true);
|
||||
menu.addEventListener("pointermove", usedPointer, true);
|
||||
return (): void => {
|
||||
menu.removeEventListener("keydown", usedKeyboard, true);
|
||||
menu.removeEventListener("pointermove", usedPointer, true);
|
||||
};
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
// Measured on the call area: CSS can't size the portalled menu against it.
|
||||
const rootElement = useRootElement();
|
||||
const [listShare, setListShare] = useState<number>();
|
||||
const [previewBlock, setPreviewBlock] = useState(0);
|
||||
useEffect(() => {
|
||||
if (!menuOpen) return;
|
||||
// Followed, since a host can resize the call while the menu is open.
|
||||
const subscription = observeElementSize$(rootElement)
|
||||
.pipe(
|
||||
map(({ width, height }) => ({
|
||||
height: Math.round(height * LIST_SHARE_OF_CALL),
|
||||
width: Math.min(MENU_WIDTH, Math.round(width - 2 * MENU_MARGIN)),
|
||||
})),
|
||||
distinctUntilChanged(
|
||||
(a, b) => a.height === b.height && a.width === b.width,
|
||||
),
|
||||
)
|
||||
.subscribe(({ height, width }) => {
|
||||
setListShare(height);
|
||||
setPreviewBlock(Math.round((width * 9) / 16) + PREVIEW_GAP);
|
||||
});
|
||||
return (): void => subscription.unsubscribe();
|
||||
}, [menuOpen, rootElement]);
|
||||
|
||||
// Paid for out of the list's share, so a preview makes the menu no taller.
|
||||
const hasPreview =
|
||||
iconsAndLabels === "video" &&
|
||||
selfPreview !== undefined &&
|
||||
listShare !== undefined;
|
||||
const previewPinned =
|
||||
hasPreview && listShare - previewBlock >= MIN_LIST_HEIGHT;
|
||||
const listMaxHeight =
|
||||
listShare === undefined
|
||||
? undefined
|
||||
: Math.max(
|
||||
MIN_LIST_HEIGHT,
|
||||
listShare - (previewPinned ? previewBlock : 0),
|
||||
);
|
||||
const preview = hasPreview && (
|
||||
<div
|
||||
aria-hidden
|
||||
className={classNames(styles.selfPreview, {
|
||||
[styles.selfPreviewPinned]: previewPinned,
|
||||
})}
|
||||
>
|
||||
{backgroundEffectSettling ? <InlineSpinner size={32} /> : selfPreview}
|
||||
</div>
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (menuOpen) devices.requestDeviceNames(); // No-op after the first call
|
||||
}, [menuOpen, devices]);
|
||||
|
||||
const audioMeter = iconsAndLabels === "audio" && (
|
||||
<LiveMicrophoneLevelMeter
|
||||
deviceId={selectedOption}
|
||||
// Only while open, so the microphone isn't held all call.
|
||||
active={menuOpen}
|
||||
/>
|
||||
);
|
||||
const MuteButton = audioMeter ? MicButton : VideoButton;
|
||||
const button = (
|
||||
<MuteButton
|
||||
enabled={enabled ?? false}
|
||||
busy={isBusy}
|
||||
onClick={(e) => {
|
||||
onMuteClick?.();
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}}
|
||||
disabled={isBusy || onMuteClick === undefined}
|
||||
data-testid={audioMeter ? "incall_mute" : "incall_videomute"}
|
||||
/>
|
||||
);
|
||||
|
||||
const showEffects =
|
||||
iconsAndLabels === "video" && (backgroundEffects?.length ?? 0) > 0;
|
||||
|
||||
let optionsButtonLabel: string;
|
||||
let menuTitle: string;
|
||||
let numberedLabel: (number: number) => string;
|
||||
switch (iconsAndLabels) {
|
||||
case "video":
|
||||
optionsButtonLabel = t("settings.devices.camera");
|
||||
menuTitle = t("settings.devices.camera_source");
|
||||
numberedLabel = (n): string =>
|
||||
t("settings.devices.camera_numbered", { n });
|
||||
break;
|
||||
case "audio":
|
||||
optionsButtonLabel = t("settings.devices.microphone");
|
||||
menuTitle = t("settings.devices.mic_source");
|
||||
numberedLabel = (n): string =>
|
||||
t("settings.devices.microphone_numbered", { n });
|
||||
break;
|
||||
}
|
||||
|
||||
const labelText = (
|
||||
label: MenuOptions["label"],
|
||||
numbered: (n: number) => string,
|
||||
): string => {
|
||||
switch (label.type) {
|
||||
case "name":
|
||||
return label.name;
|
||||
case "number":
|
||||
return numbered(label.number);
|
||||
case "default":
|
||||
return label.name === null
|
||||
? t("settings.devices.default")
|
||||
: t("settings.devices.default_named_plain", { name: label.name });
|
||||
case "speaker":
|
||||
return t("settings.devices.loudspeaker");
|
||||
case "earpiece":
|
||||
return t("settings.devices.handset");
|
||||
}
|
||||
};
|
||||
|
||||
// Nothing can be picked while a requested device is on offer but not yet
|
||||
// selected, so a second request can't overtake it.
|
||||
const plannedOutput = plannedSelection?.kind === "output";
|
||||
const selectedOfPlannedKind = plannedOutput
|
||||
? selectedOutputOption
|
||||
: selectedOption;
|
||||
const offeredOfPlannedKind = plannedOutput ? outputOptions : options;
|
||||
const settling =
|
||||
plannedSelection !== null &&
|
||||
plannedSelection.id !== selectedOfPlannedKind &&
|
||||
offeredOfPlannedKind?.some(({ id }) => id === plannedSelection.id) === true;
|
||||
|
||||
// Safari lists no outputs: show a disabled, selected Default rather than an
|
||||
// empty section.
|
||||
const noOutputsListed = outputOptions?.length === 0;
|
||||
const speakerOptions: MenuOptions[] | undefined = noOutputsListed
|
||||
? [{ id: DEFAULT_OUTPUT_ID, label: { type: "default", name: null } }]
|
||||
: outputOptions;
|
||||
const selectedSpeaker = noOutputsListed
|
||||
? DEFAULT_OUTPUT_ID
|
||||
: selectedOutputOption;
|
||||
|
||||
const deviceItems = (
|
||||
kind: "input" | "output",
|
||||
items: MenuOptions[] | undefined,
|
||||
selected: string | undefined,
|
||||
select: ((id: string) => void) | undefined,
|
||||
numbered: (n: number) => string,
|
||||
): ReactElement[] => {
|
||||
const list = items ?? [];
|
||||
// Disabled rather than hidden, so the menu keeps its shape.
|
||||
const disabled = select === undefined || list.length <= 1 || settling;
|
||||
return list.map(({ label, id }) => (
|
||||
<MenuItem
|
||||
// A radio input may not sit inside a button.
|
||||
as="div"
|
||||
hideChevron
|
||||
disabled={disabled}
|
||||
label={labelText(label, numbered)}
|
||||
Icon={
|
||||
// Inert, not aria-hidden: aria-hidden alone leaves it focusable.
|
||||
<span inert>
|
||||
<RadioInput
|
||||
checked={selected === id}
|
||||
disabled={disabled}
|
||||
// Not readOnly, which mutes the selected fill. The item handles
|
||||
// the click.
|
||||
onChange={(): void => {}}
|
||||
/>
|
||||
</span>
|
||||
}
|
||||
onSelect={(e) => {
|
||||
e.preventDefault();
|
||||
if (id === selected) return;
|
||||
setPlannedSelection({ kind, id });
|
||||
select?.(id);
|
||||
}}
|
||||
key={id}
|
||||
role="menuitemradio"
|
||||
aria-checked={selected === id}
|
||||
>
|
||||
{selected !== id &&
|
||||
plannedSelection?.kind === kind &&
|
||||
plannedSelection.id === id && (
|
||||
<SpinnerIcon
|
||||
width={24}
|
||||
height={24}
|
||||
className={styles.rotate}
|
||||
aria-label={t("settings.devices.activating")}
|
||||
/>
|
||||
)}
|
||||
</MenuItem>
|
||||
));
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classNames({
|
||||
[styles.container]: true,
|
||||
[styles.containerOpen]: menuOpen,
|
||||
})}
|
||||
>
|
||||
{/* The mute button lives inside */}
|
||||
{button}
|
||||
{onAddBackgroundImage !== undefined && (
|
||||
<input
|
||||
ref={watchFileInput}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
hidden
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
// Cleared, so the same file can be chosen twice in a row.
|
||||
e.target.value = "";
|
||||
choosingFile.current = false;
|
||||
if (file) onAddBackgroundImage(file);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<Menu
|
||||
className={styles.menu}
|
||||
// Named for screen readers only: each section has its own heading.
|
||||
title={menuTitle}
|
||||
showTitle={false}
|
||||
open={menuOpen}
|
||||
onOpenChange={onOpenChange}
|
||||
side="top"
|
||||
collisionBoundary={rootElement}
|
||||
collisionPadding={MENU_MARGIN}
|
||||
trigger={
|
||||
<Button
|
||||
iconOnly
|
||||
className={classNames({
|
||||
[styles.menuButton]: true,
|
||||
[styles.chevronIconOpen]: menuOpen,
|
||||
})}
|
||||
Icon={menuOpen ? ChevronUpIcon : ChevronDownIcon}
|
||||
kind={"tertiary"}
|
||||
size="lg"
|
||||
aria-label={optionsButtonLabel}
|
||||
/>
|
||||
}
|
||||
>
|
||||
{previewPinned && preview}
|
||||
<MenuScrollArea
|
||||
ref={trackFocusSource}
|
||||
className={classNames(styles.deviceList, {
|
||||
[styles.deviceListLeadsWithPreview]: hasPreview && !previewPinned,
|
||||
})}
|
||||
data-testid="device-list"
|
||||
style={
|
||||
{
|
||||
"--device-list-max-height":
|
||||
listMaxHeight === undefined ? undefined : `${listMaxHeight}px`,
|
||||
} as CSSProperties
|
||||
}
|
||||
>
|
||||
{hasPreview && !previewPinned && preview}
|
||||
{audioMeter && speakerOptions && (
|
||||
<MenuScrollSection title={t("settings.devices.speaker")}>
|
||||
{deviceItems(
|
||||
"output",
|
||||
speakerOptions,
|
||||
selectedSpeaker,
|
||||
onSelectOutput,
|
||||
(n) => t("settings.devices.speaker_numbered", { n }),
|
||||
)}
|
||||
</MenuScrollSection>
|
||||
)}
|
||||
<MenuScrollSection
|
||||
title={optionsButtonLabel}
|
||||
stickyBottomItem={audioMeter}
|
||||
>
|
||||
{deviceItems(
|
||||
"input",
|
||||
options,
|
||||
selectedOption,
|
||||
onSelect,
|
||||
numberedLabel,
|
||||
)}
|
||||
</MenuScrollSection>
|
||||
{showEffects && (
|
||||
<MenuScrollSection title={t("settings.background_effects_header")}>
|
||||
<BackgroundEffectGrid
|
||||
effects={backgroundEffects ?? []}
|
||||
selected={selectedBackgroundEffect}
|
||||
onSelect={onSelectBackgroundEffect}
|
||||
settling={backgroundEffectSettling}
|
||||
describedBy={
|
||||
backgroundEffectNotice === undefined ? undefined : noticeId
|
||||
}
|
||||
onAdd={
|
||||
onAddBackgroundImage === undefined
|
||||
? undefined
|
||||
: (): void => {
|
||||
choosingFile.current = true;
|
||||
fileInput.current?.click();
|
||||
}
|
||||
}
|
||||
addLabel={t("action.add_background_image")}
|
||||
onRemove={onRemoveBackgroundEffect}
|
||||
removeLabel={t("action.remove")}
|
||||
/>
|
||||
{/* In the list, so the menu grows no taller for it. */}
|
||||
{refusal !== undefined && (
|
||||
<div
|
||||
ref={scrollIntoView}
|
||||
role="none"
|
||||
className={styles.refusal}
|
||||
>
|
||||
<Alert
|
||||
type="critical"
|
||||
title={refusal.text}
|
||||
onClose={(): void => setSeenRefusal(refusal)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{refusal === undefined &&
|
||||
backgroundEffectNotice !== undefined && (
|
||||
<div id={noticeId} role="none" className={styles.notice}>
|
||||
<InfoIcon width={20} height={20} />
|
||||
<span>{backgroundEffectNotice}</span>
|
||||
</div>
|
||||
)}
|
||||
</MenuScrollSection>
|
||||
)}
|
||||
</MenuScrollArea>
|
||||
</Menu>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,62 +0,0 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`MediaMuteAndSwitchButton > renders 1`] = `
|
||||
<div>
|
||||
<div
|
||||
class="_container_e649de"
|
||||
>
|
||||
<button
|
||||
aria-busy="false"
|
||||
aria-checked="false"
|
||||
aria-disabled="true"
|
||||
aria-labelledby="_r_1_"
|
||||
class="_button_1nw83_8 _has-icon_1nw83_60 _icon-only_1nw83_53"
|
||||
data-kind="primary"
|
||||
data-size="lg"
|
||||
data-testid="incall_mute"
|
||||
role="switch"
|
||||
tabindex="0"
|
||||
>
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
fill="currentColor"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
width="24"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
d="M8 8v-.006l6.831 6.832-.002.002 1.414 1.415.003-.003 1.414 1.414-.003.003L20.5 20.5a1 1 0 0 1-1.414 1.414l-3.022-3.022A7.95 7.95 0 0 1 13 19.938V21a1 1 0 0 1-2 0v-1.062A8 8 0 0 1 4 12a1 1 0 1 1 2 0 6 6 0 0 0 8.587 5.415l-1.55-1.55A4.005 4.005 0 0 1 8 12v-1.172L2.086 4.914A1 1 0 0 1 3.5 3.5zm9.417 6.583 1.478 1.477A7.96 7.96 0 0 0 20 12a1 1 0 0 0-2 0c0 .925-.21 1.8-.583 2.583M8.073 5.238l7.793 7.793q.132-.495.134-1.031V6a4 4 0 0 0-7.927-.762"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
aria-disabled="false"
|
||||
aria-expanded="false"
|
||||
aria-haspopup="menu"
|
||||
aria-label="Microphone"
|
||||
class="_button_1nw83_8 _menuButton_e649de _has-icon_1nw83_60 _icon-only_1nw83_53"
|
||||
data-kind="tertiary"
|
||||
data-size="lg"
|
||||
data-state="closed"
|
||||
id="radix-_r_6_"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
type="button"
|
||||
>
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
fill="currentColor"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
width="24"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
d="M12 14.95q-.2 0-.375-.062a.9.9 0 0 1-.325-.213l-4.6-4.6a.95.95 0 0 1-.275-.7q0-.425.275-.7a.95.95 0 0 1 .7-.275q.425 0 .7.275l3.9 3.9 3.9-3.9a.95.95 0 0 1 .7-.275q.425 0 .7.275a.95.95 0 0 1 .275.7.95.95 0 0 1-.275.7l-4.6 4.6q-.15.15-.325.212a1.1 1.1 0 0 1-.375.063"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
Reference in New Issue
Block a user