From 76bf5916fe36e1a786f74bfbd944d7584a558ada Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 20 Nov 2026 21:24:40 +0100 Subject: [PATCH] fix(web-ui): fade MediaPlayer controls when the pointer is over the picture Pointer movement and presses now count as activity anywhere inside the player's box. The picture can sit on a canvas stacked above the frame, where the pointer never reaches the frame's own elements, so the controls did not fade back in. The autoHideControls doc comment now describes the real default. Co-Authored-By: Claude Haiku 5.5 --- .changeset/media-controls-auto-hide.md | 5 + .../src/lib/components/MediaPlayer.svelte | 20 ++- .../web-ui/tests/MediaPlayerAutoHide.test.ts | 151 ++++++++++++++++++ 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 .changeset/media-controls-auto-hide.md create mode 100644 packages/web-ui/tests/MediaPlayerAutoHide.test.ts diff --git a/.changeset/media-controls-auto-hide.md b/.changeset/media-controls-auto-hide.md new file mode 100644 index 0000000..71c4505 --- /dev/null +++ b/.changeset/media-controls-auto-hide.md @@ -0,0 +1,5 @@ +--- +"@gabriel20xx/web-ui": patch +--- + +Fix `MediaPlayer` controls not fading away during playback when the pointer stays still. Pointer movement now counts as activity anywhere over the player, including over a picture drawn on a canvas stacked above the frame, so the controls fade after a few seconds and come back when the pointer moves over the player again. The `autoHideControls` doc comment now describes its real behavior: `true` (the default) fades the controls, `false` keeps them visible. diff --git a/packages/web-ui/src/lib/components/MediaPlayer.svelte b/packages/web-ui/src/lib/components/MediaPlayer.svelte index 228fbe1..bb948fe 100644 --- a/packages/web-ui/src/lib/components/MediaPlayer.svelte +++ b/packages/web-ui/src/lib/components/MediaPlayer.svelte @@ -128,7 +128,10 @@ controls?: boolean /** Renders the controls over the media surface or as a toolbar below it. */ controlsPlacement?: 'overlay' | 'toolbar' - /** Keep controls visible during playback. */ + /** + * Fade the controls out after a few seconds of playback with no pointer or key activity over + * the player. False keeps them visible while playing. + */ autoHideControls?: boolean /** Whether clicks and key presses on the media surface control playback. */ surfaceControls?: boolean @@ -192,6 +195,19 @@ scheduleControlsHide() } + // The picture can sit on a canvas stacked above the frame, where the pointer never reaches the + // frame's own elements. Pointer activity therefore counts anywhere inside the player's box. + function handlePointerActivity(event: PointerEvent) { + if (!controls || !playerFrame) return + const bounds = playerFrame.getBoundingClientRect() + const inside = + event.clientX >= bounds.left && + event.clientX <= bounds.right && + event.clientY >= bounds.top && + event.clientY <= bounds.bottom + if (inside) handlePlayerActivity() + } + function togglePlayback() { if (controlsDisabled) return if (onTogglePlayback) { @@ -506,6 +522,8 @@
{ + const element = document.querySelector('video'); + if (!element) throw new Error('no video element'); + return element; +}; + +const frameElement = (): HTMLElement => { + const element = document.querySelector('.media-player'); + if (!element) throw new Error('no player frame'); + return element; +}; + +const controlsRoot = (): HTMLElement => { + const element = document.querySelector('.media-player-controls'); + if (!element) throw new Error('no controls'); + return element; +}; + +// jsdom has no layout, so the player's box on the page is given by hand. +function placeFrame(left: number, top: number, right: number, bottom: number) { + frameElement().getBoundingClientRect = () => + ({ left, top, right, bottom, width: right - left, height: bottom - top, x: left, y: top, toJSON: () => ({}) }) as DOMRect; +} + +// jsdom does not load or play media, so position, length and rate are set by hand. +function setMedia(element: HTMLVideoElement, media: { currentTime: number; duration: number }) { + Object.defineProperty(element, 'currentTime', { configurable: true, writable: true, value: media.currentTime }); + Object.defineProperty(element, 'duration', { configurable: true, value: media.duration }); + Object.defineProperty(element, 'playbackRate', { configurable: true, writable: true, value: 1 }); +} + +describe('MediaPlayer controls auto-hide', () => { + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it('fades the controls after a playing video has been idle, with no pointer movement', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip' } }); + const element = videoElement(); + setMedia(element, { currentTime: 0, duration: 100 }); + + await fireEvent.play(element); + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(false); + + vi.advanceTimersByTime(2600); + await tick(); + + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(true); + }); + + it('fades the controls when playback is driven from outside (controlled playing prop)', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip', playing: true } }); + await tick(); + + vi.advanceTimersByTime(2600); + await tick(); + + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(true); + }); + + it('keeps the controls visible while the pointer keeps moving over the player', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip' } }); + const element = videoElement(); + setMedia(element, { currentTime: 0, duration: 100 }); + await fireEvent.play(element); + + vi.advanceTimersByTime(2000); + await fireEvent.mouseMove(frameElement()); + vi.advanceTimersByTime(2000); + await tick(); + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(false); + + vi.advanceTimersByTime(600); + await tick(); + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(true); + }); + + it('shows the controls again on pause and does not hide them while paused', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip' } }); + const element = videoElement(); + setMedia(element, { currentTime: 0, duration: 100 }); + await fireEvent.play(element); + vi.advanceTimersByTime(2600); + await tick(); + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(true); + + await fireEvent.pause(element); + vi.advanceTimersByTime(5000); + await tick(); + + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(false); + }); + + it('never fades the controls when autoHideControls is off', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip', autoHideControls: false } }); + const element = videoElement(); + setMedia(element, { currentTime: 0, duration: 100 }); + await fireEvent.play(element); + + vi.advanceTimersByTime(10000); + await tick(); + + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(false); + }); + + it('counts pointer movement over the player area as activity when an element above the frame takes the events', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip', controlsPlacement: 'toolbar' } }); + const element = videoElement(); + setMedia(element, { currentTime: 0, duration: 100 }); + placeFrame(0, 0, 640, 360); + await fireEvent.play(element); + vi.advanceTimersByTime(2600); + await tick(); + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(true); + + // MediaViewer draws the picture on a canvas beside the frame, so the pointer never reaches the frame itself. + document.body.dispatchEvent(new MouseEvent('pointermove', { bubbles: true, clientX: 320, clientY: 180 })); + await tick(); + + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(false); + }); + + it('ignores pointer movement outside the player area', async () => { + render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip' } }); + const element = videoElement(); + setMedia(element, { currentTime: 0, duration: 100 }); + placeFrame(0, 0, 640, 360); + await fireEvent.play(element); + vi.advanceTimersByTime(2600); + await tick(); + + document.body.dispatchEvent(new MouseEvent('pointermove', { bubbles: true, clientX: 900, clientY: 180 })); + await tick(); + + expect(controlsRoot().classList.contains(HIDDEN_CLASS)).toBe(true); + }); +});