Skip to content

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

10.0 KB
Raw Blame
import React, { useMemo } from 'react';
import { createPortal } from 'react-dom';
import { CanvasSelection, CanvasDimensions } from '../types/editor';
import {
  X,
  Trash2,
  RotateCcw,
  Scissors,
  Copy,
  ClipboardPaste,
  Layers,
  Crop,
  Maximize2,
  Minimize2,
  Feather,
} from 'lucide-react';
import { useI18n } from '../i18n';

interface SelectionOverlayProps {
  selection: CanvasSelection | null;
  dimensions: CanvasDimensions;
  zoom: number;
  flipX?: boolean;
  flipY?: boolean;
  onDeselect: () => void;
  onTransformSelection: () => void;
  onFillScreentone: () => void;
  onFillColor: () => void;
  onClearSelection: () => void;
  onDeleteOutsideSelection?: () => void;
  onCutSelection?: () => void;
  onCopySelection?: () => void;
  onPasteSelection?: () => void;
  onMoveSelectionToNewLayer?: () => void;
  onInvertSelection: () => void;
  onExpandSelection?: (pixels: number) => void;
  onContractSelection?: (pixels: number) => void;
  onFeatherSelection?: (radius: number) => void;
}

/**
 * Lightweight Selection UI Overlay (Phase 6 Native DX12 GPU Marching Ants).
 *
 * Committed selection borders (marching ants) and floating lifted selection pixels
 * render directly on the DX12 GPU in `ink-engine::selection::gpu_ants` at display resolution
 * (zero DOM floating `<canvas>`, zero heavy SVG edge paths).
 * This component renders ONLY:
 * 1. A tiny lightweight SVG rubber-band outline while actively dragging a new rect/lasso selection.
 * 2. The viewport-anchored Selection Action Bar portal.
 */
export const SelectionOverlay: React.FC<SelectionOverlayProps> = ({
  selection,
  dimensions,
  zoom,
  onDeselect,
  onTransformSelection,
  onFillScreentone,
  onFillColor,
  onClearSelection,
  onDeleteOutsideSelection,
  onCutSelection,
  onCopySelection,
  onPasteSelection,
  onMoveSelectionToNewLayer,
  onInvertSelection,
  onExpandSelection,
  onContractSelection,
  onFeatherSelection,
}) => {
  const { t, tc } = useI18n();
  const invZoom = 1 / Math.max(0.05, zoom || 1);

  const draftPathD = useMemo(() => {
    if (!selection?.isDraftPreview) return '';
    const { type, bounds, polygon } = selection;
    if (type === 'rect' && bounds) {
      return `M ${bounds.x} ${bounds.y} H ${bounds.x + bounds.width} V ${bounds.y + bounds.height} H ${bounds.x} Z`;
    }
    if (type === 'lasso' && polygon && polygon.length > 1) {
      return (
        `M ${polygon[0].x} ${polygon[0].y} ` +
        polygon
          .slice(1)
          .map((p) => `L ${p.x} ${p.y}`)
          .join(' ') +
        (polygon.length > 2 ? ' Z' : '')
      );
    }
    return '';
  }, [selection]);

  if (!selection || !selection.bounds) return null;

  return (
    <>
      {/* Live lightweight drag rubber-band only before commit (committed ants render on DX12 GPU) */}
      {draftPathD && (
        <svg
          className="absolute inset-0 pointer-events-none z-24 overflow-visible"
          width={dimensions.width}
          height={dimensions.height}
          viewBox={`0 0 ${dimensions.width} ${dimensions.height}`}
        >
          <path d={draftPathD} stroke="#000000" strokeWidth={1.5 * invZoom} fill="none" />
          <path
            d={draftPathD}
            stroke="#ffffff"
            strokeWidth={1.5 * invZoom}
            strokeDasharray={`${4 * invZoom} ${4 * invZoom}`}
            fill="none"
          />
        </svg>
      )}

      {/* Viewport-anchored Selection Action Bar */}
      {!selection.isDraftPreview &&
        createPortal(
          <div
            data-native-occluder="true"
            className="fixed bottom-10 left-1/2 -translate-x-1/2 z-50 max-w-[95vw] bg-ui-floating border border-ui-default rounded-ui-md shadow-ui-floating px-2 py-1 flex flex-wrap items-center justify-center gap-1 select-none text-[11px] text-ui-primary"
            onPointerDown={(e) => e.stopPropagation()}
          >
            <button
              type="button"
              onClick={onDeselect}
              title={t('Deselect (Ctrl+D)')}
              className="px-1.5 py-1 text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
            >
              <X className="w-3.5 h-3.5" />
              <span>{t('Deselect')}</span>
            </button>

            <button
              type="button"
              onClick={onInvertSelection}
              title={t('Invert Selection (Ctrl+Shift+I)')}
              className="px-1.5 py-1 text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
            >
              <RotateCcw className="w-3.5 h-3.5" />
              <span>{t('Invert')}</span>
            </button>

            <button
              type="button"
              onClick={() => onExpandSelection?.(5)}
              title={t('Expand Selection (+5px)')}
              className="p-1 text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs transition-colors cursor-pointer"
            >
              <Maximize2 className="w-3.5 h-3.5" />
            </button>

            <button
              type="button"
              onClick={() => onContractSelection?.(5)}
              title={t('Contract Selection (-5px)')}
              className="p-1 text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs transition-colors cursor-pointer"
            >
              <Minimize2 className="w-3.5 h-3.5" />
            </button>

            <button
              type="button"
              onClick={() => onFeatherSelection?.(5)}
              title={t('Feather Selection (5px)')}
              className="p-1 text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs transition-colors cursor-pointer"
            >
              <Feather className="w-3.5 h-3.5" />
            </button>

            <div className="w-px h-4 bg-ui-subtle" />

            {onCutSelection && (
              <button
                type="button"
                onClick={onCutSelection}
                title={t('Cut Selection (Ctrl+X)')}
                className="px-1.5 py-1 text-ui-primary hover:bg-ui-control-hover rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
              >
                <Scissors className="w-3.5 h-3.5" />
                <span>{t('Cut')}</span>
              </button>
            )}

            {onCopySelection && (
              <button
                type="button"
                onClick={onCopySelection}
                title={t('Copy Selection (Ctrl+C)')}
                className="px-1.5 py-1 text-ui-primary hover:bg-ui-control-hover rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
              >
                <Copy className="w-3.5 h-3.5" />
                <span>{t('Copy')}</span>
              </button>
            )}

            {onPasteSelection && (
              <button
                type="button"
                onClick={onPasteSelection}
                title={t('Paste to New Layer (Ctrl+V)')}
                className="px-1.5 py-1 text-ui-primary hover:bg-ui-control-hover rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
              >
                <ClipboardPaste className="w-3.5 h-3.5" />
                <span>{t('Paste')}</span>
              </button>
            )}

            {onMoveSelectionToNewLayer && (
              <button
                type="button"
                onClick={onMoveSelectionToNewLayer}
                title={t('Move Selected Section to New Layer (Ctrl+Shift+J)')}
                className="px-1.5 py-1 text-ui-accent hover:bg-ui-control-hover rounded-ui-xs flex items-center gap-1 font-medium transition-colors cursor-pointer"
              >
                <Layers className="w-3.5 h-3.5" />
                <span>{t('To Layer')}</span>
              </button>
            )}

            <div className="w-px h-4 bg-ui-subtle" />

            <button
              type="button"
              onClick={onClearSelection}
              title={t('Delete Selected Section (Del)')}
              className="px-1.5 py-1 text-ui-secondary hover:text-ui-danger hover:bg-ui-danger/15 rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
            >
              <Trash2 className="w-3.5 h-3.5" />
              <span>{t('Delete')}</span>
            </button>

            {onDeleteOutsideSelection && (
              <button
                type="button"
                onClick={onDeleteOutsideSelection}
                title={t('Delete Outside Selected Section')}
                className="px-1.5 py-1 text-ui-secondary hover:text-ui-danger hover:bg-ui-danger/15 rounded-ui-xs flex items-center gap-1 transition-colors cursor-pointer"
              >
                <Crop className="w-3.5 h-3.5" />
                <span>{t('Del Outside')}</span>
              </button>
            )}

            <div className="w-px h-4 bg-ui-subtle" />

            <button
              type="button"
              onClick={onFillColor}
              title={t('Fill with Foreground Color')}
              className="px-2 py-1 text-ui-primary hover:bg-ui-control-hover rounded-ui-xs transition-colors cursor-pointer"
            >
              {tc('action', 'Fill')}
            </button>

            <button
              type="button"
              onClick={onFillScreentone}
              title={t('Fill Screentone into New Layer')}
              className="px-2 py-1 bg-ui-accent-subtle hover:bg-ui-accent-hover text-ui-accent border border-ui-accent-border rounded-ui-xs transition-colors cursor-pointer font-medium"
            >
              {t('Screentone')}
            </button>

            <button
              type="button"
              onClick={onTransformSelection}
              title={t('Transform Selection (Ctrl+T)')}
              className="px-2 py-1 text-ui-primary hover:bg-ui-control-hover rounded-ui-xs font-medium transition-colors cursor-pointer"
            >
              {t('Transform')}
            </button>
          </div>,
          document.body
        )}
    </>
  );
};