Skip to content

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

10.4 KB
Raw Blame
import React from 'react';
import {
  Paintbrush,
  Eraser,
  PanelLeftClose,
  PanelLeftOpen,
  ArrowLeftRight,
  BoxSelect,
  Lasso,
  PaintBucket,
  Type,
  Frame,
  Scaling,
  Pipette,
  Wand2,
  PenTool,
  MessageSquare,
  Spline,
  MousePointer2,
  Shapes,
  Ruler,
  Blend,
  Droplets,
  Feather,
  Box,
  Sparkles,
} from 'lucide-react';
import { ToolType } from '../types/editor';
import { LiquifyIcon } from './LiquifyIcon';
import {
  useToolStore,
  toolStoreActions,
  useUiStore,
  uiStoreActions,
  workspaceStoreActions,
  useDocumentStore,
  documentStoreActions,
  selectHasOpenDocument,
} from '../stores';
import { useI18n } from '../i18n';

export interface ToolStripProps {
  onTransformSelection?: () => void;
  onToggleTransparentColor?: () => void;
  fluid?: boolean;
}

export const ToolStrip: React.FC<ToolStripProps> = React.memo(
  ({ onTransformSelection, onToggleTransparentColor, fluid = false }) => {
    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 isHidden = useUiStore((s) => s.isLeftPanelHidden);
    const hasOpenDocument = useDocumentStore(selectHasOpenDocument);
    const straightRulerEnabled = useDocumentStore((s) => Boolean(s.straightRuler?.enabled));

    const handleToggleTransparent = () => {
      if (onToggleTransparentColor) {
        onToggleTransparentColor();
      } else {
        const next = toolStoreActions.toggleTransparentColor();
        uiStoreActions.showToast(
          next ? t('Transparent ink mode active') : t('Color ink mode active')
        );
      }
    };

    const toolItems: Array<{
      id: ToolType;
      label: string;
      icon: React.ReactNode;
      dividerAfter?: boolean;
      onClick?: () => void;
    }> = [
      {
        id: 'brush',
        label: t('Brush (B)'),
        icon: <Paintbrush className="w-3.5 h-3.5" />,
        onClick: () => {
          toolStoreActions.selectTool('brush');
          if (isHidden) uiStoreActions.toggleLeftPanelHidden();
        },
      },
      {
        id: 'decoration',
        label: t('Decoration'),
        icon: <Sparkles className="w-3.5 h-3.5" />,
        onClick: () => {
          toolStoreActions.selectTool('decoration');
          if (isHidden) uiStoreActions.toggleLeftPanelHidden();
        },
      },
      { 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: 'liquify',
        label: t('Liquify (J)'),
        icon: <LiquifyIcon className="w-3.5 h-3.5" />,
      },
      {
        id: 'retouch',
        label: t('Retouch: Smudge / Blur / Dodge / Burn / Clone (S)'),
        icon: <Droplets className="w-3.5 h-3.5" />,
        dividerAfter: true,
      },
      {
        id: 'transform',
        label: t('Transform (Ctrl+T)'),
        icon: <Scaling className="w-3.5 h-3.5" />,
        onClick: () => {
          toolStoreActions.selectTool('transform');
          onTransformSelection?.();
        },
      },
      { id: 'select-rect', label: t('Marquee (M)'), icon: <BoxSelect 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-wand', label: t('Magic Wand (W)'), icon: <Wand2 className="w-3.5 h-3.5" /> },
      {
        id: 'select-pen',
        label: t('Selection Pen'),
        icon: <PenTool className="w-3.5 h-3.5" />,
        dividerAfter: true,
      },
      { id: 'fill', label: t('Fill (G)'), icon: <PaintBucket className="w-3.5 h-3.5" /> },
      { id: 'gradient', label: t('Gradient: Linear / Radial / Mesh (Shift+G)'), icon: <Blend 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: 'object', label: t('Object (O)'), icon: <MousePointer2 className="w-3.5 h-3.5" /> },
      { id: 'text', label: t('Text (T)'), icon: <Type className="w-3.5 h-3.5" /> },
      { id: 'frame', label: t('Frame Border (F)'), icon: <Frame className="w-3.5 h-3.5" /> },
      { id: 'balloon', label: t('Speech Balloon'), icon: <MessageSquare className="w-3.5 h-3.5" /> },
      { id: 'vector-edit', label: t('Correct Line'), icon: <Spline className="w-3.5 h-3.5" /> },
      {
        id: '3d',
        label: t('3D Object & Camera Tool (K)'),
        icon: <Box className="w-3.5 h-3.5" />,
        onClick: () => {
          toolStoreActions.selectTool('3d');
          if (isHidden) uiStoreActions.toggleLeftPanelHidden();
        },
      },
    ];

    return (
      <aside
        className={
          fluid
            ? 'w-full h-full bg-ui-panel flex flex-col items-center py-1.5 overflow-y-auto no-scrollbar'
            : 'w-[44px] bg-ui-panel border-r border-ui-subtle flex flex-col items-center py-1.5 z-20 shrink-0'
        }
      >
        <div
          className={
            fluid
              ? 'flex flex-wrap items-center justify-center gap-0.5 w-full px-1'
              : 'flex flex-col items-center gap-0.5 w-full px-1'
          }
        >
          {toolItems.map((item) => {
            const isActive = currentTool === item.id;
            return (
              <React.Fragment key={item.id}>
                <button
                  type="button"
                  data-testid={`toolstrip-${item.id}-btn`}
                  onClick={() =>
                    item.onClick ? item.onClick() : toolStoreActions.selectTool(item.id)
                  }
                  title={item.label}
                  className={`relative w-8 h-7 rounded-ui-sm flex items-center justify-center transition-colors 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 className="absolute left-0 top-1.5 bottom-1.5 w-[2px] rounded-full bg-ui-accent" />
                  )}
                  {item.icon}
                </button>
                {item.dividerAfter && <div className="w-4 border-t border-ui-subtle my-1" />}
              </React.Fragment>
            );
          })}
          <div className="w-4 border-t border-ui-subtle my-1" />
          <button
            type="button"
            disabled={!hasOpenDocument}
            onClick={() => {
              if (!hasOpenDocument) return;
              const next = documentStoreActions.toggleStraightRuler();
              uiStoreActions.showToast(next.enabled ? t('Straight Ruler: ON') : t('Straight Ruler: OFF'));
            }}
            title={t('Straight Ruler (R)')}
            data-testid="toolstrip-straight-ruler-btn"
            className={`relative w-8 h-7 rounded-ui-sm flex items-center justify-center transition-colors ${
              !hasOpenDocument
                ? 'text-ui-disabled opacity-40 cursor-not-allowed'
                : straightRulerEnabled
                ? 'bg-ui-control-active text-ui-accent cursor-pointer'
                : 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover cursor-pointer'
            }`}
          >
            {straightRulerEnabled && hasOpenDocument && (
              <span className="absolute left-0 top-1.5 bottom-1.5 w-[2px] rounded-full bg-ui-accent" />
            )}
            <Ruler className="w-3.5 h-3.5" />
          </button>
        </div>

        {/* Bottom Color Swatches & Collapse Toggle */}
        <div className="mt-auto flex flex-col items-center gap-1.5 pt-2 pb-1 w-full">
          <div className="relative w-6 h-6">
            <div
              onClick={() => toolStoreActions.swapColors()}
              title={t('Secondary: {color} (X to swap, D to reset)', { color: secondaryColor })}
              className="absolute bottom-0 right-0 w-4 h-4 rounded-ui-xs cursor-pointer ring-1 ring-ui-strong"
              style={{ backgroundColor: secondaryColor }}
            />
            <div
              onClick={() => {
                if (isTransparentColor) handleToggleTransparent();
                else toolStoreActions.swapColors();
              }}
              title={t('Primary: {color}', { color: primaryColor })}
              className={`absolute top-0 left-0 w-4 h-4 rounded-ui-xs cursor-pointer z-10 ${
                !isTransparentColor
                  ? 'ring-[1.5px] ring-ui-accent'
                  : 'opacity-70 ring-1 ring-ui-strong'
              }`}
              style={{ backgroundColor: primaryColor }}
            />
          </div>

          <div
            onClick={handleToggleTransparent}
            title={t('Transparent Ink (C)')}
            className={`w-6 h-2.5 rounded-ui-xs cursor-pointer transition-all ${
              isTransparentColor
                ? 'ring-[1.5px] ring-ui-accent'
                : 'ring-1 ring-ui-default opacity-75 hover:opacity-100'
            }`}
            style={{
              backgroundImage: 'repeating-conic-gradient(#777 0% 25%, #ccc 0% 50%)',
              backgroundSize: '4px 4px',
            }}
          />

          <button
            type="button"
            onClick={() => toolStoreActions.swapColors()}
            title={t('Swap Colors (X)')}
            className="w-7 h-6 rounded-ui-sm flex items-center justify-center text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
          >
            <ArrowLeftRight className="w-3 h-3" />
          </button>

          <button
            type="button"
            onClick={() =>
              fluid
                ? workspaceStoreActions.togglePalettesHidden()
                : uiStoreActions.toggleLeftPanelHidden()
            }
            title={fluid ? t('Hide/Show Palettes (Tab)') : isHidden ? t('Show Panel (Shift+Tab)') : t('Hide Panel (Shift+Tab)')}
            className="w-7 h-7 rounded-ui-sm flex items-center justify-center text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
          >
            {isHidden ? (
              <PanelLeftOpen className="w-3.5 h-3.5" />
            ) : (
              <PanelLeftClose className="w-3.5 h-3.5" />
            )}
          </button>
        </div>
      </aside>
    );
  }
);

ToolStrip.displayName = 'ToolStrip';