From 186e1bdc9f39710dfaefb2bdcdc9accb4e108e5f Mon Sep 17 00:00:00 2001 From: "Timo K." Date: Wed, 18 Nov 2026 16:33:27 +0100 Subject: [PATCH] Split the media menu into a button and per-device context menus MediaButtonWithContextMenu holds the mute button, the chevron and the menu's open state; AudioContextMenu and CameraContextMenu are its content, and AudioMenuButton and CameraMenuButton wire each to its own view model. The view model is split the same way: the audio menu carries the output devices, the camera menu the background effects and the preview. Co-Authored-By: Claude Opus 5.5 --- src/components/AudioContextMenu.stories.tsx | 610 ++++++++++ src/components/AudioContextMenu.test.tsx | 537 +++++++++ src/components/AudioContextMenu.tsx | 103 ++ src/components/AudioMenuButton.stories.tsx | 104 ++ src/components/AudioMenuButton.test.tsx | 94 ++ src/components/AudioMenuButton.tsx | 78 ++ src/components/CallFooter.stories.tsx | 85 +- src/components/CallFooter.tsx | 26 +- src/components/CallFooterViewModel.test.ts | 6 +- src/components/CameraContextMenu.module.css | 59 + ...ries.tsx => CameraContextMenu.stories.tsx} | 640 +---------- src/components/CameraContextMenu.test.tsx | 261 +++++ src/components/CameraContextMenu.tsx | 205 ++++ ...ories.tsx => CameraMenuButton.stories.tsx} | 124 +- src/components/CameraMenuButton.test.tsx | 140 +++ src/components/CameraMenuButton.tsx | 138 +++ src/components/DeviceMenu.module.css | 51 + src/components/DeviceMenu.tsx | 259 +++++ src/components/DeviceMenuStoryFixtures.tsx | 43 + .../MediaButtonWithContextMenu.module.css | 30 + .../MediaButtonWithContextMenu.test.tsx | 93 ++ src/components/MediaButtonWithContextMenu.tsx | 119 ++ src/components/MediaMenuButton.tsx | 124 -- src/components/MediaMenuViewModel.test.ts | 79 +- src/components/MediaMenuViewModel.ts | 256 +++-- .../MediaMuteAndSwitchButton.module.css | 131 --- .../MediaMuteAndSwitchButton.test.tsx | 1021 ----------------- src/components/MediaMuteAndSwitchButton.tsx | 541 --------- .../MediaMuteAndSwitchButton.test.tsx.snap | 62 - 29 files changed, 3265 insertions(+), 2754 deletions(-) create mode 100644 src/components/AudioContextMenu.stories.tsx create mode 100644 src/components/AudioContextMenu.test.tsx create mode 100644 src/components/AudioContextMenu.tsx create mode 100644 src/components/AudioMenuButton.stories.tsx create mode 100644 src/components/AudioMenuButton.test.tsx create mode 100644 src/components/AudioMenuButton.tsx create mode 100644 src/components/CameraContextMenu.module.css rename src/components/{MediaMuteAndSwitchButton.stories.tsx => CameraContextMenu.stories.tsx} (50%) create mode 100644 src/components/CameraContextMenu.test.tsx create mode 100644 src/components/CameraContextMenu.tsx rename src/components/{MediaMenuButton.stories.tsx => CameraMenuButton.stories.tsx} (63%) create mode 100644 src/components/CameraMenuButton.test.tsx create mode 100644 src/components/CameraMenuButton.tsx create mode 100644 src/components/DeviceMenu.module.css create mode 100644 src/components/DeviceMenu.tsx create mode 100644 src/components/DeviceMenuStoryFixtures.tsx create mode 100644 src/components/MediaButtonWithContextMenu.module.css create mode 100644 src/components/MediaButtonWithContextMenu.test.tsx create mode 100644 src/components/MediaButtonWithContextMenu.tsx delete mode 100644 src/components/MediaMenuButton.tsx delete mode 100644 src/components/MediaMuteAndSwitchButton.module.css delete mode 100644 src/components/MediaMuteAndSwitchButton.test.tsx delete mode 100644 src/components/MediaMuteAndSwitchButton.tsx delete mode 100644 src/components/__snapshots__/MediaMuteAndSwitchButton.test.tsx.snap diff --git a/src/components/AudioContextMenu.stories.tsx b/src/components/AudioContextMenu.stories.tsx new file mode 100644 index 000000000..3e1cfc874 --- /dev/null +++ b/src/components/AudioContextMenu.stories.tsx @@ -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 => + (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 ( + + } + label={t("settings.devices.microphone")} + title={t("settings.devices.mic_source")} + > + + + ); +} + +const meta = { + component: AudioMenu, + decorators: [ + (Story): JSX.Element => ( + + + + + + + + ), + ], +} satisfies Meta; + +export default meta; +type Story = StoryObj; + +// 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( + `.${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(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( + `.${deviceMenuStyles.deviceList}`, + )!; + const sticky = await waitFor(() => { + const element = document.body.querySelector( + `.${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( + `.${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(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( + `.${deviceMenuStyles.deviceList}`, + )!; + const group = await menu.findByRole("group", { name: "Microphone" }); + const heading = group.querySelector(":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); +} diff --git a/src/components/AudioContextMenu.test.tsx b/src/components/AudioContextMenu.test.tsx new file mode 100644 index 000000000..794d49d1f --- /dev/null +++ b/src/components/AudioContextMenu.test.tsx @@ -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 ( + + {} } as unknown as MediaDevices} + > + {component} + + + ); +} + +/** The menu as the microphone's button opens it. */ +function AudioMenu(props: AudioContextMenuProps): JSX.Element { + return ( + + + + ); +} + +function renderMenu(component: ReactNode): RenderResult { + return render(withProviders(component)); +} + +async function open(): Promise { + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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(); + const onSelectPressed = vi.fn(); + const onOptionUpdated = vi.fn(); + function Wrapper(): JSX.Element { + const [selectedOption, setSelectedOption] = useState("mic1"); + return ( + { + onSelectPressed(); + void promise.then(() => { + setSelectedOption(id); + onOptionUpdated(); + }); + }} + /> + ); + } + + renderMenu(); + + 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(); + + 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(); + function Wrapper(): JSX.Element { + const [selectedOption, setSelectedOption] = useState("mic1"); + return ( + { + void promise.then(() => setSelectedOption(id)); + }} + outputOptions={speakers} + selectedOutputOption="spk1" + onSelectOutput={vi.fn()} + /> + ); + } + + renderMenu(); + 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( + , + ); + + 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( + , + ); + + 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( + { + commits++; + }} + > + + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + await open(); + await userEvent.click( + screen.getByRole("menuitemradio", { name: "Headset" }), + ); + + expect(onSelectOutput).toHaveBeenCalledWith("spk2"); + }); + + test("shows a single device entry disabled", async () => { + renderMenu( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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"); + }); +}); diff --git a/src/components/AudioContextMenu.tsx b/src/components/AudioContextMenu.tsx new file mode 100644 index 000000000..380c0224d --- /dev/null +++ b/src/components/AudioContextMenu.tsx @@ -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 = ({ + 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 ( + + {speakerOptions && ( + { + plan("output", id); + onSelectOutput(id); + }) + } + numbered={(n) => t("settings.devices.speaker_numbered", { n })} + requested={requested("output")} + locked={settling} + /> + )} + { + 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={ + + } + /> + + ); +}; diff --git a/src/components/AudioMenuButton.stories.tsx b/src/components/AudioMenuButton.stories.tsx new file mode 100644 index 000000000..7edc9b72a --- /dev/null +++ b/src/components/AudioMenuButton.stories.tsx @@ -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 ( + + ); +} + +const meta = { + component: AudioMenuButtonStoryWrapper, + decorators: [ + (Story): JSX.Element => ( + + + + ), + ], +} satisfies Meta; + +export default meta; +type Story = StoryObj; + +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", + ); + }, +}; diff --git a/src/components/AudioMenuButton.test.tsx b/src/components/AudioMenuButton.test.tsx new file mode 100644 index 000000000..a17a7066f --- /dev/null +++ b/src/components/AudioMenuButton.test.tsx @@ -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): AudioMenuViewModel { + return { + ...createStaticViewModel({ + 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( + + {} } as unknown as MediaDevices} + > + + + , + ); +} + +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", + ); + }); +}); diff --git a/src/components/AudioMenuButton.tsx b/src/components/AudioMenuButton.tsx new file mode 100644 index 000000000..240511bed --- /dev/null +++ b/src/components/AudioMenuButton.tsx @@ -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 = ({ 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 ( + + ); + + return ( + { + toggle?.(); + e.preventDefault(); + e.stopPropagation(); + }} + disabled={busy || toggle === undefined} + data-testid="incall_mute" + /> + } + label={t("settings.devices.microphone")} + title={t("settings.devices.mic_source")} + > + + + ); +}; diff --git a/src/components/CallFooter.stories.tsx b/src/components/CallFooter.stories.tsx index caa4096d9..0a0c49a4d 100644 --- a/src/components/CallFooter.stories.tsx +++ b/src/components/CallFooter.stories.tsx @@ -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 }, diff --git a/src/components/CallFooter.tsx b/src/components/CallFooter.tsx index 279ae0b8f..6e41f4d90 100644 --- a/src/components/CallFooter.tsx +++ b/src/components/CallFooter.tsx @@ -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 = ({ } buttons.push( - , - , + , + , ); if (toggleScreenSharing !== undefined) { diff --git a/src/components/CallFooterViewModel.test.ts b/src/components/CallFooterViewModel.test.ts index e66a30abd..3140aefee 100644 --- a/src/components/CallFooterViewModel.test.ts +++ b/src/components/CallFooterViewModel.test.ts @@ -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); }); }); diff --git a/src/components/CameraContextMenu.module.css b/src/components/CameraContextMenu.module.css new file mode 100644 index 000000000..76ac7154c --- /dev/null +++ b/src/components/CameraContextMenu.module.css @@ -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)); +} diff --git a/src/components/MediaMuteAndSwitchButton.stories.tsx b/src/components/CameraContextMenu.stories.tsx similarity index 50% rename from src/components/MediaMuteAndSwitchButton.stories.tsx rename to src/components/CameraContextMenu.stories.tsx index 9e6937d70..725bc60e0 100644 --- a/src/components/MediaMuteAndSwitchButton.stories.tsx +++ b/src/components/CameraContextMenu.stories.tsx @@ -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 => - (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(); @@ -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(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 ( -
+ } + label={t("settings.devices.camera")} + title={t("settings.devices.camera_source")} > - {callArea !== null && ( - {children} - )} -
+ + ); -}; +} const meta = { - component: MediaMuteAndSwitchButton, + component: CameraMenu, decorators: [ (Story): JSX.Element => ( - - - + ), ], -} satisfies Meta; +} satisfies Meta; export default meta; type Story = StoryObj; -// 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( - `.${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 ( - ( - `.${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( - `.${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( - `.${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( - `.${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(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( - `.${styles.deviceList}`, - )!; - const sticky = await waitFor(() => { - const element = document.body.querySelector( - `.${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( - `.${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(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( - `.${styles.deviceList}`, - )!; - const group = await menu.findByRole("group", { name: "Microphone" }); - const heading = group.querySelector(":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, diff --git a/src/components/CameraContextMenu.test.tsx b/src/components/CameraContextMenu.test.tsx new file mode 100644 index 000000000..af92396c1 --- /dev/null +++ b/src/components/CameraContextMenu.test.tsx @@ -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 ( + + {} } as unknown as MediaDevices} + > + + + + + + ); +} + +function renderMenu(props: CameraContextMenuProps): RenderResult { + return render(); +} + +async function open(): Promise { + 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("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"); + }); +}); diff --git a/src/components/CameraContextMenu.tsx b/src/components/CameraContextMenu.tsx new file mode 100644 index 000000000..57b50093d --- /dev/null +++ b/src/components/CameraContextMenu.tsx @@ -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 = ({ + 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(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 && ( +
+ {backgroundEffectSettling ? : selfPreview} +
+ ); + + return ( + <> + {onAddBackgroundImage !== undefined && ( + { + 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} + + {hasPreview && !previewPinned && preview} + { + plan("input", id); + onSelect(id); + }) + } + numbered={(n) => t("settings.devices.camera_numbered", { n })} + requested={requested("input")} + locked={settling} + /> + {backgroundEffects.length > 0 && ( + + { + 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 && ( +
+ +
+ )} + {backgroundImageRefusal === undefined && + backgroundEffectNotice !== undefined && ( +
+ + {backgroundEffectNotice} +
+ )} +
+ )} +
+ + ); +}; diff --git a/src/components/MediaMenuButton.stories.tsx b/src/components/CameraMenuButton.stories.tsx similarity index 63% rename from src/components/MediaMenuButton.stories.tsx rename to src/components/CameraMenuButton.stories.tsx index 2227ab42f..f1ffa88ce 100644 --- a/src/components/MediaMenuButton.stories.tsx +++ b/src/components/CameraMenuButton.stories.tsx @@ -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 ; +function CameraMenuButtonStoryWrapper({ + backgroundEffects, + cameraTrack, + ...device +}: StoryArgs): ReactNode { + const deviceVm = useStaticViewModel(device); + const backgroundEffectsVm = useStaticViewModel(backgroundEffects); + const { cameraTrack$ } = useStaticViewModel({ cameraTrack }); + return ( + + ); } const meta = { - component: MediaMenuButtonStoryWrapper, + component: CameraMenuButtonStoryWrapper, decorators: [ (Story): JSX.Element => ( @@ -38,69 +60,41 @@ const meta = { ), ], -} satisfies Meta; +} satisfies Meta; export default meta; type Story = StoryObj; -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; +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 { 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); diff --git a/src/components/CameraMenuButton.test.tsx b/src/components/CameraMenuButton.test.tsx new file mode 100644 index 000000000..eb6c8ea13 --- /dev/null +++ b/src/components/CameraMenuButton.test.tsx @@ -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 = {}, + effects: Partial = {}, +): CameraMenuViewModel { + return { + ...createStaticViewModel({ + 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({ + 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( + + {} } as unknown as MediaDevices} + > + + + , + ); +} + +async function open(): Promise { + 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", + ); + }); +}); diff --git a/src/components/CameraMenuButton.tsx b/src/components/CameraMenuButton.tsx new file mode 100644 index 000000000..b117ec4c6 --- /dev/null +++ b/src/components/CameraMenuButton.tsx @@ -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 = ({ 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(); + const shownRefusal = refusal !== dismissedRefusal ? refusal : undefined; + + if (options.length === 0) + return ( + + ); + + return ( + { + 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); + }} + > + setDismissedRefusal(refusal)} + selfPreview={} + /> + + ); +}; + +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"); + } +} diff --git a/src/components/DeviceMenu.module.css b/src/components/DeviceMenu.module.css new file mode 100644 index 000000000..f5f902777 --- /dev/null +++ b/src/components/DeviceMenu.module.css @@ -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)); +} diff --git a/src/components/DeviceMenu.tsx b/src/components/DeviceMenu.tsx new file mode 100644 index 000000000..927f5e2ff --- /dev/null +++ b/src/components/DeviceMenu.tsx @@ -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( + lists: Record, +): { + 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 = ({ + 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('[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 ( + + {children} + + ); +}; + +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 = ({ + 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 ( + + {options.map(({ label, id }) => ( + + {}} + /> + + } + onSelect={(e) => { + e.preventDefault(); + if (id === selected) return; + onSelect?.(id); + }} + key={id} + role="menuitemradio" + aria-checked={selected === id} + > + {selected !== id && requested === id && ( + + )} + + ))} + + ); +}; diff --git a/src/components/DeviceMenuStoryFixtures.tsx b/src/components/DeviceMenuStoryFixtures.tsx new file mode 100644 index 000000000..0ae4f1c08 --- /dev/null +++ b/src/components/DeviceMenuStoryFixtures.tsx @@ -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(null); + return ( +
+ {callArea !== null && ( + {children} + )} +
+ ); +}; + +/** 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; +} diff --git a/src/components/MediaButtonWithContextMenu.module.css b/src/components/MediaButtonWithContextMenu.module.css new file mode 100644 index 000000000..1bde067c3 --- /dev/null +++ b/src/components/MediaButtonWithContextMenu.module.css @@ -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; +} diff --git a/src/components/MediaButtonWithContextMenu.test.tsx b/src/components/MediaButtonWithContextMenu.test.tsx new file mode 100644 index 000000000..f77b3bfa7 --- /dev/null +++ b/src/components/MediaButtonWithContextMenu.test.tsx @@ -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 ( + <> + + + + ); +} + +function renderButton({ + requestDeviceNames = vi.fn(), + onOpenChange, +}: { + requestDeviceNames?: () => void; + onOpenChange?: (open: boolean) => void; +} = {}): void { + render( + + + Mute} + label="Options" + title="Options menu" + onOpenChange={onOpenChange} + > + + + + , + ); +} + +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(); + }); +}); diff --git a/src/components/MediaButtonWithContextMenu.tsx b/src/components/MediaButtonWithContextMenu.tsx new file mode 100644 index 000000000..d245cd2e5 --- /dev/null +++ b/src/components/MediaButtonWithContextMenu.tsx @@ -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 = ({ + 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 ( +
+ {muteButton} + + } + > + {children} + +
+ ); +}; diff --git a/src/components/MediaMenuButton.tsx b/src/components/MediaMenuButton.tsx deleted file mode 100644 index bf64078f5..000000000 --- a/src/components/MediaMenuButton.tsx +++ /dev/null @@ -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 = ({ 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 ( - - ); - } - - return kind === "audio" ? ( - - ) : ( - } - backgroundImageRefusal={refusalMessage} - backgroundEffectNotice={noticeText} - /> - ); -}; diff --git a/src/components/MediaMenuViewModel.test.ts b/src/components/MediaMenuViewModel.test.ts index 239e6cdd4..2f673ddae 100644 --- a/src/components/MediaMenuViewModel.test.ts +++ b/src/components/MediaMenuViewModel.test.ts @@ -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; camera$?: Observable; -} = {}): 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"); }); diff --git a/src/components/MediaMenuViewModel.ts b/src/components/MediaMenuViewModel.ts index 6bb9628a6..dbd7b22dd 100644 --- a/src/components/MediaMenuViewModel.ts +++ b/src/components/MediaMenuViewModel.ts @@ -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; +/** 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; +/** 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 & { + readonly kind: "audio"; + readonly output: ViewModel; +}; + +export type CameraMenuViewModel = ViewModel & { + readonly kind: "video"; + readonly backgroundEffects: ViewModel; + /** The camera to preview, with the selected effect; null while it is off. */ + readonly cameraTrack$: Behavior; +}; function options$( disableSwitcher$: Behavior, @@ -126,10 +127,10 @@ export function createAudioMenuViewModel( muteStates: MuteStates, mediaDevices: MediaDevices, disableSwitcher$: Behavior, -): 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, backgroundEffectsState$: Observable, camera$: Observable, -): 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, - ), - ), - ), }; } diff --git a/src/components/MediaMuteAndSwitchButton.module.css b/src/components/MediaMuteAndSwitchButton.module.css deleted file mode 100644 index 2e8c60f3f..000000000 --- a/src/components/MediaMuteAndSwitchButton.module.css +++ /dev/null @@ -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)); -} diff --git a/src/components/MediaMuteAndSwitchButton.test.tsx b/src/components/MediaMuteAndSwitchButton.test.tsx deleted file mode 100644 index 7f77f6dd8..000000000 --- a/src/components/MediaMuteAndSwitchButton.test.tsx +++ /dev/null @@ -1,1021 +0,0 @@ -/* -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, onTestFinished, test, vi } from "vitest"; -import { axe } from "vitest-axe"; -import { - act, - fireEvent, - 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 { - MediaMuteAndSwitchButton, - type MenuOptions, -} from "./MediaMuteAndSwitchButton"; -import { type BackgroundEffectGridItem } from "./BackgroundEffectGrid"; -import { MediaDevicesContext } from "../MediaDevicesContext"; -import { type MediaDevices } from "../state/MediaDevices"; -import { restoreAudioCapture, stubAudioCapture } from "../utils/test"; - -const effects: BackgroundEffectGridItem[] = [ - { id: "none", kind: "none" }, - { id: "blur", kind: "blur" }, - { id: "image:arc", kind: "image", imageUrl: "" }, - { id: "image:glow", kind: "image", imageUrl: "" }, -]; - -interface RenderOptions { - requestDeviceNames: () => void; -} - -function withProviders( - component: ReactNode, - { requestDeviceNames = (): void => {} }: Partial = {}, -): JSX.Element { - return ( - - - {component} - - - ); -} - -function renderComponent( - component: ReactNode, - options: Partial = {}, -): RenderResult { - return render(withProviders(component, options)); -} - -describe("MediaMuteAndSwitchButton", () => { - // Only one test stubs the capture; don't let it leak into the rest. - afterEach(restoreAudioCapture); - - test("renders", () => { - const { container } = renderComponent( - - - , - ); - expect(container).toMatchSnapshot(); - }); - - test("renders correct audio and video labels", () => { - const renderLabels = ( - type: "video" | "audio", - enabled: boolean, - ): RenderResult => { - return renderComponent( - , - ); - }; - const renderAudioEndabled = renderLabels("audio", true); - const renderAudioDisabled = renderLabels("audio", false); - const renderVideoEnabled = renderLabels("video", true); - const renderVideoDisabled = renderLabels("video", false); - - expect( - renderAudioEndabled.getByRole("switch", { name: "Mute microphone" }), - ).toBeInTheDocument(); - expect( - renderAudioDisabled.getByRole("switch", { name: "Unmute microphone" }), - ).toBeInTheDocument(); - expect( - renderVideoEnabled.getByRole("switch", { name: "Start video" }), - ).toBeInTheDocument(); - expect( - renderVideoDisabled.getByRole("switch", { name: "Stop video" }), - ).toBeInTheDocument(); - }); - - test("calls mute on mute press", async () => { - const user = userEvent.setup(); - const onMute = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("switch", { name: "Mute microphone" })); - - expect(onMute).toHaveBeenCalled(); - }); - - test("disables mute button while busy", async () => { - const user = userEvent.setup(); - const onMute = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - const muteButton = getByRole("switch", { name: "Mute microphone" }); - expect(muteButton).toHaveAttribute("aria-disabled", "true"); - expect(muteButton).toHaveAttribute("aria-busy", "true"); - - await user.click(muteButton); - expect(onMute).not.toHaveBeenCalled(); - }); - - test("disables video button while busy", async () => { - const user = userEvent.setup(); - const onMute = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - const videoButton = getByRole("switch", { name: "Stop video" }); - expect(videoButton).toHaveAttribute("aria-disabled", "true"); - expect(videoButton).toHaveAttribute("aria-busy", "true"); - - await user.click(videoButton); - expect(onMute).not.toHaveBeenCalled(); - }); - - test("requests device names when opened", async () => { - const user = userEvent.setup(); - const requestDeviceNames = vi.fn(); - renderComponent( - , - { requestDeviceNames }, - ); - - expect(requestDeviceNames).not.toHaveBeenCalled(); - await user.click(screen.getByRole("button", { name: "Microphone" })); - expect(requestDeviceNames).toHaveBeenCalled(); - }); - - test("shows numbered devices correctly", async () => { - const user = userEvent.setup(); - renderComponent( - <> - - - , - ); - - await user.click(screen.getByRole("button", { name: "Microphone" })); - screen.getByRole("menuitemradio", { name: "Microphone 1" }); - screen.getByRole("menuitemradio", { name: "Microphone 2" }); - await user.keyboard("[Escape]"); - await user.click(screen.getByRole("button", { name: "Camera" })); - screen.getByRole("menuitemradio", { name: "Camera 1" }); - screen.getByRole("menuitemradio", { name: "Camera 2" }); - }); - - test("calls select callback on menu click", async () => { - const user = userEvent.setup(); - const onSelect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - await user.click( - screen.getByRole("menuitemradio", { name: "Microphone 2" }), - ); - - expect(onSelect).toHaveBeenCalledWith("mic2"); - }); - test("does not call select callback on already selected menu click", async () => { - const user = userEvent.setup(); - const onSelect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - await user.click( - screen.getByRole("menuitemradio", { name: "Microphone 1" }), - ); - - expect(onSelect).not.toHaveBeenCalled(); - }); - - test("renders menu spinner until selection updates for the component", async () => { - const user = userEvent.setup(); - const { promise, resolve } = Promise.withResolvers(); - const onSelectPressed = vi.fn(); - const onOptionUpdated = vi.fn(); - function Wrapper(): JSX.Element { - const [selectedOption, setSelectedOption] = useState("mic1"); - return ( - { - onSelectPressed(); - void promise.then(() => { - setSelectedOption(id); - onOptionUpdated(); - }); - }} - /> - ); - } - - const { getByRole } = renderComponent(); - - await user.click(getByRole("button", { name: "Microphone" })); - await user.click( - screen.getByRole("menuitemradio", { name: "Microphone 2" }), - ); - - expect(onSelectPressed).toHaveBeenCalled(); - expect(onOptionUpdated).not.toHaveBeenCalled(); - // After clicking, plannedSelection="mic2" but selectedOption is still "mic1", - // so mic2 should be in an activating state - 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("background effects are one keyboard-operable labelled choice", async () => { - const user = userEvent.setup(); - const onSelectBackgroundEffect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(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 user = userEvent.setup(); - const onSelectBackgroundEffect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Camera" })); - await user.click(screen.getByRole("menuitemradio", { name: "Blur" })); - expect(onSelectBackgroundEffect).not.toHaveBeenCalled(); - }); - - test("added images are chosen like shipped ones", async () => { - const user = userEvent.setup(); - const onSelectBackgroundEffect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Camera" })); - const section = screen.getByRole("group", { name: "Background effects" }); - await user.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(() => {}); - try { - const { getByRole, container } = renderComponent( - , - ); - - await user.click(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( - container.querySelector("input[type=file]")!, - { target: { files: [file] } }, - ); - expect(onAddBackgroundImage).toHaveBeenCalledWith(file); - screen.getByRole("menuitemradio", { name: "None", checked: true }); - } finally { - pick.mockRestore(); - } - }); - - test("a refusal can be dismissed and does not outlive the menu", async () => { - const user = userEvent.setup(); - // 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 menu = (refusal?: { text: string }): JSX.Element => - withProviders( - , - ); - const { rerender, getByRole } = render(menu()); - await user.click(getByRole("button", { name: "Camera" })); - - rerender(menu({ text: "That file is not a supported image" })); - expect( - screen.getByText("That file is not a supported image"), - ).toBeVisible(); - screen.getByRole("menuitemradio", { name: "Blur", checked: true }); - await user.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. - rerender(menu({ text: "That file is not a supported image" })); - expect( - screen.getByText("That file is not a supported image"), - ).toBeVisible(); - - await user.keyboard("{Escape}"); - await user.click(getByRole("button", { name: "Camera" })); - expect(screen.queryByText("That file is not a supported image")).toBeNull(); - }); - - 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)"); - try { - const user = userEvent.setup(); - const onSelectBackgroundEffect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Camera" })); - 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"); - } finally { - userAgent.mockRestore(); - } - }); - - test("marks the selected menu item as checked", async () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - 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 user = userEvent.setup(); - const { promise, resolve } = Promise.withResolvers(); - function Wrapper(): JSX.Element { - const [selectedOption, setSelectedOption] = useState("mic1"); - return ( - { - void promise.then(() => setSelectedOption(id)); - }} - outputOptions={[ - { label: { type: "name", name: "Speakers" }, id: "spk1" }, - { label: { type: "name", name: "Headset" }, id: "spk2" }, - ]} - selectedOutputOption="spk1" - onSelectOutput={vi.fn()} - /> - ); - } - - const { getByRole } = renderComponent(); - await user.click(getByRole("button", { name: "Microphone" })); - await user.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 () => { - const user = userEvent.setup(); - // onSelect never reports back, as when a device is removed mid-switch. - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - await user.click( - screen.getByRole("menuitemradio", { name: "Microphone 2" }), - ); - await user.keyboard("{Escape}"); - await user.click(getByRole("button", { name: "Microphone" })); - - 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 user = userEvent.setup(); - const mics: MenuOptions[] = [ - { label: { type: "name", name: "Microphone 1" }, id: "mic1" }, - { label: { type: "name", name: "Microphone 2" }, id: "mic2" }, - ]; - const menu = (options: MenuOptions[]): JSX.Element => ( - - ); - - const { getByRole, rerender } = renderComponent(menu(mics)); - await user.click(getByRole("button", { name: "Microphone" })); - await user.click( - screen.getByRole("menuitemradio", { name: "Microphone 2" }), - ); - - // The second microphone is unplugged before the switch lands. - rerender(withProviders(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(); - const user = userEvent.setup(); - let commits = 0; - - const { getByRole } = renderComponent( - { - commits++; - }} - > - - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - 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("camera menu uses the same selection pattern and keeps the blur toggle", async () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Camera" })); - - 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("marks focus as keyboard-driven only when the keyboard moved it", async () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(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 () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - 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 () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - const menu = document.querySelector('[role="menu"]'); - expect(await axe(menu as HTMLElement)).toHaveNoViolations(); - }); - - test("puts the speaker section above the microphone section", async () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - const speakers = screen.getByRole("menuitemradio", { name: "Speakers" }); - const microphone = screen.getByRole("menuitemradio", { - name: "Microphone 1", - }); - expect( - speakers.compareDocumentPosition(microphone) & - Node.DOCUMENT_POSITION_FOLLOWING, - ).toBeTruthy(); - }); - - test("lists speaker and microphone sections", async () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - 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 user = userEvent.setup(); - const onSelectOutput = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - await user.click(screen.getByRole("menuitemradio", { name: "Headset" })); - - expect(onSelectOutput).toHaveBeenCalledWith("spk2"); - }); - - test("shows a single device entry disabled", async () => { - const user = userEvent.setup(); - const onSelect = vi.fn(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - // 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 () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - const speakers = screen - .getAllByRole("group") - .find((group) => group.getAttribute("aria-label") === "Speaker")!; - const entries = within(speakers).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 () => { - const user = userEvent.setup(); - const { getByRole } = renderComponent( - , - ); - - await user.click(getByRole("button", { name: "Microphone" })); - - 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"); - }); -}); diff --git a/src/components/MediaMuteAndSwitchButton.tsx b/src/components/MediaMuteAndSwitchButton.tsx deleted file mode 100644 index 86be347e2..000000000 --- a/src/components/MediaMuteAndSwitchButton.tsx +++ /dev/null @@ -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 = ({ - 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(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(); - 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('[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(); - 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 && ( -
- {backgroundEffectSettling ? : selfPreview} -
- ); - - useEffect(() => { - if (menuOpen) devices.requestDeviceNames(); // No-op after the first call - }, [menuOpen, devices]); - - const audioMeter = iconsAndLabels === "audio" && ( - - ); - const MuteButton = audioMeter ? MicButton : VideoButton; - const button = ( - { - 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 }) => ( - - {}} - /> - - } - 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 && ( - - )} - - )); - }; - - return ( -
- {/* The mute button lives inside */} - {button} - {onAddBackgroundImage !== undefined && ( - { - 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); - }} - /> - )} - - } - > - {previewPinned && preview} - - {hasPreview && !previewPinned && preview} - {audioMeter && speakerOptions && ( - - {deviceItems( - "output", - speakerOptions, - selectedSpeaker, - onSelectOutput, - (n) => t("settings.devices.speaker_numbered", { n }), - )} - - )} - - {deviceItems( - "input", - options, - selectedOption, - onSelect, - numberedLabel, - )} - - {showEffects && ( - - { - 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 && ( -
- setSeenRefusal(refusal)} - /> -
- )} - {refusal === undefined && - backgroundEffectNotice !== undefined && ( -
- - {backgroundEffectNotice} -
- )} -
- )} -
-
-
- ); -}; diff --git a/src/components/__snapshots__/MediaMuteAndSwitchButton.test.tsx.snap b/src/components/__snapshots__/MediaMuteAndSwitchButton.test.tsx.snap deleted file mode 100644 index 85aeee8ee..000000000 --- a/src/components/__snapshots__/MediaMuteAndSwitchButton.test.tsx.snap +++ /dev/null @@ -1,62 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`MediaMuteAndSwitchButton > renders 1`] = ` -
-
- - -
-
-`;