Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 2x 5x 2x 2x | /** How long the viewer may be idle during a play before the video controls fade out. */
export const CONTROLS_IDLE_MS = 3000;
/** What decides whether a preview's video controls show. */
export interface ControlsVisibility {
/** No pointer, touch, wheel or key input for `CONTROLS_IDLE_MS`. */
readonly idle: boolean;
/** The play is running: not paused, ending or not started. */
readonly playing: boolean;
/** The pointer is over the video controls. */
readonly hovered: boolean;
/** A control holds the keyboard focus. */
readonly focused: boolean;
}
/** The controls fade out only while a play runs and the viewer is idle, away from them, as a video player's do. */
export function resolveControlsHidden(visibility: ControlsVisibility): boolean {
return visibility.idle && visibility.playing && !visibility.hovered && !visibility.focused;
}
/** The video controls inside the preview HUD. */
const CONTROLS = 'be-music-playback-controls';
/** The input that wakes the controls. */
const WAKE_EVENTS = ['pointermove', 'pointerdown', 'wheel', 'keydown', 'touchstart'] as const;
/**
* Hides `hud`'s video controls (`controlsHidden`) after `CONTROLS_IDLE_MS` without input during a play, and shows them
* again on any input. `setPlaying` follows the play's stage; `dispose` removes the listeners and the timer.
*/
export class ControlsIdleController {
private readonly hud: HTMLElement & { controlsHidden: boolean };
private readonly window: Window;
private idle = false;
private playing = false;
private hovered = false;
private timer: ReturnType<typeof setTimeout> | undefined;
public constructor(hud: HTMLElement & { controlsHidden: boolean }, view: Window) {
this.hud = hud;
this.window = view;
for (const name of WAKE_EVENTS) view.addEventListener(name, this.wake, { capture: true, passive: true });
hud.addEventListener('focusin', this.update);
hud.addEventListener('focusout', this.update);
this.wake();
}
public setPlaying(playing: boolean): void {
if (playing === this.playing) return;
this.playing = playing;
this.wake();
}
public dispose(): void {
for (const name of WAKE_EVENTS) this.window.removeEventListener(name, this.wake, { capture: true });
this.hud.removeEventListener('focusin', this.update);
this.hud.removeEventListener('focusout', this.update);
if (this.timer !== undefined) clearTimeout(this.timer);
}
private readonly wake = (event?: Event): void => {
// Where the pointer is, from what it moved over: an element under it can vanish (the play button a click removes)
// without a `pointerout`.
if (event instanceof PointerEvent && event.type === 'pointermove') {
this.hovered = event.composedPath().some((target) => target instanceof Element && target.localName === CONTROLS);
}
this.idle = false;
if (this.timer !== undefined) clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.idle = true;
this.update();
}, CONTROLS_IDLE_MS);
this.update();
};
private readonly update = (): void => {
const focused = this.hud.matches(':focus-within');
this.hud.controlsHidden = resolveControlsHidden({
idle: this.idle,
playing: this.playing,
hovered: this.hovered,
focused,
});
};
}
|