Skip to content

Commit

Permalink
VideoPlayer controls hide when mouse leaves player
Browse files Browse the repository at this point in the history
  • Loading branch information
killergerbah committed Sep 1, 2024
1 parent 4f399bd commit 4cf8849
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
24 changes: 13 additions & 11 deletions common/app/components/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ interface ControlsProps {
playbackRateEnabled?: boolean;
onAudioTrackSelected: (id: string) => void;
onSeek: (progress: number) => void;
mousePositionRef: MutableRefObject<Point>;
mousePositionRef: MutableRefObject<Point | undefined>;
onShow?: (show: boolean) => void;
onPause: () => void;
onPlay: () => void;
Expand Down Expand Up @@ -676,7 +676,7 @@ export default function Controls({
const [lastCommittedVolume, setLastCommittedVolume] = useState<number>(100);
const theme = useTheme();
const isReallySmallScreen = useMediaQuery(theme.breakpoints.down(380));
const lastMousePositionRef = useRef<Point>({ x: 0, y: 0 });
const lastMousePositionRef = useRef<Point | undefined>(undefined);
const lastShowTimestampRef = useRef<number>(Date.now());
const lastNumberInputChangeTimestampRef = useRef<number>(Date.now());
const lastShowRef = useRef<boolean>(true);
Expand Down Expand Up @@ -731,15 +731,18 @@ export default function Controls({
if (showOnMouseMovement) {
currentShow =
Date.now() - lastShowTimestampRef.current < 2000 ||
Math.pow(mousePositionRef.current.x - lastMousePositionRef.current.x, 2) +
Math.pow(mousePositionRef.current.y - lastMousePositionRef.current.y, 2) >
100;
(mousePositionRef.current !== undefined &&
lastMousePositionRef.current !== undefined &&
Math.pow(mousePositionRef.current.x - lastMousePositionRef.current.x, 2) +
Math.pow(mousePositionRef.current.y - lastMousePositionRef.current.y, 2) >
100);
} else {
currentShow =
((containerRef.current && mousePositionRef.current.y > containerRef.current.offsetTop - 20) ||
(closeButtonRef.current &&
mousePositionRef.current.y < closeButtonRef.current.offsetHeight + 20)) ??
false;
mousePositionRef.current !== undefined &&
((containerRef.current !== null &&
mousePositionRef.current.y > containerRef.current.offsetTop - 20) ||
(closeButtonRef.current !== null &&
mousePositionRef.current.y < closeButtonRef.current.offsetHeight + 20));
}

currentShow =
Expand All @@ -758,8 +761,7 @@ export default function Controls({
}

lastShowRef.current = currentShow;
lastMousePositionRef.current.x = mousePositionRef.current.x;
lastMousePositionRef.current.y = mousePositionRef.current.y;
lastMousePositionRef.current = { x: mousePositionRef.current?.x ?? 0, y: mousePositionRef.current?.y ?? 0 };
}, 100);
return () => clearInterval(interval);
}, [mousePositionRef, showOnMouseMovement, playing]);
Expand Down
15 changes: 9 additions & 6 deletions common/app/components/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export default function VideoPlayer({
const showSubtitlesRef = useRef<IndexedSubtitleModel[]>([]);
showSubtitlesRef.current = showSubtitles;
const clock = useMemo<Clock>(() => new Clock(), []);
const mousePositionRef = useRef<Point>({ x: 0, y: 0 });
const mousePositionRef = useRef<Point | undefined>(undefined);
const [showCursor, setShowCursor] = useState<boolean>(false);
const lastMouseMovementTimestamp = useRef<number>(0);
const containerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -614,17 +614,20 @@ export default function VideoPlayer({
}
}, [handleSeek, seekRequest, length]);

function handleMouseMove(e: React.MouseEvent<HTMLDivElement>) {
const handleMouseMove = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
lastMouseMovementTimestamp.current = Date.now();

if (!containerRef.current) {
return;
}

var bounds = containerRef.current.getBoundingClientRect();
mousePositionRef.current.x = e.clientX - bounds.left;
mousePositionRef.current.y = e.clientY - bounds.top;
}
mousePositionRef.current = { x: e.clientX - bounds.left, y: e.clientY - bounds.top };
}, []);

const handleMouseLeave = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
mousePositionRef.current = undefined;
}, []);

const handleAudioTrackSelected = useCallback(
(id: string) => {
Expand Down Expand Up @@ -1300,7 +1303,7 @@ export default function VideoPlayer({
}

return (
<div ref={containerRef} onMouseMove={handleMouseMove} className={classes.root}>
<div ref={containerRef} onMouseMove={handleMouseMove} onMouseLeave={handleMouseLeave} className={classes.root}>
<Alert open={alertOpen} onClose={handleAlertClosed} autoHideDuration={3000} severity={alertSeverity}>
{alertMessage}
</Alert>
Expand Down

0 comments on commit 4cf8849

Please sign in to comment.