Skip to content

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

7.3 KB
Raw Blame
import React, { useRef } from 'react';
import {
  Blend,
  BoxSelect,
  Droplets,
  Eraser,
  Feather,
  Lasso,
  PaintBucket,
  Paintbrush,
  Pipette,
  Scaling,
  Shapes,
} from 'lucide-react';
import { ToolType } from '../../types/editor';
import { FocusHudPoint } from '../../types/workspace';
import { toolStoreActions, useToolStore } from '../../stores';
import { LiquifyIcon } from '../LiquifyIcon';
import { FocusGripDots, getFocusHudSurfaceStyle, useFocusHudDrag } from './focusHudUtils';
import { useI18n } from '../../i18n';

export interface FocusToolRailProps {
  leftHandedMode: boolean;
  floatingOpacity: number;
  isAutoFaded: boolean;
  customPosition?: FocusHudPoint;
  onTransformSelection?: () => void;
  onToggleTransparentColor?: () => void;
}

export const FocusToolRail: React.FC<FocusToolRailProps> = React.memo(
  ({
    leftHandedMode,
    floatingOpacity,
    isAutoFaded,
    customPosition,
    onTransformSelection,
    onToggleTransparentColor,
  }) => {
    const { t } = useI18n();
    const currentTool = useToolStore((s) => s.tool);
    const primaryColor = useToolStore((s) => s.primaryColor);
    const secondaryColor = useToolStore((s) => s.secondaryColor);
    const isTransparentColor = useToolStore((s) => s.isTransparentColor);

    const railRef = useRef<HTMLDivElement>(null);
    const { isDragging, handleStartDrag, handleResetWidgetPosition } = useFocusHudDrag(
      'toolRail',
      railRef,
      customPosition
    );

    const tools: Array<{
      id: ToolType;
      label: string;
      icon: React.ReactNode;
      onClick?: () => void;
    }> = [
      { id: 'brush', label: t('Brush (B)'), icon: <Paintbrush className="w-3.5 h-3.5" /> },
      { id: 'eraser', label: t('Eraser (E)'), icon: <Eraser className="w-3.5 h-3.5" /> },
      { id: 'pipette', label: t('Eyedropper (I)'), icon: <Pipette className="w-3.5 h-3.5" /> },
      { id: 'select-lasso', label: t('Lasso (L)'), icon: <Lasso className="w-3.5 h-3.5" /> },
      { id: 'select-rect', label: t('Marquee (M)'), icon: <BoxSelect className="w-3.5 h-3.5" /> },
      { id: 'fill', label: t('Fill (G)'), icon: <PaintBucket className="w-3.5 h-3.5" /> },
      { id: 'gradient', label: t('Gradient (Shift+G)'), icon: <Blend className="w-3.5 h-3.5" /> },
      { id: 'liquify', label: t('Liquify (J)'), icon: <LiquifyIcon className="w-3.5 h-3.5" /> },
      { id: 'retouch', label: t('Retouch (S)'), icon: <Droplets className="w-3.5 h-3.5" /> },
      { id: 'shape', label: t('Shapes (U)'), icon: <Shapes className="w-3.5 h-3.5" /> },
      { id: 'pen', label: t('Bézier Pen (P)'), icon: <Feather className="w-3.5 h-3.5" /> },
      {
        id: 'transform',
        label: t('Transform (Ctrl+T)'),
        icon: <Scaling className="w-3.5 h-3.5" />,
        onClick: () => {
          toolStoreActions.selectTool('transform');
          onTransformSelection?.();
        },
      },
    ];

    const isFixed = Boolean(customPosition);
    const anchorSide = isFixed ? 'custom' : leftHandedMode ? 'right' : 'left';
    const anchorClass = isFixed
      ? ''
      : leftHandedMode
        ? 'absolute top-3 right-3'
        : 'absolute top-3 left-3';

    const surfaceStyle = getFocusHudSurfaceStyle(
      floatingOpacity,
      isAutoFaded,
      isFixed
        ? {
            position: 'fixed',
            left: customPosition!.x,
            top: customPosition!.y,
            zIndex: isDragging ? 49 : 42,
          }
        : { zIndex: 42 }
    );

    return (
      <aside
        ref={railRef}
        role="toolbar"
        aria-label={t('Focus Tool Rail')}
        aria-orientation="vertical"
        data-testid="focus-tool-rail"
        data-anchor-side={anchorSide}
        data-hud-blur={floatingOpacity < 0.98 ? 'true' : 'false'}
        data-auto-faded={String(isAutoFaded)}
        onPointerDown={(e) => e.stopPropagation()}
        onWheel={(e) => e.stopPropagation()}
        style={surfaceStyle}
        className={`${anchorClass} w-[38px] py-1.5 px-1 rounded-ui-lg border shadow-ui-floating flex flex-col items-center gap-0.5 max-h-[calc(100vh-196px)] overflow-y-auto no-scrollbar transition-[opacity,border-color,box-shadow] duration-150 select-none ui-icon-stroke ${
          isDragging ? 'border-ui-strong shadow-ui-popover' : 'border-ui-default'
        }`}
      >
        {/* Top Drag Handle */}
        <div
          data-testid="focus-tool-rail-drag-handle"
          onPointerDown={handleStartDrag}
          onDoubleClick={handleResetWidgetPosition}
          title={t('Drag to move Tool Rail · Double-click to snap back')}
          className="group w-full pb-1 mb-0.5 flex items-center justify-center border-b border-ui-subtle cursor-grab active:cursor-grabbing shrink-0"
        >
          <FocusGripDots orientation="horizontal" />
        </div>

        {tools.map((item) => {
          const isActive = currentTool === item.id;
          return (
            <button
              key={item.id}
              type="button"
              data-testid={`focus-tool-${item.id}`}
              aria-label={item.label}
              aria-pressed={isActive}
              title={item.label}
              onClick={() =>
                item.onClick ? item.onClick() : toolStoreActions.selectTool(item.id)
              }
              className={`relative w-7 h-7 rounded-ui-sm flex items-center justify-center transition-colors shrink-0 cursor-pointer ${
                isActive
                  ? 'bg-ui-control-active text-ui-accent'
                  : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover'
              }`}
            >
              {isActive && (
                <span
                  aria-hidden="true"
                  className={`absolute top-1.5 bottom-1.5 w-[2px] rounded-full bg-ui-accent ${
                    leftHandedMode ? 'right-0' : 'left-0'
                  }`}
                />
              )}
              {item.icon}
            </button>
          );
        })}

        {/* Compact Color Swatches */}
        <div className="pt-1.5 mt-0.5 border-t border-ui-subtle flex flex-col items-center gap-1 shrink-0">
          <div className="relative w-5 h-5">
            <button
              type="button"
              onClick={() => toolStoreActions.swapColors()}
              title={t('Secondary: {color} (X to swap)', { color: secondaryColor })}
              aria-label={t('Swap colors')}
              className="absolute bottom-0 right-0 w-3.5 h-3.5 rounded-ui-xs ring-1 ring-ui-strong cursor-pointer"
              style={{ backgroundColor: secondaryColor }}
            />
            <button
              type="button"
              onClick={() => {
                if (isTransparentColor) {
                  if (onToggleTransparentColor) onToggleTransparentColor();
                  else toolStoreActions.toggleTransparentColor();
                } else {
                  toolStoreActions.swapColors();
                }
              }}
              title={t('Primary: {color}', { color: primaryColor })}
              aria-label={t('Primary color swatch')}
              className={`absolute top-0 left-0 w-3.5 h-3.5 rounded-ui-xs z-10 cursor-pointer ${
                !isTransparentColor ? 'ring-[1.5px] ring-ui-accent' : 'ring-1 ring-ui-strong opacity-70'
              }`}
              style={{ backgroundColor: primaryColor }}
            />
          </div>
        </div>
      </aside>
    );
  }
);
FocusToolRail.displayName = 'FocusToolRail';