Skip to content

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

13.7 KB
Raw Blame
import React, { useRef } from 'react';
import { Image as ImageIcon, Trash2, Type, Upload } from 'lucide-react';
import {
  prepareWatermarkImageDataUrl,
  TimelapseWatermarkConfig,
  TimelapseWatermarkFontStyle,
  TimelapseWatermarkPosition,
} from '../../utils/timelapseWatermark';
import { ToggleSwitch } from '../ui';
import { isImeComposing, useI18n } from '../../i18n';

const POSITION_GRID: Array<{ value: TimelapseWatermarkPosition; label: string }> = [
  { value: 'top-left', label: 'TL' },
  { value: 'top-center', label: 'TC' },
  { value: 'top-right', label: 'TR' },
  { value: 'center', label: 'Center' },
  { value: 'bottom-left', label: 'BL' },
  { value: 'bottom-center', label: 'BC' },
  { value: 'bottom-right', label: 'BR' },
];

const FONT_STYLES: Array<{ value: TimelapseWatermarkFontStyle; label: string }> = [
  { value: 'sans', label: 'Sans' },
  { value: 'bold', label: 'Bold' },
  { value: 'mono', label: 'Mono' },
  { value: 'serif', label: 'Serif' },
];

const QUICK_COLORS = ['#ffffff', '#141414', '#f59e0b', '#38bdf8', '#f43f5e'];

export interface TimelapseWatermarkControlsProps {
  config: TimelapseWatermarkConfig;
  onChange: (next: TimelapseWatermarkConfig) => void;
  disabled?: boolean;
  onError?: (msg: string) => void;
}

export const TimelapseWatermarkControls: React.FC<TimelapseWatermarkControlsProps> = ({
  config,
  onChange,
  disabled = false,
  onError,
}) => {
  const { t, tc } = useI18n();
  const fileInputRef = useRef<HTMLInputElement | null>(null);

  const patch = (partial: Partial<TimelapseWatermarkConfig>) => {
    onChange({ ...config, ...partial });
  };

  const handleImageUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (!file) return;
    try {
      const dataUrl = await prepareWatermarkImageDataUrl(file);
      patch({ mode: 'image', imageDataUrl: dataUrl, enabled: true });
    } catch (err) {
      onError?.(err instanceof Error ? err.message : t('Failed to load watermark image'));
    } finally {
      if (fileInputRef.current) fileInputRef.current.value = '';
    }
  };

  return (
    <div className="space-y-3">
      {/* Master Enable Toggle */}
      <div className="flex items-center justify-between bg-ui-header border border-ui-subtle rounded-ui-md px-3 py-2">
        <div>
          <div className="text-[11px] font-medium text-ui-primary">{t('Enable Watermark')}</div>
          <div className="text-[10px] text-ui-muted">
            {t('Rendered live in preview and burned into exported videos')}
          </div>
        </div>
        <ToggleSwitch
          checked={config.enabled}
          onChange={(enabled) => patch({ enabled })}
          disabled={disabled}
          aria-label={t('Enable watermark')}
        />
      </div>

      <div className={config.enabled ? 'space-y-3' : 'space-y-3 opacity-50 pointer-events-none'}>
        {/* Watermark Mode */}
        <div className="space-y-1">
          <label className="text-[10px] uppercase tracking-wider text-ui-muted block">{t('Type')}</label>
          <div className="grid grid-cols-2 gap-1 bg-ui-input border border-ui-subtle p-0.5 rounded-ui-sm">
            <button
              type="button"
              disabled={disabled}
              onClick={() => patch({ mode: 'text' })}
              className={`py-1.5 rounded-ui-xs text-[11px] flex items-center justify-center gap-1.5 cursor-pointer transition-colors ${
                config.mode === 'text'
                  ? 'bg-ui-control-active text-ui-primary font-medium'
                  : 'text-ui-muted hover:text-ui-primary'
              }`}
            >
              <Type className="w-3 h-3" />
              <span>{t('Text')}</span>
            </button>
            <button
              type="button"
              disabled={disabled}
              onClick={() => patch({ mode: 'image' })}
              className={`py-1.5 rounded-ui-xs text-[11px] flex items-center justify-center gap-1.5 cursor-pointer transition-colors ${
                config.mode === 'image'
                  ? 'bg-ui-control-active text-ui-primary font-medium'
                  : 'text-ui-muted hover:text-ui-primary'
              }`}
            >
              <ImageIcon className="w-3 h-3" />
              <span>{t('Logo / Signature')}</span>
            </button>
          </div>
        </div>

        {/* Mode-Specific Controls */}
        {config.mode === 'text' ? (
          <div className="space-y-2.5">
            <div className="space-y-1">
              <label className="text-[10px] uppercase tracking-wider text-ui-muted block">
                {t('Watermark Text')}
              </label>
              <input
                type="text"
                maxLength={80}
                value={config.text}
                disabled={disabled}
                onChange={(e) => patch({ text: e.target.value })}
                onKeyDown={(e) => {
                  e.stopPropagation();
                  if (isImeComposing(e)) return;
                }}
                placeholder={t('@yourhandle or Ink Studio')}
                className="w-full h-7 px-2.5 rounded-ui-sm bg-ui-input border border-ui-default text-xs text-ui-primary focus:outline-none focus:border-ui-focus"
              />
            </div>

            <div className="grid grid-cols-2 gap-2">
              <div className="space-y-1">
                <label className="text-[10px] uppercase tracking-wider text-ui-muted block">
                  {t('Style')}
                </label>
                <div className="grid grid-cols-4 gap-0.5 bg-ui-input border border-ui-subtle p-0.5 rounded-ui-sm">
                  {FONT_STYLES.map((st) => (
                    <button
                      key={st.value}
                      type="button"
                      disabled={disabled}
                      onClick={() => patch({ fontStyle: st.value })}
                      className={`py-1 rounded-ui-xs text-[10px] cursor-pointer transition-colors ${
                        config.fontStyle === st.value
                          ? 'bg-ui-control-active text-ui-primary font-semibold'
                          : 'text-ui-muted hover:text-ui-primary'
                      }`}
                    >
                      {tc('fontStyle', st.label)}
                    </button>
                  ))}
                </div>
              </div>

              <div className="space-y-1">
                <label className="text-[10px] uppercase tracking-wider text-ui-muted block">
                  {t('Color')}
                </label>
                <div className="flex items-center gap-1 h-7 px-1.5 rounded-ui-sm bg-ui-input border border-ui-subtle">
                  {QUICK_COLORS.map((hex) => (
                    <button
                      key={hex}
                      type="button"
                      disabled={disabled}
                      onClick={() => patch({ textColor: hex })}
                      title={hex}
                      className={`w-4 h-4 rounded-full border cursor-pointer transition-transform ${
                        config.textColor.toLowerCase() === hex.toLowerCase()
                          ? 'border-ui-accent scale-110'
                          : 'border-ui-default'
                      }`}
                      style={{ backgroundColor: hex }}
                    />
                  ))}
                  <input
                    type="color"
                    aria-label={t('Custom watermark color')}
                    value={config.textColor}
                    disabled={disabled}
                    onChange={(e) => patch({ textColor: e.target.value })}
                    className="w-5 h-5 ml-auto bg-transparent border-0 cursor-pointer"
                  />
                </div>
              </div>
            </div>

            <div className="flex items-center justify-between bg-ui-header/70 border border-ui-subtle rounded-ui-sm px-2.5 py-1.5">
              <span className="text-[11px] text-ui-secondary">{t('Subtle backdrop pill')}</span>
              <ToggleSwitch
                checked={config.showBackdrop}
                onChange={(showBackdrop) => patch({ showBackdrop })}
                disabled={disabled}
                aria-label={t('Watermark backdrop pill')}
              />
            </div>
          </div>
        ) : (
          <div className="space-y-2">
            <input
              ref={fileInputRef}
              type="file"
              accept="image/png,image/webp,image/svg+xml,image/jpeg"
              onChange={handleImageUpload}
              className="hidden"
            />
            <div className="flex items-center gap-2 bg-ui-header border border-ui-subtle rounded-ui-md p-2">
              <div className="w-12 h-12 rounded-ui-sm bg-ui-void border border-ui-subtle flex items-center justify-center overflow-hidden shrink-0">
                {config.imageDataUrl ? (
                  <img
                    src={config.imageDataUrl}
                    alt={t('Watermark logo preview')}
                    className="max-w-full max-h-full object-contain"
                  />
                ) : (
                  <ImageIcon className="w-4 h-4 text-ui-muted" />
                )}
              </div>
              <div className="flex-1 min-w-0 space-y-1">
                <div className="text-[11px] text-ui-primary font-medium truncate">
                  {config.imageDataUrl ? t('Custom logo / signature') : t('No image selected')}
                </div>
                <div className="flex items-center gap-1.5">
                  <button
                    type="button"
                    disabled={disabled}
                    onClick={() => fileInputRef.current?.click()}
                    className="px-2 py-1 rounded-ui-xs bg-ui-control hover:bg-ui-control-hover border border-ui-default text-[10px] text-ui-primary flex items-center gap-1 cursor-pointer"
                  >
                    <Upload className="w-3 h-3" />
                    <span>{config.imageDataUrl ? t('Replace') : t('Upload PNG/SVG')}</span>
                  </button>
                  {config.imageDataUrl && (
                    <button
                      type="button"
                      disabled={disabled}
                      onClick={() => patch({ imageDataUrl: null })}
                      title={t('Remove logo')}
                      className="p-1 rounded-ui-xs bg-ui-control hover:bg-ui-danger/15 border border-ui-subtle text-ui-muted hover:text-ui-danger cursor-pointer"
                    >
                      <Trash2 className="w-3 h-3" />
                    </button>
                  )}
                </div>
              </div>
            </div>
          </div>
        )}

        {/* Position Anchor Grid */}
        <div className="space-y-1">
          <label className="text-[10px] uppercase tracking-wider text-ui-muted block">
            {t('Position')}
          </label>
          <div className="grid grid-cols-4 gap-1 bg-ui-input border border-ui-subtle p-1 rounded-ui-sm">
            {POSITION_GRID.map((pos) => (
              <button
                key={pos.value}
                type="button"
                disabled={disabled}
                onClick={() => patch({ position: pos.value })}
                className={`py-1 rounded-ui-xs text-[10px] font-ui-mono cursor-pointer transition-colors ${
                  pos.value === 'center' ? 'col-span-1' : ''
                } ${
                  config.position === pos.value
                    ? 'bg-ui-control-active text-ui-primary font-semibold'
                    : 'text-ui-muted hover:text-ui-primary'
                }`}
              >
                {tc('wmPos', pos.label)}
              </button>
            ))}
          </div>
        </div>

        {/* Size / Opacity / Margin Sliders */}
        <div className="grid grid-cols-3 gap-2">
          <div className="space-y-1">
            <div className="flex items-center justify-between text-[10px] text-ui-muted">
              <span>{t('Size')}</span>
              <span className="font-ui-mono tabular-nums text-ui-secondary">{config.scale}%</span>
            </div>
            <input
              type="range"
              min={5}
              max={40}
              step={1}
              disabled={disabled}
              value={config.scale}
              onChange={(e) => patch({ scale: Number(e.target.value) })}
              className="w-full cursor-pointer"
            />
          </div>

          <div className="space-y-1">
            <div className="flex items-center justify-between text-[10px] text-ui-muted">
              <span>{t('Opacity')}</span>
              <span className="font-ui-mono tabular-nums text-ui-secondary">
                {Math.round(config.opacity * 100)}%
              </span>
            </div>
            <input
              type="range"
              min={10}
              max={100}
              step={5}
              disabled={disabled}
              value={Math.round(config.opacity * 100)}
              onChange={(e) => patch({ opacity: Number(e.target.value) / 100 })}
              className="w-full cursor-pointer"
            />
          </div>

          <div className="space-y-1">
            <div className="flex items-center justify-between text-[10px] text-ui-muted">
              <span>{t('Margin')}</span>
              <span className="font-ui-mono tabular-nums text-ui-secondary">{config.margin}%</span>
            </div>
            <input
              type="range"
              min={0}
              max={12}
              step={1}
              disabled={disabled}
              value={config.margin}
              onChange={(e) => patch({ margin: Number(e.target.value) })}
              className="w-full cursor-pointer"
            />
          </div>
        </div>
      </div>
    </div>
  );
};