From f9d736653ccbb6c85e54382da0c59ec8af08a0eb Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Tue, 3 Sep 2024 11:51:16 +0100 Subject: [PATCH] [Video] Hide mouse when inactive (#5094) --- .../VideoEmbedInner/VideoWebControls.tsx | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/view/com/util/post-embeds/VideoEmbedInner/VideoWebControls.tsx b/src/view/com/util/post-embeds/VideoEmbedInner/VideoWebControls.tsx index 2ff2f651..d9b99ef3 100644 --- a/src/view/com/util/post-embeds/VideoEmbedInner/VideoWebControls.tsx +++ b/src/view/com/util/post-embeds/VideoEmbedInner/VideoWebControls.tsx @@ -78,8 +78,8 @@ export function Controls({ const setSubtitlesEnabled = useSetSubtitlesEnabled() const { state: hovered, - onIn: onMouseEnter, - onOut: onMouseLeave, + onIn: onHover, + onOut: onEndHover, } = useInteractionState() const [isFullscreen, toggleFullscreen] = useFullscreen(fullscreenRef) const {state: hasFocus, onIn: onFocus, onOut: onBlur} = useInteractionState() @@ -220,6 +220,25 @@ export function Controls({ onSeek(clamp(currentTime + 5, 0, duration)) }, [onSeek, videoRef]) + const [showCursor, setShowCursor] = useState(true) + const cursorTimeoutRef = useRef>() + const onPointerMoveEmptySpace = useCallback(() => { + setShowCursor(true) + if (cursorTimeoutRef.current) { + clearTimeout(cursorTimeoutRef.current) + } + cursorTimeoutRef.current = setTimeout(() => { + setShowCursor(false) + onEndHover() + }, 2000) + }, [onEndHover]) + const onPointerLeaveEmptySpace = useCallback(() => { + setShowCursor(false) + if (cursorTimeoutRef.current) { + clearTimeout(cursorTimeoutRef.current) + } + }, []) + const showControls = (focused && !playing) || (interactingViaKeypress ? hasFocus : hovered) @@ -236,13 +255,17 @@ export function Controls({ evt.stopPropagation() setInteractingViaKeypress(false) }} - onMouseEnter={onMouseEnter} - onMouseLeave={onMouseLeave} + onPointerEnter={onHover} + onPointerMove={onHover} + onPointerLeave={onEndHover} onFocus={onFocus} onBlur={onBlur} onKeyDown={onKeyDown}> - {active && !showControls && !focused && duration > 0 && ( + {!showControls && !focused && duration > 0 && ( )} + onPointerEnter={onStartHover} + onPointerLeave={onEndHover}>