From db14c025580f77cb8ed1db999d355ba0a9affc97 Mon Sep 17 00:00:00 2001 From: Gabriel20xx Date: Fri, 25 Sep 2026 11:28:19 +0200 Subject: [PATCH] Adjust shared media player controls --- .../src/lib/components/MediaPlayer.svelte | 88 ++++++- .../lib/components/MediaPlayerControls.svelte | 225 ++++++++++++------ 2 files changed, 232 insertions(+), 81 deletions(-) diff --git a/packages/web-ui/src/lib/components/MediaPlayer.svelte b/packages/web-ui/src/lib/components/MediaPlayer.svelte index d8a8c2f..2a43e0b 100644 --- a/packages/web-ui/src/lib/components/MediaPlayer.svelte +++ b/packages/web-ui/src/lib/components/MediaPlayer.svelte @@ -15,9 +15,11 @@ autoplay = false, class: className = '', mediaClass = '', - showSeekButtons = true, + showSeekButtons: _showSeekButtons = true, showVolume = true, showFullscreen = true, + showGraph = false, + graphVisible = false, qualityOptions = [], selectedQuality = '', currentTime: externalCurrentTime = null, @@ -30,6 +32,8 @@ onEnded, onSeek, onSkip, + onQualityChange, + onToggleGraph, endControls, }: { kind?: MediaPlayerKind @@ -39,9 +43,12 @@ autoplay?: boolean class?: string mediaClass?: string + /** @deprecated Seeking is available with the arrow keys and mobile double-tap. */ showSeekButtons?: boolean showVolume?: boolean showFullscreen?: boolean + showGraph?: boolean + graphVisible?: boolean qualityOptions?: MediaPlayerQualityOption[] selectedQuality?: string currentTime?: number | null @@ -54,6 +61,8 @@ onEnded?: (event: Event) => void onSeek?: (value: number) => void onSkip?: (seconds: number) => void + onQualityChange?: (value: string) => void + onToggleGraph?: () => void endControls?: Snippet } = $props() @@ -66,7 +75,9 @@ let muted = $state(false) let fullscreen = $state(false) let controlsVisible = $state(true) + let orientationLocked = $state(false) let controlsHideTimeout: ReturnType | null = null + let orientationLockRequest = 0 const currentTime = $derived(externalCurrentTime ?? internalTime) const duration = $derived(externalDuration ?? internalDuration) @@ -127,6 +138,60 @@ } } + function isMobileDevice() { + return ( + typeof window !== 'undefined' && + (window.matchMedia?.('(pointer: coarse)').matches === true || navigator.maxTouchPoints > 0) + ) + } + + function isWideVideo() { + return Boolean( + mediaElement && + !audio && + mediaElement instanceof HTMLVideoElement && + mediaElement.videoWidth > mediaElement.videoHeight, + ) + } + + async function syncScreenOrientation(nextFullscreen: boolean) { + const request = ++orientationLockRequest + const orientation = + typeof window !== 'undefined' + ? (window.screen?.orientation as + | (ScreenOrientation & { + lock: (orientation: 'landscape') => Promise + }) + | undefined) + : undefined + const shouldLock = nextFullscreen && isMobileDevice() && isWideVideo() + + if (!shouldLock) { + if (orientationLocked) orientation?.unlock() + orientationLocked = false + return + } + + if (!orientation?.lock) return + try { + await orientation.lock('landscape') + if (request !== orientationLockRequest) { + orientation.unlock() + return + } + orientationLocked = true + } catch { + // Orientation locking is not available in every mobile browser. + } + } + + function handleVideoDoubleTap(event: MouseEvent) { + if (audio || !isMobileDevice()) return + const target = event.currentTarget as HTMLVideoElement + const bounds = target.getBoundingClientRect() + skip(event.clientX - bounds.left < bounds.width / 2 ? -10 : 10) + } + function handlePlay(event: Event) { playing = true onPlay?.(event) @@ -148,6 +213,7 @@ internalDuration = Number.isFinite(mediaElement.duration) ? mediaElement.duration : 0 volume = mediaElement.volume muted = mediaElement.muted || mediaElement.volume === 0 + void syncScreenOrientation(fullscreen) onLoadedMetadata?.(event) } @@ -219,6 +285,7 @@ function handleFullscreenChange() { fullscreen = Boolean(document.fullscreenElement) + void syncScreenOrientation(fullscreen) } $effect(() => { @@ -247,16 +314,18 @@ return () => { for (const eventName of activityEvents) playerFrame?.removeEventListener(eventName, handlePlayerActivity) + orientationLockRequest += 1 + if (orientationLocked) { + window.screen?.orientation?.unlock() + orientationLocked = false + } } }) -
+
{#if audio}
@@ -357,6 +430,7 @@ width: 100%; height: 100%; object-fit: contain; + touch-action: manipulation; } .media-player-audio-surface { diff --git a/packages/web-ui/src/lib/components/MediaPlayerControls.svelte b/packages/web-ui/src/lib/components/MediaPlayerControls.svelte index 3b387fe..03953f8 100644 --- a/packages/web-ui/src/lib/components/MediaPlayerControls.svelte +++ b/packages/web-ui/src/lib/components/MediaPlayerControls.svelte @@ -1,5 +1,6 @@
@@ -120,75 +146,75 @@ > {#if playing}{:else}{/if} - {#if showSeekButtons} + {#if showVolume} - + onSkip?.(10)}> + oninput={handleVolume} + /> {/if} {formatTime(currentTime)} / {formatTime(duration)}
- {#if showVolume} - - - {/if} - {#if qualityOptions.length > 0} - - {/if} {@render endControls?.()} - {#if showGraph} - + {#if showGraph || qualityOptions.length > 0} +
+ + {#if settingsOpen} + + {/if} +
{/if} {#if showFullscreen}