Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

11.8 KB
Raw Blame
import React, { useEffect, useRef, useState } from 'react';
import {
  ChevronLeft,
  ChevronRight,
  Minimize2,
  Pause,
  Play,
  Repeat,
  SkipBack,
  X,
} from 'lucide-react';
import { TimelapseDurationPreset } from '../../utils/timelapseEngine';
import { formatTimecode, TimelapsePlaybackSpeed } from './useTimelapsePreviewPlayer';
import { useI18n } from '../../i18n';

const QUICK_INTERVALS: Array<{ value: TimelapseDurationPreset; label: string }> = [
  { value: '15s', label: '15s' },
  { value: '30s', label: '30s' },
  { value: '60s', label: '60s' },
  { value: '120s', label: '2m' },
  { value: 'full', label: 'Full' },
];

export interface TimelapseFullscreenViewerProps {
  isOpen: boolean;
  onExit: () => void;
  documentName: string;
  canvasRef: React.RefObject<HTMLCanvasElement | null>;
  isPlaying: boolean;
  onTogglePlay: () => void;
  onStepBy: (delta: number) => void;
  onRestart: () => void;
  timelineIndex: number;
  totalTimelineFrames: number;
  onSeek: (idx: number) => void;
  currentTimeSec: number;
  totalDurationSec: number;
  currentSourceFrameIndex: number;
  totalCapturedFrames: number;
  duration: TimelapseDurationPreset;
  onChangeDuration: (d: TimelapseDurationPreset) => void;
  fps: number;
  outputDims: { width: number; height: number };
  playbackSpeed: TimelapsePlaybackSpeed;
  onCycleSpeed: () => void;
  loop: boolean;
  onToggleLoop: () => void;
}

export const TimelapseFullscreenViewer: React.FC<TimelapseFullscreenViewerProps> = ({
  isOpen,
  onExit,
  documentName,
  canvasRef,
  isPlaying,
  onTogglePlay,
  onStepBy,
  onRestart,
  timelineIndex,
  totalTimelineFrames,
  onSeek,
  currentTimeSec,
  totalDurationSec,
  currentSourceFrameIndex,
  totalCapturedFrames,
  duration,
  onChangeDuration,
  fps,
  outputDims,
  playbackSpeed,
  onCycleSpeed,
  loop,
  onToggleLoop,
}) => {
  const { t, td } = useI18n();
  const containerRef = useRef<HTMLDivElement | null>(null);
  const [hudVisible, setHudVisible] = useState(true);
  const hideTimerRef = useRef<number | null>(null);

  // Request native browser/WebView fullscreen gracefully; stay working in full-viewport overlay if declined
  useEffect(() => {
    if (!isOpen) return;
    const el = containerRef.current;
    let enteredNative = false;
    if (el && typeof el.requestFullscreen === 'function') {
      el.requestFullscreen()
        .then(() => {
          enteredNative = true;
        })
        .catch(() => {
          enteredNative = false;
        });
    }

    const handleFullscreenChange = () => {
      if (enteredNative && typeof document !== 'undefined' && !document.fullscreenElement) {
        onExit();
      }
    };
    if (typeof document !== 'undefined') {
      document.addEventListener('fullscreenchange', handleFullscreenChange);
    }

    return () => {
      if (typeof document !== 'undefined') {
        document.removeEventListener('fullscreenchange', handleFullscreenChange);
        if (document.fullscreenElement === el && typeof document.exitFullscreen === 'function') {
          document.exitFullscreen().catch(() => {});
        }
      }
    };
  }, [isOpen, onExit]);

  // Auto-hide HUD while playing & cursor is idle
  useEffect(() => {
    if (!isOpen) return;
    if (!isPlaying) {
      setHudVisible(true);
      if (hideTimerRef.current !== null) {
        window.clearTimeout(hideTimerRef.current);
        hideTimerRef.current = null;
      }
      return;
    }
    const scheduleHide = () => {
      if (hideTimerRef.current !== null) window.clearTimeout(hideTimerRef.current);
      hideTimerRef.current = window.setTimeout(() => {
        setHudVisible(false);
      }, 2400);
    };
    scheduleHide();
    return () => {
      if (hideTimerRef.current !== null) {
        window.clearTimeout(hideTimerRef.current);
        hideTimerRef.current = null;
      }
    };
  }, [isOpen, isPlaying]);

  const bumpHud = () => {
    setHudVisible(true);
    if (isPlaying) {
      if (hideTimerRef.current !== null) window.clearTimeout(hideTimerRef.current);
      hideTimerRef.current = window.setTimeout(() => setHudVisible(false), 2400);
    }
  };

  if (!isOpen) return null;

  const progressPct =
    totalTimelineFrames > 1 ? (timelineIndex / (totalTimelineFrames - 1)) * 100 : 0;

  return (
    <div
      ref={containerRef}
      role="dialog"
      aria-modal="true"
      aria-label={t('Time-lapse Full-screen Preview')}
      onMouseMove={bumpHud}
      onClick={(e) => e.stopPropagation()}
      className="fixed inset-0 z-[100] bg-black flex flex-col items-center justify-center select-none overflow-hidden"
    >
      {/* Top Overlay Bar */}
      <div
        className={`absolute top-0 inset-x-0 z-20 px-5 py-3 bg-gradient-to-b from-black/80 via-black/45 to-transparent flex items-center justify-between transition-opacity duration-200 ${
          hudVisible ? 'opacity-100' : 'opacity-0 pointer-events-none'
        }`}
      >
        <div className="flex items-center gap-3 min-w-0">
          <span className="text-sm font-medium text-white truncate">
            {td(documentName.replace(/\*+$/, '') || 'Untitled')}
          </span>
          <span className="text-[11px] font-ui-mono tabular-nums text-white/70 bg-white/10 px-2 py-0.5 rounded-ui-sm">
            {t('{width}×{height} · {fps} FPS · {count} frames', {
              width: outputDims.width,
              height: outputDims.height,
              fps,
              count: totalCapturedFrames,
            })}
          </span>
        </div>

        <button
          type="button"
          onClick={onExit}
          aria-label={t('Exit full-screen')}
          title={t('Exit Full-screen (Esc / F)')}
          className="px-2.5 py-1.5 rounded-ui-sm bg-white/10 hover:bg-white/20 text-white text-xs flex items-center gap-1.5 transition-colors cursor-pointer"
        >
          <X className="w-3.5 h-3.5" />
          <span>{t('Exit')}</span>
        </button>
      </div>

      {/* Center Canvas Stage */}
      <div
        onClick={onTogglePlay}
        onDoubleClick={onExit}
        className="w-full h-full flex items-center justify-center p-4 sm:p-10 cursor-pointer"
      >
        <canvas
          ref={canvasRef}
          className="max-w-full max-h-full object-contain shadow-2xl rounded-ui-sm"
        />
      </div>

      {/* Bottom Floating Transport HUD */}
      <div
        onMouseEnter={() => setHudVisible(true)}
        className={`absolute bottom-0 inset-x-0 z-20 px-6 pb-5 pt-10 bg-gradient-to-t from-black/90 via-black/60 to-transparent transition-opacity duration-200 ${
          hudVisible ? 'opacity-100' : 'opacity-0 pointer-events-none'
        }`}
      >
        <div className="max-w-4xl mx-auto space-y-2.5">
          {/* Timeline Scrubber */}
          <div className="flex items-center gap-3">
            <span className="text-[11px] font-ui-mono tabular-nums text-white/90 w-12">
              {formatTimecode(currentTimeSec)}
            </span>
            <div className="relative flex-1 flex items-center">
              <input
                type="range"
                aria-label={t('Full-screen timeline scrubber')}
                min={0}
                max={Math.max(0, totalTimelineFrames - 1)}
                value={Math.min(timelineIndex, Math.max(0, totalTimelineFrames - 1))}
                onChange={(e) => onSeek(Number(e.target.value))}
                className="w-full accent-white cursor-pointer"
                style={{
                  background: `linear-gradient(to right, rgba(255,255,255,0.92) ${progressPct}%, rgba(255,255,255,0.22) ${progressPct}%)`,
                }}
              />
            </div>
            <span className="text-[11px] font-ui-mono tabular-nums text-white/70 w-12 text-right">
              {formatTimecode(totalDurationSec)}
            </span>
          </div>

          {/* Transport + Interval Switcher Row */}
          <div className="flex flex-wrap items-center justify-between gap-3">
            <div className="flex items-center gap-1.5">
              <button
                type="button"
                onClick={onRestart}
                title={t('Jump to start')}
                className="p-1.5 rounded-ui-sm bg-white/10 hover:bg-white/20 text-white transition-colors cursor-pointer"
              >
                <SkipBack className="w-3.5 h-3.5" />
              </button>
              <button
                type="button"
                onClick={() => onStepBy(-1)}
                title={t('Previous frame (Left arrow)')}
                className="p-1.5 rounded-ui-sm bg-white/10 hover:bg-white/20 text-white transition-colors cursor-pointer"
              >
                <ChevronLeft className="w-3.5 h-3.5" />
              </button>
              <button
                type="button"
                onClick={onTogglePlay}
                title={t('Play / Pause (Space)')}
                className="px-3 py-1.5 rounded-ui-sm bg-white text-black hover:bg-white/90 font-medium text-xs flex items-center gap-1.5 transition-colors cursor-pointer"
              >
                {isPlaying ? <Pause className="w-3.5 h-3.5" /> : <Play className="w-3.5 h-3.5" />}
                <span>{isPlaying ? t('Pause') : t('Play')}</span>
              </button>
              <button
                type="button"
                onClick={() => onStepBy(1)}
                title={t('Next frame (Right arrow)')}
                className="p-1.5 rounded-ui-sm bg-white/10 hover:bg-white/20 text-white transition-colors cursor-pointer"
              >
                <ChevronRight className="w-3.5 h-3.5" />
              </button>
              <button
                type="button"
                onClick={onToggleLoop}
                title={loop ? t('Loop enabled') : t('Loop disabled')}
                className={`p-1.5 rounded-ui-sm transition-colors cursor-pointer ${
                  loop
                    ? 'bg-white/25 text-white'
                    : 'bg-white/10 text-white/55 hover:text-white hover:bg-white/15'
                }`}
              >
                <Repeat className="w-3.5 h-3.5" />
              </button>
              <button
                type="button"
                onClick={onCycleSpeed}
                title={t('Playback speed')}
                className="px-2 py-1 rounded-ui-sm bg-white/10 hover:bg-white/20 text-white text-[11px] font-ui-mono tabular-nums cursor-pointer"
              >
                {playbackSpeed}×
              </button>
            </div>

            {/* Quick Interval Switcher directly inside Full-Screen */}
            <div className="flex items-center gap-1 bg-white/10 p-0.5 rounded-ui-sm">
              {QUICK_INTERVALS.map((opt) => (
                <button
                  key={opt.value}
                  type="button"
                  onClick={() => onChangeDuration(opt.value)}
                  className={`px-2 py-1 rounded-ui-xs text-[11px] font-ui-mono tabular-nums cursor-pointer transition-colors ${
                    duration === opt.value
                      ? 'bg-white text-black font-semibold'
                      : 'text-white/75 hover:text-white'
                  }`}
                >
                  {t(opt.label)}
                </button>
              ))}
            </div>

            <div className="flex items-center gap-2.5">
              <span className="text-[10px] font-ui-mono tabular-nums text-white/65">
                {t('Keyframe {current} / {total}', {
                  current: Math.min(currentSourceFrameIndex + 1, totalCapturedFrames),
                  total: totalCapturedFrames,
                })}
              </span>
              <button
                type="button"
                onClick={onExit}
                title={t('Exit Full-screen (F)')}
                className="p-1.5 rounded-ui-sm bg-white/10 hover:bg-white/20 text-white transition-colors cursor-pointer"
              >
                <Minimize2 className="w-3.5 h-3.5" />
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};