import { getSessionApi } from "@jellyfin/sdk/lib/utils/api/session-api"; import { IconButton, Slider, Tooltip, Typography } from "@mui/material"; import { WebviewWindow as appWindow } from "@tauri-apps/api/webviewWindow"; import { AnimatePresence, motion } from "motion/react"; import React, { type MouseEvent, useCallback, useEffect, useRef, useState, } from "react"; import { useShallow } from "zustand/shallow"; import CastButton from "@/components/buttons/castButton"; import PlayNextButton from "@/components/buttons/playNextButton"; import PlayPreviousButton from "@/components/buttons/playPreviousButtom"; import QueueButton from "@/components/buttons/queueButton"; import BubbleSlider from "@/components/playback/videoPlayer/bubbleSlider"; import { secToTicks, ticksToSec } from "@/utils/date/time"; import { buildPlayDeepLink } from "@/utils/deepLinks"; import { getPlayerVideoElement } from "@/utils/methods/pictureInPicture"; import { openSyncPlayDialog } from "@/utils/methods/syncPlay"; import { useApiInContext } from "@/utils/store/api"; import { useShareStore } from "@/utils/store/share"; import { usePlaybackStore } from "@/utils/store/playback"; import useSyncPlayback from "@/utils/store/syncPlayback"; import type { VideoFitMode } from "@/utils/types/videoFitMode"; import CaptionsButton from "./buttons/CaptionsButton"; import ChaptersListButton from "./buttons/ChaptersListButton"; import ForwardButton from "./buttons/ForwardButton"; import FullscreenButton from "./buttons/FullscreenButton"; import NextChapterButton from "./buttons/NextChapterButton"; import PictureInPictureButton from "./buttons/PictureInPictureButton"; import PlayPauseButton from "./buttons/PlayPauseButton"; import PrevChapterButton from "./buttons/PrevChapterButton"; import RewindButton from "./buttons/RewindButton"; import VideoFitButton from "./buttons/VideoFitButton"; import EndsAtDisplay from "./EndsAtDisplay"; import VideoPlayerSettingsMenu from "./settingsMenu"; import "./controls.scss"; import { clearQueue } from "@/utils/store/queue"; import useQueue from "@/utils/store/queue"; import ProgressDisplay from "./ProgressDisplay"; /** * Constant for the volume change interval when using the mouse wheel. * This value determines how much the volume will change with each scroll step. */ const VOLUME_SCROLL_INTERVAL = 0.02; type VideoPlayerControlsProps = { // isVisible: boolean; onHover?: (event: MouseEvent) => void; onLeave?: (event: MouseEvent) => void; fitMode: VideoFitMode; onFitModeChange: (mode: VideoFitMode) => void; }; const VideoPlayerControls = ({ // isVisible, onHover, onLeave, fitMode, onFitModeChange, }: VideoPlayerControlsProps) => { const playerOSDRef = useRef(null); const api = useApiInContext((state) => state.api); const syncEnabled = useSyncPlayback((state) => state.enabled); const syncMaster = useSyncPlayback((state) => state.master); const startSync = useSyncPlayback((state) => state.start); const stopSync = useSyncPlayback((state) => state.stop); const syncThisVideo = syncEnabled && syncMaster?.sessionId === null; const toggleSync = () => { if (syncThisVideo) { stopSync(); return; } if (itemId) startSync({ itemId, sessionId: null }); }; const { mediaSourceId, itemId, item, itemName, episodeTitle, playsessionId, isPlayerPlaying, toggleIsPlaying, toggleIsPlayerFullscreen, isUserSeeking, // seekValue, isPlayerMuted, volume, setVolume, toggleIsPlayerMuted, increaseVolumeByStep, decreaseVolumeByStep, getCurrentTime, isUserHovering, currentTime, } = usePlaybackStore( useShallow((state) => ({ mediaSourceId: state.mediaSource.id, itemChapters: state.metadata.item.Chapters, itemId: state.metadata.item.Id, item: state.metadata.item, itemName: state.metadata.itemName, episodeTitle: state.metadata.episodeTitle, // mediaSegments: state.metadata.mediaSegments, isPlayerPlaying: state.playerState.isPlayerPlaying, playsessionId: state.playsessionId, toggleIsPlaying: state.toggleIsPlaying, toggleIsPlayerFullscreen: state.toggleIsPlayerFullscreen, isUserSeeking: state.playerState.isUserSeeking, seekValue: state.playerState.seekValue, isPlayerMuted: state.playerState.isPlayerMuted, volume: state.playerState.volume, setVolume: state.setVolume, toggleIsPlayerMuted: state.toggleIsPlayerMuted, increaseVolumeByStep: state.increaseVolumeByStep, decreaseVolumeByStep: state.decreaseVolumeByStep, getCurrentTime: state.getCurrentTime, isUserHovering: state.playerState.isUserHovering, currentTime: state.playerState.currentTime, })), ); // Volume control with mouse wheel useEffect(() => { const handleMouseWheel = (event: WheelEvent) => { if (event.deltaY < 0) { increaseVolumeByStep(VOLUME_SCROLL_INTERVAL); } else if (event.deltaY > 0) { decreaseVolumeByStep(VOLUME_SCROLL_INTERVAL); } }; const currentRef = playerOSDRef.current; // attach the event listener currentRef?.addEventListener("wheel", handleMouseWheel); // remove the event listener return () => { currentRef?.removeEventListener("wheel", handleMouseWheel); }; }, [increaseVolumeByStep, decreaseVolumeByStep]); const [clickTimeout, setClickTimeout] = useState | null>(null); const [showVolumeControl, setShowVolumeControl] = useState(false); const [settingsMenuRef, setSettingsMenuRef] = useState(null); const [videoFrame, setVideoFrame] = useState<{ top: number; left: number; right: number; } | null>(null); // Keep the back button and the top action buttons on one line inside the // visible video area, below any letterbox bars. useEffect(() => { let frame = 0; const update = () => { const video = getPlayerVideoElement(); if (!video) return; const rect = video.getBoundingClientRect(); const ratio = video.videoWidth > 0 && video.videoHeight > 0 ? video.videoWidth / video.videoHeight : null; const contentWidth = ratio ? Math.min(rect.width, rect.height * ratio) : rect.width; const contentHeight = ratio ? contentWidth / ratio : rect.height; const contentLeft = rect.left + (rect.width - contentWidth) / 2; const contentTop = rect.top + (rect.height - contentHeight) / 2; setVideoFrame((current) => { const next = { // 14% keeps the controls inside the picture even when the // file has letterbox bars baked into a 16:9 frame. top: Math.round( contentTop + Math.max(28, contentHeight * 0.14), ), left: Math.round(contentLeft + 24), right: Math.round( window.innerWidth - contentLeft - contentWidth + 24, ), }; return current?.top === next.top && current?.left === next.left && current?.right === next.right ? current : next; }); }; const schedule = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(update); }; schedule(); const video = getPlayerVideoElement(); video?.addEventListener("loadedmetadata", schedule); window.addEventListener("resize", schedule); const interval = window.setInterval(schedule, 1000); return () => { cancelAnimationFrame(frame); window.clearInterval(interval); video?.removeEventListener("loadedmetadata", schedule); window.removeEventListener("resize", schedule); }; }, [playsessionId]); // const creditInfo = mediaSegments?.Items?.find( // (segment) => segment.Type === "Outro", // ); // Credits and Next Episode Card // const [forceShowCredits, setForceShowCredits] = useState(false); // const showUpNextCard = useMemo(() => { // if (queue?.[currentQueueItemIndex]?.Id === queue?.[queue.length - 1]?.Id) { // return false; // Check if the current playing episode is last episode in queue // } // if (creditInfo) { // if ( // currentTime >= (creditInfo.StartTicks ?? currentTime + 1) && // currentTime < (creditInfo.EndTicks ?? 0) // ) // return true; // } // if ( // Math.ceil(ticksToSec(itemDuration) - ticksToSec(currentTime)) <= 30 && // Math.ceil(ticksToSec(itemDuration) - ticksToSec(currentTime)) > 0 // ) { // return true; // } // return false; // }, [currentTime, creditInfo, itemDuration, queue, currentQueueItemIndex]); const handleExitPlayer = useCallback(async () => { appWindow.getCurrent().setFullscreen(false); history.back(); if (!api) { throw Error("API is not available, cannot report playback stopped."); } // Report Jellyfin server: Playback has ended/stopped getSessionApi(api).reportPlaybackStopped({ playbackStopInfo: { Failed: false, ItemId: itemId, MediaSourceId: mediaSourceId, PlaySessionId: playsessionId, PositionTicks: secToTicks(getCurrentTime() ?? 0), }, }); usePlaybackStore.setState(usePlaybackStore.getInitialState()); clearQueue(); }, [api, mediaSourceId, playsessionId, itemId]); return ( { if (event.currentTarget !== event.target) { return; } if (event.detail === 1) { setClickTimeout( setTimeout(() => { toggleIsPlaying(); }, 200), ); } else if (event.detail === 2 && clickTimeout) { clearTimeout(clickTimeout); toggleIsPlayerFullscreen(); } }} >
arrow_back
useShareStore .getState() .openShareDialog( buildPlayDeepLink( itemId ?? "", currentTime > 0 ? ticksToSec(currentTime) : getCurrentTime(), ), "Поделиться этой сценой", ) } aria-label="Поделиться сценой" title="Поделиться сценой" > share groups setSettingsMenuRef(e.currentTarget)} aria-label="Настройки плеера" title="Настройки плеера" > settings setSettingsMenuRef(null)} />
{String(itemName)} {episodeTitle && ( {String(episodeTitle)} )}
setShowVolumeControl(false)} > {showVolumeControl && ( { const val = Array.isArray(newValue) ? newValue[0] : newValue; setVolume(val); }} sx={{ width: 100, color: "white" }} /> )} setShowVolumeControl(true)} > {isPlayerMuted ? "volume_off" : volume < 0.4 ? "volume_down" : "volume_up"} ({ kind: "video", positionTicks: secToTicks( usePlaybackStore.getState().getCurrentTime(), ), queue: useQueue.getState().tracks ?? [], startIndex: useQueue.getState().currentItemIndex, })} /> sync
); }; export default VideoPlayerControls;