Skip to content

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

9.1 KB
Raw Blame
import React, { useEffect } from 'react';
import { Paintbrush, Eraser, ArrowLeftRight, Pipette, Sparkles } from 'lucide-react';
import { ToolType } from '../types/editor';
import { LiquifyIcon } from './LiquifyIcon';
import { isImeComposing, useI18n } from '../i18n';

interface LeftSidebarProps {
  currentTool: ToolType;
  onSelectTool: (tool: ToolType) => void;
  primaryColor: string;
  secondaryColor: string;
  onSwapColors: () => void;
  isTransparentColor?: boolean;
  onToggleTransparentColor?: () => void;
}

export const LeftSidebar: React.FC<LeftSidebarProps> = ({
  currentTool,
  onSelectTool,
  primaryColor,
  secondaryColor,
  onSwapColors,
  isTransparentColor = false,
  onToggleTransparentColor,
}) => {
  const { t } = useI18n();

  // Keyboard shortcuts: B for Brush, E for Eraser, I for Pipette, J for Liquify, X for swap colors
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (isImeComposing(e)) return;
      if (['INPUT', 'TEXTAREA'].includes((e.target as HTMLElement).tagName)) {
        return;
      }
      if (e.ctrlKey || e.metaKey || e.altKey) {
        return;
      }
      const key = e.key.toLowerCase();
      if (key === 'b') {
        onSelectTool('brush');
      } else if (key === 'e') {
        onSelectTool('eraser');
      } else if (key === 'i') {
        onSelectTool('pipette');
      } else if (key === 'j') {
        onSelectTool('liquify');
      } else if (key === 'x') {
        onSwapColors();
      } else if (key === 'c') {
        onToggleTransparentColor?.();
      }
    };

    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [onSelectTool, onSwapColors, onToggleTransparentColor]);

  return (
    <aside className="w-14 bg-ui-panel border-r border-ui-subtle flex flex-col items-center py-3 select-none z-10 shrink-0">
      <div className="flex flex-col items-center space-y-2">
        {/* Brush Button */}
        <button
          type="button"
          onClick={() => onSelectTool('brush')}
          title={t('Brush Tool (B)')}
          className={`w-10 h-10 rounded-ui-sm flex items-center justify-center transition-colors relative group cursor-pointer ${
            currentTool === 'brush'
              ? 'bg-ui-control-active border border-ui-default text-ui-accent'
              : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover border border-transparent'
          }`}
        >
          <Paintbrush className="w-5 h-5" />
          {/* Tooltip */}
          <div className="absolute left-12 bg-ui-elevated text-ui-primary text-xs px-2 py-1 rounded-ui-sm border border-ui-default shadow-ui-floating opacity-0 pointer-events-none group-hover:opacity-100 transition-opacity whitespace-nowrap z-50">
            {t('Brush (B)')}
          </div>
        </button>

        {/* Decoration Button */}
        <button
          type="button"
          onClick={() => onSelectTool('decoration')}
          title={t('Decoration Tool')}
          className={`w-10 h-10 rounded-ui-sm flex items-center justify-center transition-colors relative group cursor-pointer ${
            currentTool === 'decoration'
              ? 'bg-ui-control-active border border-ui-default text-ui-accent'
              : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover border border-transparent'
          }`}
        >
          <Sparkles className="w-5 h-5" />
          <div className="absolute left-12 bg-ui-elevated text-ui-primary text-xs px-2 py-1 rounded-ui-sm border border-ui-default shadow-ui-floating opacity-0 pointer-events-none group-hover:opacity-100 transition-opacity whitespace-nowrap z-50">
            {t('Decoration')}
          </div>
        </button>

        {/* Eraser Button */}
        <button
          type="button"
          onClick={() => onSelectTool('eraser')}
          title={t('Eraser Tool (E)')}
          className={`w-10 h-10 rounded-ui-sm flex items-center justify-center transition-colors relative group cursor-pointer ${
            currentTool === 'eraser'
              ? 'bg-ui-control-active border border-ui-default text-ui-accent'
              : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover border border-transparent'
          }`}
        >
          <Eraser className="w-5 h-5" />
          <div className="absolute left-12 bg-ui-elevated text-ui-primary text-xs px-2 py-1 rounded-ui-sm border border-ui-default shadow-ui-floating opacity-0 pointer-events-none group-hover:opacity-100 transition-opacity whitespace-nowrap z-50">
            {t('Eraser (E)')}
          </div>
        </button>

        {/* Pipette Button */}
        <button
          type="button"
          onClick={() => onSelectTool('pipette')}
          title={t('Pipette Tool (I / Hold Alt)')}
          className={`w-10 h-10 rounded-ui-sm flex items-center justify-center transition-colors relative group cursor-pointer ${
            currentTool === 'pipette'
              ? 'bg-ui-control-active border border-ui-default text-ui-accent'
              : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover border border-transparent'
          }`}
        >
          <Pipette className="w-5 h-5" />
          <div className="absolute left-12 bg-ui-elevated text-ui-primary text-xs px-2 py-1 rounded-ui-sm border border-ui-default shadow-ui-floating opacity-0 pointer-events-none group-hover:opacity-100 transition-opacity whitespace-nowrap z-50">
            {t('Pipette (I / Hold Alt)')}
          </div>
        </button>

        {/* Liquify Button */}
        <button
          type="button"
          onClick={() => onSelectTool('liquify')}
          title={t('Liquify Tool (J)')}
          className={`w-10 h-10 rounded-ui-sm flex items-center justify-center transition-colors relative group cursor-pointer ${
            currentTool === 'liquify'
              ? 'bg-ui-control-active border border-ui-default text-ui-accent'
              : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover border border-transparent'
          }`}
        >
          <LiquifyIcon className="w-5 h-5" />
          <div className="absolute left-12 bg-ui-elevated text-ui-primary text-xs px-2 py-1 rounded-ui-sm border border-ui-default shadow-ui-floating opacity-0 pointer-events-none group-hover:opacity-100 transition-opacity whitespace-nowrap z-50">
            {t('Liquify (J)')}
          </div>
        </button>
      </div>

      <div className="my-3 w-8 border-t border-ui-subtle" />

      {/* Primary & Secondary Color Indicator Swatches */}
      <div className="mt-auto flex flex-col items-center space-y-2 pb-1">
        <div className="relative w-9 h-9">
          {/* Secondary Color Swatch (bottom-right) */}
          <div
            onClick={onSwapColors}
            title={t('Secondary Color: {color} (Click or press X to swap)', { color: secondaryColor })}
            className="absolute bottom-0 right-0 w-6 h-6 rounded-ui-xs border border-ui-default cursor-pointer hover:border-ui-strong shadow-sm z-0"
            style={{ backgroundColor: secondaryColor }}
          />

          {/* Primary Color Swatch (top-left) */}
          <div
            onClick={() => {
              if (isTransparentColor) {
                onToggleTransparentColor?.();
              } else {
                onSwapColors();
              }
            }}
            title={t('Primary Active Color: {color} (Click to select/swap)', { color: primaryColor })}
            className={`absolute top-0 left-0 w-6 h-6 rounded-ui-xs cursor-pointer transition-all z-10 ${
              !isTransparentColor
                ? 'border border-ui-accent ring-1 ring-ui-accent-border'
                : 'border border-ui-default hover:border-ui-strong opacity-80'
            }`}
            style={{ backgroundColor: primaryColor }}
          />
        </div>

        {/* Transparent Color Swatch */}
        <div
          onClick={onToggleTransparentColor}
          title={t('Transparent Ink / Color (C) - Brush as textured eraser')}
          className={`w-7 h-3.5 rounded-ui-xs border cursor-pointer relative overflow-hidden transition-all ${
            isTransparentColor
              ? 'border-ui-accent ring-1 ring-ui-accent-border'
              : 'border-ui-default hover:border-ui-strong opacity-80 hover:opacity-100'
          }`}
          style={{
            backgroundImage:
              'linear-gradient(45deg, #777 25%, transparent 25%), linear-gradient(-45deg, #777 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #777 75%), linear-gradient(-45deg, transparent 75%, #777 75%)',
            backgroundSize: '4px 4px',
            backgroundPosition: '0 0, 0 2px, 2px -2px, -2px 0px',
            backgroundColor: '#bbb',
          }}
        />

        {/* Swap button */}
        <button
          type="button"
          onClick={onSwapColors}
          title={t('Swap Colors (X)')}
          className="p-1 text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-sm transition-colors cursor-pointer"
        >
          <ArrowLeftRight className="w-3.5 h-3.5" />
        </button>

        <span className="text-[9px] text-ui-muted font-ui-mono">
          X / C
        </span>
      </div>
    </aside>
  );
};