Sync shared snapshot from 76bf591
This commit is contained in:
1 parent
6b84a5d27d
commit
ed3544acfe
3 files changed
+175
-1
No files matched your search
@@ -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.
|
||||||
@@ -128,7 +128,10 @@
|
|||||||
controls?: boolean
|
controls?: boolean
|
||||||
/** Renders the controls over the media surface or as a toolbar below it. */
|
/** Renders the controls over the media surface or as a toolbar below it. */
|
||||||
controlsPlacement?: 'overlay' | 'toolbar'
|
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
|
autoHideControls?: boolean
|
||||||
/** Whether clicks and key presses on the media surface control playback. */
|
/** Whether clicks and key presses on the media surface control playback. */
|
||||||
surfaceControls?: boolean
|
surfaceControls?: boolean
|
||||||
@@ -192,6 +195,19 @@
|
|||||||
scheduleControlsHide()
|
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() {
|
function togglePlayback() {
|
||||||
if (controlsDisabled) return
|
if (controlsDisabled) return
|
||||||
if (onTogglePlayback) {
|
if (onTogglePlayback) {
|
||||||
@@ -506,6 +522,8 @@
|
|||||||
<svelte:window
|
<svelte:window
|
||||||
onfullscreenchange={handleFullscreenChange}
|
onfullscreenchange={handleFullscreenChange}
|
||||||
onkeydown={globalKeyboardControls ? handleKeydown : undefined}
|
onkeydown={globalKeyboardControls ? handleKeydown : undefined}
|
||||||
|
onpointermove={controls ? handlePointerActivity : undefined}
|
||||||
|
onpointerdown={controls ? handlePointerActivity : undefined}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -0,0 +1,151 @@
|
|||||||
|
import { cleanup, fireEvent, render } from '@testing-library/svelte';
|
||||||
|
import { tick } from 'svelte';
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
import MediaPlayer from '../src/lib/components/MediaPlayer.svelte';
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
const SOURCE = 'https://media.example/clip.mp4';
|
||||||
|
const HIDDEN_CLASS = 'media-player-controls-hidden';
|
||||||
|
|
||||||
|
const videoElement = (): HTMLVideoElement => {
|
||||||
|
const element = document.querySelector<HTMLVideoElement>('video');
|
||||||
|
if (!element) throw new Error('no video element');
|
||||||
|
return element;
|
||||||
|
};
|
||||||
|
|
||||||
|
const frameElement = (): HTMLElement => {
|
||||||
|
const element = document.querySelector<HTMLElement>('.media-player');
|
||||||
|
if (!element) throw new Error('no player frame');
|
||||||
|
return element;
|
||||||
|
};
|
||||||
|
|
||||||
|
const controlsRoot = (): HTMLElement => {
|
||||||
|
const element = document.querySelector<HTMLElement>('.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);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user