131 lines
3.7 KiB
TypeScript
131 lines
3.7 KiB
TypeScript
import type { ReactNode } from 'react';
|
|
import { Bar } from './Bar';
|
|
import { fmtMs } from '../lib/format';
|
|
import {
|
|
ForwardIcon,
|
|
FullscreenIcon,
|
|
PauseIcon,
|
|
PlayIcon,
|
|
RewindIcon,
|
|
SkipNextIcon,
|
|
} from './icons';
|
|
import { t } from '../i18n';
|
|
|
|
// The one custom player control bar, shared by Watch Together (room /ws
|
|
// protocol) and Now Playing (bot RPC). Purely presentational: the two sync
|
|
// channels differ only in the callbacks the views pass in. Embedded players
|
|
// (YouTube/Twitch/HTML5) always run with native controls hidden; this bar is
|
|
// the only playback UI.
|
|
export function PlayerControls(props: {
|
|
playing: boolean;
|
|
positionMs: number;
|
|
durationMs: number; // 0 = unknown
|
|
live?: boolean; // no seekable timeline (Twitch / generic embeds)
|
|
canControl: boolean; // transport disabled when false; volume stays local
|
|
bufferedPct?: number; // 0..100 second fill on the seek bar
|
|
volumePct: number; // 0..100
|
|
volumeLive?: boolean; // apply volume while dragging (local volume)
|
|
onTogglePlay: () => void;
|
|
onSeekMs: (ms: number) => void;
|
|
onSeekBy?: (deltaSec: number) => void; // renders ±10s when present
|
|
onVolume: (pct: number) => void;
|
|
onSkip?: () => void; // renders next-track button when present
|
|
skipDisabled?: boolean;
|
|
onFullscreen?: () => void;
|
|
settings?: ReactNode;
|
|
showLocal?: boolean; // hide the volume/quality row (default shown)
|
|
extra?: ReactNode; // view-specific buttons after the seek bar
|
|
extraLocal?: ReactNode; // view-specific controls in the volume row
|
|
}) {
|
|
const { live, durationMs, positionMs, canControl } = props;
|
|
const progressPct = live
|
|
? 100
|
|
: durationMs > 0
|
|
? Math.min(100, (positionMs / durationMs) * 100)
|
|
: 0;
|
|
return (
|
|
<div className="player-controls">
|
|
<div className="watch-controls">
|
|
<button
|
|
className="ctrl ctrl-main"
|
|
title={t('Play/Pause')}
|
|
disabled={!canControl}
|
|
onClick={props.onTogglePlay}
|
|
>
|
|
{props.playing ? <PauseIcon /> : <PlayIcon />}
|
|
</button>
|
|
{!live && props.onSeekBy && (
|
|
<>
|
|
<button
|
|
className="ctrl"
|
|
title={t('Back 10 seconds')}
|
|
disabled={!canControl}
|
|
onClick={() => props.onSeekBy!(-10)}
|
|
>
|
|
<RewindIcon />
|
|
</button>
|
|
<button
|
|
className="ctrl"
|
|
title={t('Forward 10 seconds')}
|
|
disabled={!canControl}
|
|
onClick={() => props.onSeekBy!(10)}
|
|
>
|
|
<ForwardIcon />
|
|
</button>
|
|
</>
|
|
)}
|
|
{props.onSkip && (
|
|
<button
|
|
className="ctrl"
|
|
title={t('Play next')}
|
|
disabled={!canControl || props.skipDisabled}
|
|
onClick={props.onSkip}
|
|
>
|
|
<SkipNextIcon />
|
|
</button>
|
|
)}
|
|
<span className="mono">{live ? '—' : fmtMs(positionMs)}</span>
|
|
<Bar
|
|
pct={progressPct}
|
|
bufferPct={live ? undefined : props.bufferedPct}
|
|
formatValue={(f) =>
|
|
live || !durationMs
|
|
? t('LIVE')
|
|
: fmtMs(Math.round(f * durationMs))
|
|
}
|
|
onDragStart={() => !live && durationMs > 0}
|
|
onCommit={(f) => props.onSeekMs(Math.round(f * durationMs))}
|
|
/>
|
|
<span className="mono">
|
|
{live ? t('LIVE') : fmtMs(durationMs)}
|
|
</span>
|
|
{props.extra}
|
|
</div>
|
|
{(props.showLocal ?? true) && (
|
|
<div className="watch-controls watch-local">
|
|
<span className="muted">{t('Vol')}</span>
|
|
<Bar
|
|
slim
|
|
live={props.volumeLive}
|
|
pct={props.volumePct}
|
|
formatValue={(f) => `${Math.round(f * 100)}%`}
|
|
onCommit={(f) => props.onVolume(Math.round(f * 100))}
|
|
/>
|
|
<span className="mono">{Math.round(props.volumePct)}%</span>
|
|
{props.settings}
|
|
{props.onFullscreen && (
|
|
<button
|
|
className="ctrl"
|
|
title={t('Fullscreen')}
|
|
onClick={props.onFullscreen}
|
|
>
|
|
<FullscreenIcon />
|
|
</button>
|
|
)}
|
|
{props.extraLocal}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|