Skip to content

Build your personal knowledge base with Trilium Notes

feat(video_player): basic toggle of the controls

Elian Doran42a05f411b6c
diff --git a/apps/client/src/widgets/type_widgets/file/Video.css b/apps/client/src/widgets/type_widgets/file/Video.css
index 657f398187..eaa932c5a8 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.css
+++ b/apps/client/src/widgets/type_widgets/file/Video.css
@@ -9,6 +9,15 @@
height: 100%;
}
+ &.controls-hidden {
+ cursor: pointer;
+
+ .video-preview-controls {
+ opacity: 0;
+ pointer-events: none;
+ }
+ }
+
.video-preview-controls {
position: absolute;
bottom: 0;
@@ -19,6 +28,8 @@
flex-direction: column;
gap: 0.5em;
background: rgba(0, 0, 0, 0.5);
+ opacity: 1;
+ transition: opacity 300ms ease;
.video-buttons-row {
display: flex;
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index bb6c48d9b7..b795699086 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -1,7 +1,7 @@
import "./Video.css";
import { RefObject } from "preact";
-import { useEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import FNote from "../../../entities/fnote";
import { getUrlForDownload } from "../../../services/open";
@@ -19,6 +19,42 @@ export default function VideoPreview({ note }: { note: FNote }) {
const wrapperRef = useRef<HTMLDivElement>(null);
const videoRef = useRef<HTMLVideoElement>(null);
const [playing, setPlaying] = useState(false);
+ const [controlsVisible, setControlsVisible] = useState(true);
+ const hideTimerRef = useRef<ReturnType<typeof setTimeout>>();
+
+ const showControlsTemporarily = useCallback(() => {
+ setControlsVisible(true);
+ clearTimeout(hideTimerRef.current);
+ if (videoRef.current && !videoRef.current.paused) {
+ hideTimerRef.current = setTimeout(() => setControlsVisible(false), 3000);
+ }
+ }, []);
+
+ // Auto-hide when playback starts, show when paused.
+ useEffect(() => {
+ if (playing) {
+ hideTimerRef.current = setTimeout(() => setControlsVisible(false), 3000);
+ } else {
+ clearTimeout(hideTimerRef.current);
+ setControlsVisible(true);
+ }
+ return () => clearTimeout(hideTimerRef.current);
+ }, [playing]);
+
+ const onWrapperClick = useCallback((e: MouseEvent) => {
+ // Don't toggle if clicking on controls themselves.
+ const target = e.target as HTMLElement;
+ if (target.closest(".video-preview-controls")) return;
+ setControlsVisible((prev) => {
+ const next = !prev;
+ clearTimeout(hideTimerRef.current);
+ if (next && playing) {
+ hideTimerRef.current = setTimeout(() => setControlsVisible(false), 3000);
+ }
+ return next;
+ });
+ }, [playing]);
+
const togglePlayback = () => {
const video = videoRef.current;
if (!video) return;
@@ -36,9 +72,8 @@ export default function VideoPreview({ note }: { note: FNote }) {
video.currentTime = Math.max(0, Math.min(video.duration, video.currentTime + seconds));
};
-
return (
- <div ref={wrapperRef} className="video-preview-wrapper">
+ <div ref={wrapperRef} className={`video-preview-wrapper ${controlsVisible ? "" : "controls-hidden"}`} onClick={onWrapperClick} onMouseMove={showControlsTemporarily}>
<video
ref={videoRef}
class="video-preview"