Skip to content

Build your personal knowledge base with Trilium Notes

feat(video_player): button to toggle PIP

Elian Doran4bb662c5fbec
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index ab6b36baf9..1ec8bcb9fa 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -58,6 +58,7 @@ export default function VideoPreview({ note }: { note: FNote }) {
</div>
<div className="right">
<VolumeControl videoRef={videoRef} />
+ <PictureInPictureButton videoRef={videoRef} />
<FullscreenButton targetRef={wrapperRef} />
</div>
</div>
@@ -252,6 +253,49 @@ function PlaybackSpeed({ videoRef }: { videoRef: RefObject<HTMLVideoElement> })
);
}
+function PictureInPictureButton({ videoRef }: { videoRef: RefObject<HTMLVideoElement> }) {
+ const [active, setActive] = useState(false);
+ // The standard PiP API is only supported in Chromium-based browsers.
+ // Firefox uses its own proprietary PiP implementation.
+ const supported = "requestPictureInPicture" in HTMLVideoElement.prototype;
+
+ useEffect(() => {
+ const video = videoRef.current;
+ if (!video || !supported) return;
+
+ const onEnter = () => setActive(true);
+ const onLeave = () => setActive(false);
+
+ video.addEventListener("enterpictureinpicture", onEnter);
+ video.addEventListener("leavepictureinpicture", onLeave);
+ return () => {
+ video.removeEventListener("enterpictureinpicture", onEnter);
+ video.removeEventListener("leavepictureinpicture", onLeave);
+ };
+ }, [supported]);
+
+ if (!supported) return null;
+
+ const toggle = () => {
+ const video = videoRef.current;
+ if (!video) return;
+
+ if (document.pictureInPictureElement) {
+ document.exitPictureInPicture();
+ } else {
+ video.requestPictureInPicture();
+ }
+ };
+
+ return (
+ <ActionButton
+ icon={active ? "bx bx-exit" : "bx bx-window-open"}
+ text={active ? "Exit picture-in-picture" : "Picture-in-picture"}
+ onClick={toggle}
+ />
+ );
+}
+
function FullscreenButton({ targetRef }: { targetRef: RefObject<HTMLElement> }) {
const [isFullscreen, setIsFullscreen] = useState(false);