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 } from 'react';
import { Contrast } from 'lucide-react';
import {
  ToolType,
  BrushSettings,
  CanvasColorMode,
  CanvasDimensions,
  SoftProofSettings,
  CmykStandard,
} from '../types/editor';
import { ThemePreset } from '../types/settings';
import { DEFAULT_STATUS_BAR_MODULES, StatusBarModulesConfig } from '../types/workspace';
import { formatZoomPercent } from '../hooks/useCanvasNavigation';
import { formatRotationDegrees } from '../utils/canvasUtils';
import { useI18n } from '../i18n';

const CMYK_STANDARD_LABELS: Record<CmykStandard, string> = {
  'japan-color-2001': 'Japan Color 2001',
  'us-web-coated-swop': 'US Web Coated',
  'iso-coated-v2': 'ISO Coated v2',
};

interface StatusBarProps {
  cursorPos?: { x: number; y: number } | null;
  currentPressure?: number;
  tool: ToolType;
  brushSettings: BrushSettings;
  dimensions?: CanvasDimensions;
  zoom: number;
  rotation: number;
  onResetRotation: () => void;
  onResetZoomTo100?: () => void;
  colorMode?: CanvasColorMode;
  showCursorCoordinates?: boolean;
  showPressureInStatusBar?: boolean;
  modules?: Partial<StatusBarModulesConfig>;
  hasOpenDocument?: boolean;
  themePreset?: ThemePreset;
  softProofSettings?: SoftProofSettings;
  valueCheckEnabled?: boolean;
  onToggleValueCheck?: () => void;
}

export const StatusBar: React.FC<StatusBarProps> = React.memo(({
  tool,
  brushSettings,
  dimensions,
  zoom,
  rotation,
  onResetRotation,
  onResetZoomTo100,
  colorMode = 'color',
  showCursorCoordinates = true,
  showPressureInStatusBar = true,
  modules,
  hasOpenDocument = true,
  themePreset: _themePreset = 'modern',
  softProofSettings,
  valueCheckEnabled = false,
  onToggleValueCheck,
}) => {
  const { t, tc } = useI18n();
  const posTextRef = useRef<HTMLSpanElement>(null);
  const pressureBarRef = useRef<HTMLDivElement>(null);
  const pressureTextRef = useRef<HTMLSpanElement>(null);

  const resolvedModules: StatusBarModulesConfig = {
    ...DEFAULT_STATUS_BAR_MODULES,
    ...modules,
  };
  const showCoords = showCursorCoordinates && resolvedModules.cursorCoords;
  const showPressure = showPressureInStatusBar && resolvedModules.pressureMeter;

  useEffect(() => {
    let animId: number;
    let latestDetail: { x: number | null; y: number | null; pressure: number } | null = null;

    const handlePointerStat = (e: Event) => {
      latestDetail = (e as CustomEvent).detail;
      cancelAnimationFrame(animId);
      animId = requestAnimationFrame(() => {
        if (!latestDetail) return;
        if (posTextRef.current) {
          if (
            latestDetail.x !== null &&
            latestDetail.y !== null &&
            latestDetail.x !== undefined &&
            latestDetail.y !== undefined
          ) {
            posTextRef.current.textContent = `${latestDetail.x}, ${latestDetail.y}px`;
          } else {
            posTextRef.current.textContent = '— , —';
          }
        }
        if (pressureBarRef.current && pressureTextRef.current) {
          const percent = Math.round((latestDetail.pressure || 0) * 100);
          pressureBarRef.current.style.width = `${percent}%`;
          pressureTextRef.current.textContent = `${percent}%`;
        }
      });
    };

    window.addEventListener('canvas-pointer-stat', handlePointerStat);
    return () => {
      window.removeEventListener('canvas-pointer-stat', handlePointerStat);
      cancelAnimationFrame(animId);
    };
  }, []);

  return (
    <footer
      data-testid="status-bar"
      data-chrome-bar="status"
      data-theme-preset="modern"
      className="h-7 bg-ui-header border-t border-ui-subtle px-2.5 text-ui-secondary flex items-center justify-between gap-2 text-xs select-none z-20 shrink-0 overflow-x-auto no-scrollbar"
    >
      {/* Left Cluster: Tool Info, Canvas Info & Value Check */}
      <div className="flex items-center gap-1.5 shrink-0">
        {resolvedModules.toolInfo && (
          <div
            data-testid="status-module-tool-info"
            className="bg-ui-input border border-ui-subtle px-2 py-0.5 rounded-ui-sm flex items-center gap-1.5"
          >
            <span className="text-ui-primary capitalize font-medium text-[11px]">
              {tc('toolId', tool)}
            </span>
            <span className="text-ui-disabled">|</span>
            <span className="text-[10px] font-ui-mono text-ui-secondary">
              {brushSettings.smoothing ? t('Bézier') : t('Direct')}
            </span>
            <span className="text-ui-disabled">|</span>
            <span className="text-ui-accent font-ui-mono tabular-nums text-[10px]">
              {t('Stab: {value}', { value: brushSettings.stabilization ?? 0 })}
            </span>
          </div>
        )}

        {resolvedModules.canvasInfo && (
          <div
            data-testid="status-module-canvas-info"
            className="bg-ui-input border border-ui-subtle px-2 py-0.5 rounded-ui-sm flex items-center gap-1.5 font-ui-mono tabular-nums text-[10px]"
          >
            {hasOpenDocument ? (
              <>
                {dimensions && (
                  <>
                    <span className="text-ui-primary" title={t('Canvas Dimensions')}>
                      {dimensions.width}×{dimensions.height}px
                    </span>
                    {dimensions.dpi ? (
                      <span className="text-ui-muted">{dimensions.dpi}dpi</span>
                    ) : null}
                    <span className="text-ui-disabled">|</span>
                  </>
                )}
                <span
                  className={`font-medium ${
                    colorMode === 'monochrome'
                      ? 'text-ui-warning'
                      : colorMode === 'grayscale'
                        ? 'text-ui-secondary'
                        : 'text-ui-accent'
                  }`}
                  title={t('Active Document Color Mode: {mode}', {
                    mode: tc('colorModeId', colorMode),
                  })}
                >
                  {colorMode === 'monochrome'
                    ? t('1-bit Manga')
                    : colorMode === 'grayscale'
                      ? t('Grayscale')
                      : t('Color RGB')}
                </span>
                {softProofSettings?.enabled && (
                  <>
                    <span className="text-ui-disabled">|</span>
                    <span
                      className="text-ui-accent font-medium"
                      title={
                        softProofSettings.gamutWarning
                          ? t('CMYK Soft Proofing: {standard} (Gamut Warning ON)', {
                              standard: softProofSettings.standard,
                            })
                          : t('CMYK Soft Proofing: {standard}', {
                              standard: softProofSettings.standard,
                            })
                      }
                    >
                      {t('[CMYK Proof: {label}]', {
                        label: CMYK_STANDARD_LABELS[softProofSettings.standard] || 'US Web Coated',
                      })}
                    </span>
                  </>
                )}
              </>
            ) : (
              <span className="text-ui-disabled italic">{t('No active document')}</span>
            )}
          </div>
        )}

        {resolvedModules.valueCheck && hasOpenDocument && (valueCheckEnabled || onToggleValueCheck) && (
          <button
            type="button"
            data-testid={valueCheckEnabled ? 'status-value-check-badge' : 'status-value-check-toggle'}
            aria-pressed={valueCheckEnabled}
            onClick={onToggleValueCheck}
            className={`px-2 py-0.5 rounded-ui-sm border font-ui-mono text-[10px] font-medium flex items-center gap-1 transition-colors cursor-pointer ${
              valueCheckEnabled
                ? 'bg-ui-control-active border-ui-accent-border text-ui-accent hover:text-ui-primary'
                : 'bg-ui-input border-ui-subtle text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
            }`}
            title={
              valueCheckEnabled
                ? t('1-Click Value Check (Live B/W Tone Check) is ON — Click or press Shift+Y to disable')
                : t('1-Click Value Check (Shift+Y) — Toggle Live B/W Tone Preview')
            }
          >
            <Contrast className="w-3 h-3 shrink-0" />
            <span>{valueCheckEnabled ? t('[Value Check: B/W]') : t('Value')}</span>
          </button>
        )}
      </div>

      {/* Right Cluster: Pointer Stats, Rotation & Zoom */}
      <div className="flex items-center gap-1.5 font-ui-mono tabular-nums text-xs shrink-0">
        {/* Coordinates */}
        {showCoords && (
          <div
            data-testid="status-module-cursor-coords"
            className="bg-ui-input border border-ui-subtle px-2 py-0.5 rounded-ui-sm flex items-center gap-1.5"
          >
            <span className="text-ui-muted text-[10px]">{t('Pos:')}</span>
            <span ref={posTextRef} className="text-ui-primary text-[11px] tabular-nums">
              — , —
            </span>
          </div>
        )}

        {/* Stylus Pressure Bar */}
        {showPressure && (
          <div
            data-testid="status-module-pressure"
            className="bg-ui-input border border-ui-subtle px-2 py-0.5 rounded-ui-sm flex items-center gap-2"
          >
            <span className="text-ui-muted text-[10px]">{t('Pressure:')}</span>
            <div className="w-14 h-1.5 bg-ui-control rounded-full overflow-hidden">
              <div
                ref={pressureBarRef}
                className="h-full bg-ui-accent transition-all duration-75"
                style={{ width: `0%` }}
              />
            </div>
            <span ref={pressureTextRef} className="w-7 text-right text-ui-secondary text-[10px] tabular-nums">
              0%
            </span>
          </div>
        )}

        {/* Rotation & Zoom */}
        {resolvedModules.rotationZoom && (
          <>
            <div
              data-testid="status-module-rotation"
              onClick={hasOpenDocument ? onResetRotation : undefined}
              onDoubleClick={hasOpenDocument ? onResetRotation : undefined}
              className={`bg-ui-input border border-ui-subtle px-2 py-0.5 rounded-ui-sm flex items-center gap-1.5 transition-colors ${
                hasOpenDocument
                  ? 'cursor-pointer hover:bg-ui-control-hover text-ui-primary'
                  : 'opacity-40 pointer-events-none'
              }`}
              title={t('Canvas Rotation (Click or Double-click to Reset 0°)')}
            >
              <span className="text-ui-muted text-[10px]">{t('Angle:')}</span>
              <span className="text-ui-primary font-medium text-[11px] tabular-nums">
                {hasOpenDocument ? formatRotationDegrees(rotation) : '0°'}
              </span>
            </div>

            <div
              data-testid="status-module-zoom"
              onClick={hasOpenDocument ? onResetZoomTo100 : undefined}
              className={`bg-ui-input border border-ui-subtle px-2 py-0.5 rounded-ui-sm flex items-center gap-1.5 transition-colors ${
                hasOpenDocument
                  ? onResetZoomTo100
                    ? 'cursor-pointer hover:bg-ui-control-hover text-ui-primary'
                    : ''
                  : 'opacity-40 pointer-events-none'
              }`}
              title={
                onResetZoomTo100
                  ? t('Canvas Zoom Level (Click to Reset to 100% — Ctrl+1)')
                  : t('Canvas Zoom Level')
              }
            >
              <span className="text-ui-muted text-[10px]">{t('Zoom:')}</span>
              <span className="text-ui-primary font-medium text-[11px] tabular-nums">
                {hasOpenDocument ? formatZoomPercent(zoom) : '—'}
              </span>
            </div>
          </>
        )}
      </div>
    </footer>
  );
});

StatusBar.displayName = 'StatusBar';