skip buttons, better popout handling

Co-authored-by: Jip Frijlink <JipFr@users.noreply.github.com>
This commit is contained in:
Jelle van Snik 2023-01-23 22:38:05 +01:00
parent b2748f7390
commit 20685577ab
11 changed files with 172 additions and 32 deletions

View File

@ -28,6 +28,8 @@ export enum Icons {
EDIT = "edit",
AIRPLAY = "airplay",
EPISODES = "episodes",
SKIP_FORWARD = "skip_forward",
SKIP_BACKWARD = "skip_backward",
}
export interface IconProps {
@ -63,6 +65,8 @@ const iconList: Record<Icons, string> = {
bookmark_outline: `<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 384 512"><!--! Font Awesome Pro 6.2.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path fill="currentColor" d="M336 0h-288C21.49 0 0 21.49 0 48v431.9c0 24.7 26.79 40.08 48.12 27.64L192 423.6l143.9 83.93C357.2 519.1 384 504.6 384 479.9V48C384 21.49 362.5 0 336 0zM336 452L192 368l-144 84V54C48 50.63 50.63 48 53.1 48h276C333.4 48 336 50.63 336 54V452z"/></svg>`,
airplay: `<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-airplay"><path d="M5 17H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2h-1"></path><polygon fill="currentColor" points="12 15 17 21 7 21 12 15"></polygon></svg>`,
episodes: `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M3 4C1.34315 4 0 5.34314 0 7V13.9496C0 15.6065 1.34315 16.9496 3 16.9496H5.86645V14.9496H3C2.44772 14.9496 2 14.5019 2 13.9496V7C2 6.44771 2.44771 6 3 6H16.0327C16.585 6 17.0327 6.44772 17.0327 7V9.86645H19.0327V7C19.0327 5.34315 17.6896 4 16.0327 4H3Z" fill="currentColor"/><rect x="5.89929" y="10.5444" width="17" height="10" rx="2" stroke="currentColor" stroke-width="2"/></svg>`,
skip_forward: `<svg width="26" height="24" viewBox="0 0 26 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M11.3333 12.3333L16 7.66667M16 7.66667L11.3333 3M16 7.66667H6.66667C5.42899 7.66667 4.242 8.15833 3.36684 9.0335C2.49167 9.90867 2 11.0957 2 12.3333C2 13.571 2.49167 14.758 3.36684 15.6332C4.242 16.5083 5.42899 17 6.66667 17H9" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" /><path d="M16.5043 14.2727V23H14.6591V16.0241H14.608L12.6094 17.277V15.6406L14.7699 14.2727H16.5043ZM22.0004 23.1918C21.2674 23.1889 20.6367 23.0085 20.1083 22.6506C19.5827 22.2926 19.1779 21.7741 18.8938 21.0952C18.6126 20.4162 18.4734 19.5994 18.4762 18.6449C18.4762 17.6932 18.6168 16.8821 18.8981 16.2116C19.1822 15.5412 19.587 15.0312 20.1126 14.6818C20.641 14.3295 21.2702 14.1534 22.0004 14.1534C22.7305 14.1534 23.3583 14.3295 23.8839 14.6818C24.4123 15.0341 24.8185 15.5455 25.1026 16.2159C25.3867 16.8835 25.5273 17.6932 25.5245 18.6449C25.5245 19.6023 25.3825 20.4205 25.0984 21.0994C24.8171 21.7784 24.4137 22.2969 23.8881 22.6548C23.3626 23.0128 22.7333 23.1918 22.0004 23.1918ZM22.0004 21.6619C22.5004 21.6619 22.8995 21.4105 23.1978 20.9077C23.4961 20.4048 23.6438 19.6506 23.641 18.6449C23.641 17.983 23.5728 17.4318 23.4364 16.9915C23.3029 16.5511 23.1126 16.2202 22.8654 15.9986C22.6211 15.777 22.3327 15.6662 22.0004 15.6662C21.5032 15.6662 21.1055 15.9148 20.8072 16.4119C20.5089 16.9091 20.3583 17.6534 20.3555 18.6449C20.3555 19.3153 20.4222 19.875 20.5558 20.3239C20.6921 20.7699 20.8839 21.1051 21.131 21.3295C21.3782 21.5511 21.668 21.6619 22.0004 21.6619Z" fill="currentColor" /></svg>`,
skip_backward: `<svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M13.6667 12.3333L9 7.66667M9 7.66667L13.6667 3M9 7.66667H18.3333C19.571 7.66667 20.758 8.15833 21.6332 9.0335C22.5083 9.90867 23 11.0957 23 12.3333C23 13.571 22.5083 14.758 21.6332 15.6332C20.758 16.5083 19.571 17 18.3333 17H16" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M4.50426 14.2727V23H2.65909V16.0241H2.60795L0.609375 17.277V15.6406L2.76989 14.2727H4.50426ZM10.0004 23.1918C9.2674 23.1889 8.63672 23.0085 8.10831 22.6506C7.58274 22.2926 7.17791 21.7741 6.89382 21.0952C6.61257 20.4162 6.47337 19.5994 6.47621 18.6449C6.47621 17.6932 6.61683 16.8821 6.89808 16.2116C7.18217 15.5412 7.587 15.0312 8.11257 14.6818C8.64098 14.3295 9.27024 14.1534 10.0004 14.1534C10.7305 14.1534 11.3583 14.3295 11.8839 14.6818C12.4123 15.0341 12.8185 15.5455 13.1026 16.2159C13.3867 16.8835 13.5273 17.6932 13.5245 18.6449C13.5245 19.6023 13.3825 20.4205 13.0984 21.0994C12.8171 21.7784 12.4137 22.2969 11.8881 22.6548C11.3626 23.0128 10.7333 23.1918 10.0004 23.1918ZM10.0004 21.6619C10.5004 21.6619 10.8995 21.4105 11.1978 20.9077C11.4961 20.4048 11.6438 19.6506 11.641 18.6449C11.641 17.983 11.5728 17.4318 11.4364 16.9915C11.3029 16.5511 11.1126 16.2202 10.8654 15.9986C10.6211 15.777 10.3327 15.6662 10.0004 15.6662C9.5032 15.6662 9.10547 15.9148 8.80717 16.4119C8.50888 16.9091 8.35831 17.6534 8.35547 18.6449C8.35547 19.3153 8.42223 19.875 8.55575 20.3239C8.69212 20.7699 8.88388 21.1051 9.13104 21.3295C9.3782 21.5511 9.66797 21.6619 10.0004 21.6619Z" fill="currentColor"/></svg>`,
};
export const Icon = memo((props: IconProps) => {

View File

@ -11,6 +11,7 @@ import { PauseControl } from "./controls/PauseControl";
import { ProgressControl } from "./controls/ProgressControl";
import { SeriesSelectionControl } from "./controls/SeriesSelectionControl";
import { ShowTitleControl } from "./controls/ShowTitleControl";
import { SkipTime } from "./controls/SkipTime";
import { TimeControl } from "./controls/TimeControl";
import { VolumeControl } from "./controls/VolumeControl";
import { VideoPlayerError } from "./parts/VideoPlayerError";
@ -41,8 +42,9 @@ function LeftSideControls() {
onMouseEnter={handleMouseEnter}
>
<PauseControl />
<VolumeControl className="mr-2" />
<TimeControl />
<VolumeControl className="mr-2" />
<SkipTime />
</div>
<ShowTitleControl />
</>

View File

@ -29,6 +29,8 @@ export function BackdropControl(props: BackdropControlProps) {
(e: React.MouseEvent<HTMLDivElement>) => {
if (!clickareaRef.current || clickareaRef.current !== e.target) return;
if (videoState.popout !== null) return;
if (videoState.isPlaying) videoState.pause();
else videoState.play();
},
@ -49,6 +51,7 @@ export function BackdropControl(props: BackdropControlProps) {
const currentValue = moved || videoState.isPaused;
if (currentValue !== lastBackdropValue.current) {
lastBackdropValue.current = currentValue;
if (!currentValue) videoState.closePopout();
props.onBackdropChange?.(currentValue);
}
}, [videoState, moved, props]);

View File

@ -45,6 +45,7 @@ export function ProgressControl() {
ref={ref}
className="-my-3 flex h-8 items-center"
onMouseDown={dragMouseDown}
onTouchStart={dragMouseDown}
>
<div
className={`relative h-1 flex-1 rounded-full bg-gray-500 bg-opacity-50 transition-[height] duration-100 group-hover:h-2 ${

View File

@ -9,26 +9,12 @@ import { Loading } from "@/components/layout/Loading";
import { IconPatch } from "@/components/buttons/IconPatch";
import { useVideoPlayerState } from "../VideoContext";
import { VideoPlayerIconButton } from "../parts/VideoPlayerIconButton";
import { VideoPopout } from "../parts/VideoPopout";
interface Props {
className?: string;
}
export function PopupThingy(props: {
children?: React.ReactNode;
containerClassName?: string;
}) {
return (
<div className="absolute inset-x-0 h-0">
<div className="absolute bottom-10 right-0 h-96 w-72 rounded-lg bg-denim-400">
<div className={["h-full w-full", props.containerClassName].join(" ")}>
{props.children}
</div>
</div>
</div>
);
}
function PopupSection(props: {
children?: React.ReactNode;
className?: string;
@ -185,22 +171,22 @@ function PopupEpisodeSelect() {
export function SeriesSelectionControl(props: Props) {
const { videoState } = useVideoPlayerState();
const [open, setOpen] = useState(false);
if (!videoState.seasonData.isSeries) return null;
return (
<div className={props.className}>
<div className="relative">
{open ? (
<PopupThingy containerClassName="grid grid-rows-[auto,minmax(0,1fr)]">
<PopupEpisodeSelect />
</PopupThingy>
) : null}
<VideoPopout
id="episodes"
className="grid grid-rows-[auto,minmax(0,1fr)]"
>
<PopupEpisodeSelect />
</VideoPopout>
<VideoPlayerIconButton
icon={Icons.EPISODES}
text="Episodes"
onClick={() => setOpen((s) => !s)}
onClick={() => videoState.openPopout("episodes")}
/>
</div>
</div>

View File

@ -0,0 +1,46 @@
import { useVideoPlayerState } from "../VideoContext";
function durationExceedsHour(secs: number): boolean {
return secs > 60 * 60;
}
function formatSeconds(secs: number, showHours = false): string {
if (Number.isNaN(secs)) {
if (showHours) return "0:00:00";
return "0:00";
}
let time = secs;
const seconds = Math.floor(time % 60);
time /= 60;
const minutes = Math.floor(time % 60);
time /= 60;
const hours = Math.floor(time);
const paddedSecs = seconds.toString().padStart(2, "0");
const paddedMins = minutes.toString().padStart(2, "0");
if (!showHours) return [minutes, paddedSecs].join(":");
return [hours, paddedMins, paddedSecs].join(":");
}
interface Props {
className?: string;
}
export function SkipTime(props: Props) {
const { videoState } = useVideoPlayerState();
const hasHours = durationExceedsHour(videoState.duration);
const time = formatSeconds(videoState.time, hasHours);
const duration = formatSeconds(videoState.duration, hasHours);
return (
<div className={props.className}>
<p className="select-none text-white">
{time} / {duration}
</p>
</div>
);
}

View File

@ -1,3 +1,5 @@
import { Icon, Icons } from "@/components/Icon";
import { VideoPlayerIconButton } from "../parts/VideoPlayerIconButton";
import { useVideoPlayerState } from "../VideoContext";
function durationExceedsHour(secs: number): boolean {
@ -32,14 +34,26 @@ interface Props {
export function TimeControl(props: Props) {
const { videoState } = useVideoPlayerState();
const hasHours = durationExceedsHour(videoState.duration);
const time = formatSeconds(videoState.time, hasHours);
const duration = formatSeconds(videoState.duration, hasHours);
const skipForward = () => {
videoState.setTime(videoState.time + 10);
};
const skipBackward = () => {
videoState.setTime(videoState.time - 10);
};
return (
<div className={props.className}>
<p className="select-none text-white">
{time} / {duration}
<p className="flex select-none items-center text-white">
<VideoPlayerIconButton
icon={Icons.SKIP_BACKWARD}
onClick={skipBackward}
/>
<VideoPlayerIconButton
icon={Icons.SKIP_FORWARD}
onClick={skipForward}
/>
</p>
</div>
);

View File

@ -42,6 +42,8 @@ export interface PlayerControls {
setShowData(data: ShowData): void;
setCurrentEpisode(sId: string, eId: string): void;
startAirplay(): void;
openPopout(id: string): void;
closePopout(): void;
}
export const initialControls: PlayerControls = {
@ -57,6 +59,8 @@ export const initialControls: PlayerControls = {
setShowData: () => null,
startAirplay: () => null,
setCurrentEpisode: () => null,
openPopout: () => null,
closePopout: () => null,
};
export function populateControls(
@ -129,6 +133,12 @@ export function populateControls(
setLeftControlsHover(hovering) {
update((s) => ({ ...s, leftControlHovering: hovering }));
},
openPopout(id: string) {
update((s) => ({ ...s, popout: id }));
},
closePopout() {
update((s) => ({ ...s, popout: null }));
},
setShowData(data) {
update((s) => ({ ...s, seasonData: data }));
},

View File

@ -23,6 +23,7 @@ export type PlayerState = {
hasInitialized: boolean;
leftControlHovering: boolean;
hasPlayedOnce: boolean;
popout: string | null;
seasonData: {
isSeries: boolean;
current?: {
@ -61,6 +62,7 @@ export const initialPlayerState: PlayerContext = {
leftControlHovering: false,
hasPlayedOnce: false,
error: null,
popout: null,
seasonData: {
isSeries: false,
},

View File

@ -0,0 +1,54 @@
import { useEffect, useRef } from "react";
import { useVideoPlayerState } from "../VideoContext";
interface Props {
children?: React.ReactNode;
id?: string;
className?: string;
}
export function VideoPopout(props: Props) {
const { videoState } = useVideoPlayerState();
const popoutRef = useRef<HTMLDivElement>(null);
const isOpen = videoState.popout === props.id;
useEffect(() => {
if (!isOpen) return;
const popoutEl = popoutRef.current;
let hasTriggered = false;
function windowClick() {
setTimeout(() => {
if (hasTriggered) return;
videoState.closePopout();
hasTriggered = false;
}, 10);
}
function popoutClick() {
hasTriggered = true;
setTimeout(() => {
hasTriggered = false;
}, 100);
}
window.addEventListener("click", windowClick);
popoutEl?.addEventListener("click", popoutClick);
return () => {
window.removeEventListener("click", windowClick);
popoutEl?.removeEventListener("click", popoutClick);
};
}, [isOpen, videoState]);
if (!isOpen) return null;
return (
<div className="is-popout absolute inset-x-0 h-0">
<div className="absolute bottom-10 right-0 h-96 w-72 rounded-lg bg-denim-400">
<div
ref={popoutRef}
className={["h-full w-full", props.className].join(" ")}
>
{props.children}
</div>
</div>
</div>
);
}

View File

@ -1,5 +1,9 @@
import React, { RefObject, useCallback, useEffect, useState } from "react";
type ActivityEvent =
| React.MouseEvent<HTMLElement>
| React.TouchEvent<HTMLElement>;
export function makePercentageString(num: number) {
return `${num.toFixed(2)}%`;
}
@ -8,6 +12,18 @@ export function makePercentage(num: number) {
return Number(Math.max(0, Math.min(num, 100)).toFixed(2));
}
function isClickEvent(
evt: React.MouseEvent<HTMLElement> | React.TouchEvent<HTMLElement>
): evt is React.MouseEvent<HTMLElement> {
return evt.type === "mousedown";
}
const getEventX = (
evt: React.MouseEvent<HTMLElement> | React.TouchEvent<HTMLElement>
) => {
return isClickEvent(evt) ? evt.pageX : evt.touches[0].pageX;
};
export function useProgressBar(
barRef: RefObject<HTMLElement>,
commit: (percentage: number) => void,
@ -25,19 +41,20 @@ export function useProgressBar(
if (commitImmediately) commit(pos);
}
function mouseUp(ev: MouseEvent) {
function mouseUp(ev: MouseEvent | TouchEvent) {
if (!mouseDown) return;
setMouseDown(false);
document.body.removeAttribute("data-no-select");
if (!barRef.current) return;
const rect = barRef.current.getBoundingClientRect();
const pos = (ev.pageX - rect.left) / barRef.current.offsetWidth;
const pos = (getEventX(ev) - rect.left) / barRef.current.offsetWidth;
commit(pos);
}
document.addEventListener("mousemove", mouseMove);
document.addEventListener("mouseup", mouseUp);
document.addEventListener("touchend", mouseUp);
return () => {
document.removeEventListener("mousemove", mouseMove);
@ -46,13 +63,14 @@ export function useProgressBar(
}, [mouseDown, barRef, commit, commitImmediately]);
const dragMouseDown = useCallback(
(ev: React.MouseEvent<HTMLElement>) => {
(ev: React.MouseEvent<HTMLElement> | React.TouchEvent<HTMLElement>) => {
setMouseDown(true);
document.body.setAttribute("data-no-select", "true");
if (!barRef.current) return;
const rect = barRef.current.getBoundingClientRect();
const pos = ((ev.pageX - rect.left) / barRef.current.offsetWidth) * 100;
const pos =
((getEventX(ev) - rect.left) / barRef.current.offsetWidth) * 100;
setProgress(pos);
},
[setProgress, barRef]