Skip to content

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

14.5 KB
Raw Blame
import React, { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
import { Clock, Crosshair, Layers, Cpu, Monitor, Trash2 } from 'lucide-react';
import { CanvasColorMode, LayerItem } from '../types/editor';
import { ThemePreset, hexToRgb } from '../types/settings';
import { useDocumentStore, useToolStore, uiStoreActions } from '../stores';
import { performanceManager } from '../utils/performanceManager';
import { recoveryService } from '../services/recoveryService';
import {
  ArtworkTimeTracker,
  artworkTimeTracker,
  formatWorkTimeClock,
  formatWorkTimeHoursMinutes,
} from '../utils/artworkTimeTracker';
import { useI18n } from '../i18n';

function rgbToHsv(r: number, g: number, b: number): { h: number; s: number; v: number } {
  const rn = r / 255;
  const gn = g / 255;
  const bn = b / 255;
  const max = Math.max(rn, gn, bn);
  const min = Math.min(rn, gn, bn);
  const d = max - min;
  let h = 0;
  if (d !== 0) {
    if (max === rn) h = ((gn - bn) / d) % 6;
    else if (max === gn) h = (bn - rn) / d + 2;
    else h = (rn - gn) / d + 4;
    h = Math.round(h * 60);
    if (h < 0) h += 360;
  }
  const s = max === 0 ? 0 : Math.round((d / max) * 100);
  const v = Math.round(max * 100);
  return { h, s, v };
}

function formatColorModeLabel(mode: CanvasColorMode): string {
  switch (mode) {
    case 'monochrome':
      return '1-bit Mono';
    case 'grayscale':
      return 'Grayscale';
    case 'color':
    default:
      return 'RGB Color';
  }
}

export interface InfoPanelProps {
  layers?: LayerItem[];
  colorMode?: CanvasColorMode;
  themePreset?: ThemePreset;
  /** Optional custom tracker instance (primarily for unit tests). */
  tracker?: ArtworkTimeTracker;
}

export const InfoPanel: React.FC<InfoPanelProps> = React.memo(
  ({ layers = [], colorMode: colorModeProp, themePreset = 'modern', tracker = artworkTimeTracker }) => {
    const { t } = useI18n();
    const activeDocumentId = useDocumentStore((s) => s.activeDocumentId);
    const dimensions = useDocumentStore((s) => s.dimensions);
    const storeColorMode = useDocumentStore((s) => s.colorMode);
    const primaryColor = useToolStore((s) => s.primaryColor);
    const colorMode = colorModeProp ?? storeColorMode;

    const getSnapshot = useCallback(
      () => tracker.getSnapshot(activeDocumentId),
      [tracker, activeDocumentId]
    );

    const timeSnap = useSyncExternalStore(tracker.subscribe, getSnapshot, getSnapshot);

    // Zero-rerender live cursor X / Y / Pressure readout via canvas-pointer-stat
    const posXRef = useRef<HTMLSpanElement | null>(null);
    const posYRef = useRef<HTMLSpanElement | null>(null);
    const pressureRef = useRef<HTMLSpanElement | null>(null);

    useEffect(() => {
      if (typeof window === 'undefined') return;
      let rafId = 0;
      let latest: { x: number | null; y: number | null; pressure: number } | null = null;

      const handlePointerStat = (e: Event) => {
        latest = (e as CustomEvent).detail;
        cancelAnimationFrame(rafId);
        rafId = requestAnimationFrame(() => {
          if (!latest) return;
          const hasCoords =
            latest.x !== null &&
            latest.y !== null &&
            latest.x !== undefined &&
            latest.y !== undefined;
          if (posXRef.current) {
            posXRef.current.textContent = hasCoords ? String(latest.x) : '—';
          }
          if (posYRef.current) {
            posYRef.current.textContent = hasCoords ? String(latest.y) : '—';
          }
          if (pressureRef.current) {
            const pct = Math.round((latest.pressure || 0) * 100);
            pressureRef.current.textContent = `${pct}%`;
          }
        });
      };

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

    // Lightweight periodic memory & renderer poll (every 2s while Info tab is visible)
    const [memoryStats, setMemoryStats] = useState<{
      usedMb: number;
      budgetMb: number;
      backend: 'dx12' | 'webgl2' | 'canvas2d';
      vramMb: number;
    }>({
      usedMb: 0,
      budgetMb: 1024,
      backend: 'dx12',
      vramMb: 0,
    });

    useEffect(() => {
      let active = true;
      const updateMem = async () => {
        if (recoveryService.getIsDrawingActive()) return;
        const t = await performanceManager.getMemoryTelemetry();
        if (!active) return;
        const usedMb = Math.round(t.activeTileMb * 10) / 10;
        const budgetMb = Math.max(1, Math.round(t.tileBudgetMb));
        const backend = t.compositor.backend;
        const vramMb = t.compositor.estimatedVramMb;
        setMemoryStats((prev) =>
          prev.usedMb === usedMb &&
          prev.budgetMb === budgetMb &&
          prev.backend === backend &&
          prev.vramMb === vramMb
            ? prev
            : { usedMb, budgetMb, backend, vramMb }
        );
      };
      void updateMem();
      const id = setInterval(() => void updateMem(), 2000);
      return () => {
        active = false;
        clearInterval(id);
      };
    }, []);

    const colorBreakdown = useMemo(() => {
      const rgb = hexToRgb(primaryColor) || { r: 0, g: 0, b: 0 };
      const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
      return { rgb, hsv, hex: primaryColor.toUpperCase() };
    }, [primaryColor]);

    const visibleLayerCount = useMemo(
      () => layers.filter((l) => l.visible).length,
      [layers]
    );

    const memPercent = Math.min(100, Math.round((memoryStats.usedMb / memoryStats.budgetMb) * 100));

    return (
      <div
        data-testid="info-panel"
        data-theme-preset={themePreset}
        className="flex flex-col h-full min-h-0 overflow-y-auto no-scrollbar select-none bg-ui-panel text-ui-primary p-2 gap-2 text-[11px]"
      >
        {/* Clean Automatic Artwork Time Counter (Text-Only, No Manual Controls) */}
        <section
          data-testid="info-work-time-section"
          data-status={timeSnap.status}
          className="rounded-ui-md bg-ui-input border border-ui-subtle px-2 py-1.5 flex flex-col gap-1"
          title={t('Automatically tracks active time spent drawing and editing this artwork')}
        >
          <div className="flex items-center justify-between gap-2">
            <span className="flex items-center gap-1 text-[10px] text-ui-muted">
              <Clock className="w-3 h-3 text-ui-icon-muted" />
              <span>{t('Work Time')}</span>
            </span>
            <span
              data-testid="info-work-time-hm"
              className="font-ui-mono tabular-nums text-[10px] text-ui-muted"
            >
              {formatWorkTimeHoursMinutes(timeSnap.accumulatedMs)}
            </span>
          </div>

          <div className="flex items-baseline justify-between gap-2">
            <span
              data-testid="info-work-time-clock"
              className="font-ui-mono tabular-nums text-[15px] leading-tight font-medium text-ui-primary tracking-tight"
            >
              {formatWorkTimeClock(timeSnap.accumulatedMs)}
            </span>
            <span
              data-testid="info-session-time"
              className="font-ui-mono tabular-nums text-[10px] text-ui-muted"
              title={t('Time spent in the current session')}
            >
              {t('Session {time}', { time: formatWorkTimeClock(timeSnap.sessionMs) })}
            </span>
          </div>
        </section>

        {/* CSP-Style Cursor Coordinates & Active Color */}
        <section className="rounded-ui-md bg-ui-input border border-ui-subtle p-2 flex flex-col gap-1.5">
          <div className="flex items-center justify-between text-[10px] text-ui-muted">
            <div className="flex items-center gap-1 uppercase tracking-wider font-medium">
              <Crosshair className="w-3 h-3 text-ui-icon-muted" />
              <span>{t('Cursor')}</span>
            </div>
            <span className="font-ui-mono tabular-nums text-ui-muted">
              {t('P:')} <span ref={pressureRef} className="text-ui-primary">0%</span>
            </span>
          </div>

          <div className="grid grid-cols-2 gap-1.5 font-ui-mono tabular-nums text-[10px]">
            <div className="flex items-center justify-between px-1.5 py-0.5 rounded-ui-xs bg-ui-panel border border-ui-subtle">
              <span className="text-ui-muted">X</span>
              <span ref={posXRef} data-testid="info-cursor-x" className="text-ui-primary">
                —
              </span>
            </div>
            <div className="flex items-center justify-between px-1.5 py-0.5 rounded-ui-xs bg-ui-panel border border-ui-subtle">
              <span className="text-ui-muted">Y</span>
              <span ref={posYRef} data-testid="info-cursor-y" className="text-ui-primary">
                —
              </span>
            </div>
          </div>

          {/* Active Color RGB / HSV */}
          <div className="flex items-center gap-2 pt-1 border-t border-ui-subtle">
            <div
              className="w-6 h-6 rounded-ui-xs border border-ui-default shrink-0 shadow-inner"
              style={{ backgroundColor: colorBreakdown.hex }}
              title={t('Active Color: {color}', { color: colorBreakdown.hex })}
            />
            <div className="flex-1 min-w-0 grid grid-cols-2 gap-x-2 gap-y-0.5 font-ui-mono tabular-nums text-[10px]">
              <div className="flex justify-between">
                <span className="text-ui-muted">R</span>
                <span className="text-ui-primary">{colorBreakdown.rgb.r}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-ui-muted">H</span>
                <span className="text-ui-primary">{colorBreakdown.hsv.h}°</span>
              </div>
              <div className="flex justify-between">
                <span className="text-ui-muted">G</span>
                <span className="text-ui-primary">{colorBreakdown.rgb.g}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-ui-muted">S</span>
                <span className="text-ui-primary">{colorBreakdown.hsv.s}%</span>
              </div>
              <div className="flex justify-between">
                <span className="text-ui-muted">B</span>
                <span className="text-ui-primary">{colorBreakdown.rgb.b}</span>
              </div>
              <div className="flex justify-between">
                <span className="text-ui-muted">V</span>
                <span className="text-ui-primary">{colorBreakdown.hsv.v}%</span>
              </div>
            </div>
          </div>
        </section>

        {/* Canvas & Memory Info (CSP Info Palette Style) */}
        <section className="rounded-ui-md bg-ui-input border border-ui-subtle p-2 flex flex-col gap-1">
          <div className="flex items-center justify-between text-[10px]">
            <span className="text-ui-muted">{t('Canvas')}</span>
            <span className="font-ui-mono tabular-nums text-ui-primary">
              {dimensions.width} × {dimensions.height} px
            </span>
          </div>
          <div className="flex items-center justify-between text-[10px]">
            <span className="text-ui-muted">{t('Resolution')}</span>
            <span className="font-ui-mono tabular-nums text-ui-primary">
              {dimensions.dpi ?? 300} dpi · {t(formatColorModeLabel(colorMode))}
            </span>
          </div>
          <div className="flex items-center justify-between text-[10px]">
            <span className="text-ui-muted flex items-center gap-1">
              <Layers className="w-2.5 h-2.5 text-ui-icon-muted" />
              <span>{t('Layers')}</span>
            </span>
            <span className="font-ui-mono tabular-nums text-ui-primary">
              {visibleLayerCount} / {layers.length}
            </span>
          </div>
          <div className="flex items-center justify-between text-[10px]">
            <span className="text-ui-muted flex items-center gap-1">
              <Monitor className="w-2.5 h-2.5 text-ui-icon-muted" />
              <span>{t('Engine')}</span>
            </span>
            <span className="font-ui-mono tabular-nums text-ui-primary">
              {memoryStats.backend === 'dx12'
                ? `DX12 (wgpu)${memoryStats.vramMb > 0 ? ` · ${memoryStats.vramMb} MB` : ''}`
                : memoryStats.backend === 'webgl2'
                  ? `WebGL2${memoryStats.vramMb > 0 ? ` · ${memoryStats.vramMb} MB` : ''}`
                  : t('Canvas 2D (CPU)')}
            </span>
          </div>
          <div className="flex flex-col gap-1 pt-1 mt-0.5 border-t border-ui-subtle">
            <div className="flex items-center justify-between text-[10px]">
              <span className="text-ui-muted flex items-center gap-1">
                <Cpu className="w-2.5 h-2.5 text-ui-icon-muted" />
                <span>{t('Tile RAM')}</span>
              </span>
              <div className="flex items-center gap-1.5">
                <span className="font-ui-mono tabular-nums text-ui-primary">
                  {memoryStats.usedMb} / {memoryStats.budgetMb} MB
                </span>
                <button
                  type="button"
                  onClick={async () => {
                    const res = await performanceManager.purgeMemory('all');
                    uiStoreActions.showToast(
                      res.freedTileMb > 0 || res.offloadedUndoSteps > 0
                        ? t('Freed {mb} MB tiles, {steps} undo steps', {
                            mb: res.freedTileMb,
                            steps: res.offloadedUndoSteps,
                          })
                        : t('Memory & GPU caches flushed')
                    );
                  }}
                  className="p-0.5 rounded-ui-xs text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
                  title={t('Purge Tile, Undo & GPU Caches')}
                >
                  <Trash2 className="w-2.5 h-2.5" />
                </button>
              </div>
            </div>
            <div className="w-full h-1 rounded-full bg-ui-control overflow-hidden">
              <div
                className={`h-full rounded-full transition-all duration-300 ${
                  memPercent > 85 ? 'bg-ui-danger' : 'bg-ui-accent'
                }`}
                style={{
                  width: `${memPercent}%`,
                }}
              />
            </div>
          </div>
        </section>
      </div>
    );
  }
);

InfoPanel.displayName = 'InfoPanel';