Skip to content

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

5.7 KB
Raw Blame
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { CspBrush, isBrushLikeTool } from '../types/editor';
import {
  loadCustomBrushes,
  getCachedCustomBrushes,
  preloadTipImage,
  CUSTOM_BRUSHES_CHANGED_EVENT,
} from '../utils/brushStorage';
import { useToolStore, useUiStore, uiStoreActions } from '../stores';
import { ToolStrip } from './ToolStrip';
import { SubToolList } from './SubToolList';
import { ToolPropertyInspector } from './ToolPropertyInspector';
import { useI18n } from '../i18n';

export interface LeftPanelProps {
  onDeselect?: () => void;
  onTransformSelection?: () => void;
  onClearSelection?: () => void;
  onDeleteOutsideSelection?: () => void;
  onCutSelection?: () => void;
  onCopySelection?: () => void;
  onPasteSelection?: () => void;
  onMoveSelectionToNewLayer?: () => void;
  onExpandSelection?: (pixels: number) => void;
  onContractSelection?: (pixels: number) => void;
  onFeatherSelection?: (radius: number) => void;
  onInvertSelection?: () => void;
  onToggleTransparentColor?: () => void;
}

export const LeftPanel: React.FC<LeftPanelProps> = React.memo(
  ({
    onDeselect,
    onTransformSelection,
    onClearSelection,
    onDeleteOutsideSelection,
    onCutSelection,
    onCopySelection,
    onPasteSelection,
    onMoveSelectionToNewLayer,
    onExpandSelection,
    onContractSelection,
    onFeatherSelection,
    onInvertSelection,
    onToggleTransparentColor,
  }) => {
    const { t } = useI18n();
    const currentTool = useToolStore((s) => s.tool);
    const width = useUiStore((s) => s.leftPanelWidth);
    const isHidden = useUiStore((s) => s.isLeftPanelHidden);

    const [customBrushes, setCustomBrushes] = useState<CspBrush[]>(() =>
      getCachedCustomBrushes()
    );
    const [isDraggingWidth, setIsDraggingWidth] = useState(false);
    const dragStartXRef = useRef(0);
    const dragStartWidthRef = useRef(width);

    const syncCustomBrushes = useCallback(() => {
      loadCustomBrushes().then((brushes) => {
        setCustomBrushes(brushes);
        for (const b of brushes) {
          if (b.tipDataUrl) preloadTipImage(b.tipDataUrl).catch(() => {});
          if (b.secondaryTipDataUrl) preloadTipImage(b.secondaryTipDataUrl).catch(() => {});
          if (b.subTips) {
            for (const st of b.subTips) {
              if (st.dataUrl) preloadTipImage(st.dataUrl).catch(() => {});
            }
          }
          if (b.paperTexture) preloadTipImage(b.paperTexture).catch(() => {});
        }
      });
    }, []);

    useEffect(() => {
      syncCustomBrushes();
      const handleChanged = () => {
        setCustomBrushes([...getCachedCustomBrushes()]);
      };
      window.addEventListener(CUSTOM_BRUSHES_CHANGED_EVENT, handleChanged);
      return () => window.removeEventListener(CUSTOM_BRUSHES_CHANGED_EVENT, handleChanged);
    }, [syncCustomBrushes]);

    useEffect(() => {
      if (!isDraggingWidth) return;
      const handlePointerMove = (e: PointerEvent) => {
        const delta = e.clientX - dragStartXRef.current;
        const newWidth = Math.max(
          128,
          Math.min(300, Math.round(dragStartWidthRef.current + delta))
        );
        uiStoreActions.setLeftPanelWidth(newWidth);
      };
      const handlePointerUp = () => setIsDraggingWidth(false);
      window.addEventListener('pointermove', handlePointerMove);
      window.addEventListener('pointerup', handlePointerUp);
      return () => {
        window.removeEventListener('pointermove', handlePointerMove);
        window.removeEventListener('pointerup', handlePointerUp);
      };
    }, [isDraggingWidth]);

    const isBrushOrEraser = isBrushLikeTool(currentTool);

    return (
      <div className="flex h-full select-none z-10 shrink-0 gap-px bg-ui-void">
        <ToolStrip
          onTransformSelection={onTransformSelection}
          onToggleTransparentColor={onToggleTransparentColor}
        />

        {!isHidden && (
          <aside
            style={{ width }}
            className="relative bg-ui-panel border-r border-ui-subtle flex flex-col text-ui-primary text-xs shrink-0 overflow-hidden"
          >
            {isBrushOrEraser && (
              <SubToolList
                customBrushes={customBrushes}
                setCustomBrushes={setCustomBrushes}
              />
            )}

            <ToolPropertyInspector
              width={width}
              customBrushes={customBrushes}
              setCustomBrushes={setCustomBrushes}
              onDeselect={onDeselect}
              onTransformSelection={onTransformSelection}
              onClearSelection={onClearSelection}
              onDeleteOutsideSelection={onDeleteOutsideSelection}
              onCutSelection={onCutSelection}
              onCopySelection={onCopySelection}
              onPasteSelection={onPasteSelection}
              onMoveSelectionToNewLayer={onMoveSelectionToNewLayer}
              onExpandSelection={onExpandSelection}
              onContractSelection={onContractSelection}
              onFeatherSelection={onFeatherSelection}
              onInvertSelection={onInvertSelection}
            />

            {/* Subtle Drag Handle on Right Edge */}
            <div
              onMouseDown={(e) => {
                e.preventDefault();
                setIsDraggingWidth(true);
                dragStartXRef.current = e.clientX;
                dragStartWidthRef.current = width;
              }}
              title={t('Resize brush panel')}
              className={`absolute right-0 top-0 bottom-0 w-1 cursor-col-resize transition-colors z-30 ${
                isDraggingWidth
                  ? 'bg-[var(--ui-accent-border)]'
                  : 'hover:bg-[var(--ui-accent-border)]'
              }`}
            />
          </aside>
        )}
      </div>
    );
  }
);

LeftPanel.displayName = 'LeftPanel';