From 90f076f3baf7435e2aa0b133f40c401f0d4d9375 Mon Sep 17 00:00:00 2001 From: Eyejoker Date: Fri, 27 Nov 2026 11:12:53 +0900 Subject: [PATCH] fix(webui): stop duplicate seeks on the video progress bar (#532) * fix(webui): avoid duplicate seek on progress bar tap A single tap/click fired onSeek twice: once on pointer-down and again on pointer-up at the same position. The second seek restarted the native seek right as data began arriving, which stalled playback for several seconds on slow connections. Track pointer movement and only re-seek on release after an actual drag. Co-Authored-By: Claude * fix(webui): suppress synthetic mouse seek after touch on progress bar Mobile browsers dispatch synthetic mousedown/mouseup after touch interactions. The mouse path re-queued a seek to the same position, which restarted the native seek ~500ms later and stalled playback on slow connections. Ignore mouse-down within 700ms of a touch end. Co-Authored-By: Claude * fix(webui): reset progress bar drag state on touchcancel OS gestures (app switch, palm rejection, notification pull) cancel touches without a touchend. The drag/hover state then stayed stuck until the next interaction. Reset state without seeking. Co-Authored-By: Claude * perf(webui): load seek preview video metadata only The hidden 180p preview video used preload="auto", downloading the whole file alongside main playback. On constrained connections it competed for bandwidth exactly when a seek needed data, lengthening rebuffering. Metadata is enough until the user actually scrubs. Co-Authored-By: Claude * test(webui): cover progress bar seeking on click, tap and touch cancel Check that a plain click or tap seeks once, a real drag seeks again on release, synthetic mouse events right after a touch are ignored, a cancelled touch resets without seeking, and the hidden preview video preloads metadata only. Co-Authored-By: Claude * docs(changelog): note the progress bar seek fixes Co-Authored-By: Claude --------- Co-authored-by: phj1081 <5908832+phj1081@users.noreply.github.com> Co-authored-by: Claude --- CHANGELOG.md | 1 + .../viewers/video/progress-bar.test.tsx | 138 ++++++++++++++++++ .../components/viewers/video/progress-bar.tsx | 62 +++++++- 3 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 packages/webui/components/viewers/video/progress-bar.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index cc160854..4605b357 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **video**: Fix playback stalling after tapping or clicking the progress bar on slow connections, and stop the hidden seek preview from downloading the whole video in the background. - **cli**: Fix release publication failing to find staged standalone downloads. ### Changed diff --git a/packages/webui/components/viewers/video/progress-bar.test.tsx b/packages/webui/components/viewers/video/progress-bar.test.tsx new file mode 100644 index 00000000..ec163d94 --- /dev/null +++ b/packages/webui/components/viewers/video/progress-bar.test.tsx @@ -0,0 +1,138 @@ +// @vitest-environment happy-dom +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' +import React from 'react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import ProgressBar from './progress-bar' + +const TOTAL_FRAMES = 1000 + +function renderBar(onSeek = vi.fn()) { + render( + , + ) + const bar = screen.getByTestId('seekbar') + vi.spyOn(bar, 'getBoundingClientRect').mockReturnValue({ + left: 0, + right: 100, + top: 0, + bottom: 10, + width: 100, + height: 10, + x: 0, + y: 0, + toJSON: () => ({}), + } as DOMRect) + return { bar, onSeek } +} + +function touchEvent(type: string, clientX: number, list: 'touches' | 'changedTouches') { + const event = new Event(type, { bubbles: true, cancelable: true }) + Object.defineProperty(event, 'touches', { + value: list === 'touches' ? [{ clientX, clientY: 5 }] : [], + }) + Object.defineProperty(event, 'changedTouches', { + value: list === 'changedTouches' ? [{ clientX, clientY: 5 }] : [], + }) + return event +} + +describe('ProgressBar seeking', () => { + beforeEach(() => { + vi.useFakeTimers({ toFake: ['Date'] }) + vi.setSystemTime(new Date('2026-01-01T00:00:00Z')) + }) + + afterEach(() => { + cleanup() + vi.useRealTimers() + vi.restoreAllMocks() + }) + + it('seeks once for a plain click', () => { + const { bar, onSeek } = renderBar() + + fireEvent.mouseDown(bar, { button: 0, clientX: 40 }) + act(() => { + window.dispatchEvent(new MouseEvent('mouseup', { clientX: 40, clientY: 5 })) + }) + + expect(onSeek).toHaveBeenCalledTimes(1) + expect(onSeek).toHaveBeenLastCalledWith(400) + }) + + it('seeks again on release after a real drag', () => { + const { bar, onSeek } = renderBar() + + fireEvent.mouseDown(bar, { button: 0, clientX: 40 }) + act(() => { + window.dispatchEvent(new MouseEvent('mousemove', { clientX: 60 })) + window.dispatchEvent(new MouseEvent('mouseup', { clientX: 60, clientY: 5 })) + }) + + expect(onSeek.mock.calls.map(([frame]) => frame)).toEqual([400, 600, 600]) + }) + + it('seeks once for a plain tap and ignores the synthetic mouse events that follow', () => { + const { bar, onSeek } = renderBar() + + act(() => { + bar.dispatchEvent(touchEvent('touchstart', 30, 'touches')) + }) + act(() => { + window.dispatchEvent(touchEvent('touchend', 30, 'changedTouches')) + }) + vi.setSystemTime(new Date('2026-01-01T00:00:00.300Z')) + fireEvent.mouseDown(bar, { button: 0, clientX: 30 }) + + expect(onSeek).toHaveBeenCalledTimes(1) + expect(onSeek).toHaveBeenLastCalledWith(300) + }) + + it('accepts a mouse click again once the touch suppression window has passed', () => { + const { bar, onSeek } = renderBar() + + act(() => { + bar.dispatchEvent(touchEvent('touchstart', 30, 'touches')) + }) + act(() => { + window.dispatchEvent(touchEvent('touchend', 30, 'changedTouches')) + }) + vi.setSystemTime(new Date('2026-01-01T00:00:01Z')) + fireEvent.mouseDown(bar, { button: 0, clientX: 70 }) + + expect(onSeek.mock.calls.map(([frame]) => frame)).toEqual([300, 700]) + }) + + it('resets the drag without seeking when the touch is cancelled', () => { + const { bar, onSeek } = renderBar() + + act(() => { + bar.dispatchEvent(touchEvent('touchstart', 30, 'touches')) + }) + act(() => { + window.dispatchEvent(touchEvent('touchmove', 50, 'touches')) + }) + act(() => { + window.dispatchEvent(new Event('touchcancel')) + }) + act(() => { + window.dispatchEvent(touchEvent('touchmove', 80, 'touches')) + }) + + expect(onSeek.mock.calls.map(([frame]) => frame)).toEqual([300, 500]) + }) + + it('loads only metadata for the hidden seek preview video', () => { + renderBar() + + const preview = document.querySelector('video.hidden') + expect(preview).not.toBeNull() + expect(preview?.getAttribute('preload')).toBe('metadata') + }) +}) diff --git a/packages/webui/components/viewers/video/progress-bar.tsx b/packages/webui/components/viewers/video/progress-bar.tsx index fc109a7a..6611e455 100644 --- a/packages/webui/components/viewers/video/progress-bar.tsx +++ b/packages/webui/components/viewers/video/progress-bar.tsx @@ -38,6 +38,15 @@ const ProgressBar: React.FC = ({ const [hoverPosition, setHoverPosition] = useState(null) // 0 to 1 const [isHovering, setIsHovering] = useState(false) const [isDragging, setIsDragging] = useState(false) + // Tracks whether the pointer actually moved during a drag. A plain tap/click + // already seeks on pointer-down; re-seeking to the same frame on release + // restarts the native seek and stalls playback on slow connections. + const didDragMoveRef = useRef(false) + const lastSeekPosRef = useRef(null) + // After a touch interaction, mobile browsers fire synthetic mouse events + // (mousedown/mouseup) for the same tap. Suppress them so one tap does not + // trigger a second seek through the mouse path. + const lastTouchEndAtRef = useRef(0) const onSeekRef = useRef(onSeek) useEffect(() => { @@ -92,8 +101,11 @@ const ProgressBar: React.FC = ({ const handleMouseDown = (e: React.MouseEvent) => { if (e.button !== 0) return + if (Date.now() - lastTouchEndAtRef.current < 700) return e.preventDefault() setIsDragging(true) + didDragMoveRef.current = false + lastSeekPosRef.current = e.clientX const percentage = getPercentageFromEvent(e.clientX) setHoverPosition(percentage) setIsHovering(true) @@ -102,6 +114,8 @@ const ProgressBar: React.FC = ({ const handleTouchStart = (e: React.TouchEvent) => { setIsDragging(true) + didDragMoveRef.current = false + lastSeekPosRef.current = e.touches[0].clientX const percentage = getPercentageFromEvent(e.touches[0].clientX) setHoverPosition(percentage) setIsHovering(true) @@ -112,6 +126,12 @@ const ProgressBar: React.FC = ({ if (!isDragging) return const handleWindowMouseMove = (e: MouseEvent) => { + if (lastSeekPosRef.current !== null && Math.abs(e.clientX - lastSeekPosRef.current) >= 2) { + didDragMoveRef.current = true + lastSeekPosRef.current = e.clientX + } else if (!didDragMoveRef.current) { + return + } const percentage = getPercentageFromEvent(e.clientX) setHoverPosition(percentage) setIsHovering(true) @@ -147,12 +167,27 @@ const ProgressBar: React.FC = ({ setHoverPosition(null) } - const percentage = getPercentageFromEvent(e.clientX) - onSeekRef.current(percentage * totalFrames) + // A plain click already seeked on mousedown; only seek again if the + // pointer actually moved (drag release position differs from start). + if (didDragMoveRef.current) { + const percentage = getPercentageFromEvent(e.clientX) + onSeekRef.current(percentage * totalFrames) + } + didDragMoveRef.current = false + lastSeekPosRef.current = null } const handleWindowTouchMove = (e: TouchEvent) => { if (e.touches.length === 0) return + if ( + lastSeekPosRef.current !== null && + Math.abs(e.touches[0].clientX - lastSeekPosRef.current) >= 2 + ) { + didDragMoveRef.current = true + lastSeekPosRef.current = e.touches[0].clientX + } else if (!didDragMoveRef.current) { + return + } const percentage = getPercentageFromEvent(e.touches[0].clientX) setHoverPosition(percentage) setIsHovering(true) @@ -172,6 +207,7 @@ const ProgressBar: React.FC = ({ const handleWindowTouchEnd = (e: TouchEvent) => { setIsDragging(false) + lastTouchEndAtRef.current = Date.now() const clientX = e.changedTouches.length > 0 ? e.changedTouches[0].clientX : null const clientY = e.changedTouches.length > 0 ? e.changedTouches[0].clientY : null @@ -191,22 +227,40 @@ const ProgressBar: React.FC = ({ setHoverPosition(null) } - if (clientX !== null) { + if (clientX !== null && didDragMoveRef.current) { + // A plain tap already seeked on touchstart; only seek again after a + // real drag so a single tap doesn't restart the seek and stall. const percentage = getPercentageFromEvent(clientX) onSeekRef.current(percentage * totalFrames) } + didDragMoveRef.current = false + lastSeekPosRef.current = null + } + + const handleWindowTouchCancel = () => { + // OS gestures (app switch, palm rejection, notification pull) cancel + // the touch. Reset drag state without seeking so the bar doesn't stay + // stuck in dragging/hover mode. + setIsDragging(false) + setIsHovering(false) + setHoverPosition(null) + didDragMoveRef.current = false + lastSeekPosRef.current = null + lastTouchEndAtRef.current = Date.now() } window.addEventListener('mousemove', handleWindowMouseMove) window.addEventListener('mouseup', handleWindowMouseUp) window.addEventListener('touchmove', handleWindowTouchMove, { passive: true }) window.addEventListener('touchend', handleWindowTouchEnd) + window.addEventListener('touchcancel', handleWindowTouchCancel) return () => { window.removeEventListener('mousemove', handleWindowMouseMove) window.removeEventListener('mouseup', handleWindowMouseUp) window.removeEventListener('touchmove', handleWindowTouchMove) window.removeEventListener('touchend', handleWindowTouchEnd) + window.removeEventListener('touchcancel', handleWindowTouchCancel) } }, [isDragging, totalFrames, fps, getPercentageFromEvent]) @@ -269,7 +323,7 @@ const ProgressBar: React.FC = ({ ref={previewVideoRef} src={previewUrl} className="hidden" - preload="auto" + preload="metadata" muted playsInline />