Skip to content

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

16.4 KB
Raw Blame
import React, { useMemo } from 'react';
import {
  Minus,
  Plus,
  Users,
  Palette,
  Settings,
  RotateCcw,
  FlipHorizontal,
  FlipVertical,
  Contrast,
  Maximize2,
  Minimize2,
} from 'lucide-react';
import {
  CanvasDimensions,
  MenuSection,
  CanvasColorMode,
  SoftProofSettings,
  DocumentTab,
} from '../types/editor';
import { ThemePreset } from '../types/settings';
import { formatZoomPercent } from '../hooks/useCanvasNavigation';
import { formatRotationDegrees } from '../utils/canvasUtils';
import { MenuBar } from './MenuBar';
import { WindowControls } from './WindowControls';
import { CommandBar, WorkspaceCommandHandlers } from './CommandBar';
import { useI18n } from '../i18n';

export interface HeaderProps {
  canUndo: boolean;
  canRedo: boolean;
  onUndo: () => void;
  onRedo: () => void;
  zoom: number;
  onZoomIn: () => void;
  onZoomOut: () => void;
  onResetZoom: () => void;
  rotation: number;
  onResetRotation: () => void;
  flipX?: boolean;
  flipY?: boolean;
  onToggleFlipHorizontal?: () => void;
  onToggleFlipVertical?: () => void;
  onResetViewFlip?: () => void;
  valueCheckEnabled?: boolean;
  onToggleValueCheck?: () => void;
  dimensions?: CanvasDimensions;
  colorMode?: CanvasColorMode;
  menuSections?: MenuSection[];
  hasOpenDocument?: boolean;
  documents?: DocumentTab[];
  activeDocumentId?: string | null;
  onSelectDocument?: (id: string) => void;
  onCloseDocument?: (id: string) => void;
  onNewDocument?: () => void;
  onRenameDocument?: (id: string, newName: string) => void;
  commandHandlers?: WorkspaceCommandHandlers;
  isCollabConnected?: boolean;
  isCollabReconnecting?: boolean;
  collabPeerCount?: number;
  onOpenCollabModal?: () => void;
  isAppearanceStudioOpen?: boolean;
  onToggleAppearanceStudio?: () => void;
  isFocusMode?: boolean;
  onToggleFocusMode?: () => void;
  onOpenSettings?: () => void;
  themePreset?: ThemePreset;
  softProofSettings?: SoftProofSettings;
  appView?: 'home' | 'workspace';
  onSelectAppView?: (view: 'home' | 'workspace') => void;
}

export const Header: React.FC<HeaderProps> = React.memo(({
  canUndo,
  canRedo,
  onUndo,
  onRedo,
  zoom,
  onZoomIn,
  onZoomOut,
  onResetZoom,
  rotation,
  onResetRotation,
  flipX = false,
  flipY = false,
  onToggleFlipHorizontal,
  onToggleFlipVertical,
  onResetViewFlip,
  valueCheckEnabled = false,
  onToggleValueCheck,
  colorMode = 'color',
  menuSections,
  hasOpenDocument = true,
  commandHandlers,
  isCollabConnected = false,
  isCollabReconnecting = false,
  collabPeerCount = 1,
  onOpenCollabModal,
  isAppearanceStudioOpen = false,
  onToggleAppearanceStudio,
  isFocusMode = false,
  onToggleFocusMode,
  onOpenSettings,
  themePreset = 'modern',
  softProofSettings,
  appView = 'workspace',
  onSelectAppView,
}) => {
  const { t } = useI18n();
  const isRotated = Math.abs(rotation % 360) > 0.05;

  const resolvedHandlers = useMemo<WorkspaceCommandHandlers>(
    () =>
      commandHandlers ?? {
        canUndo,
        canRedo,
        onUndo,
        onRedo,
        onFlipHorizontal: onToggleFlipHorizontal,
        onFlipVertical: onToggleFlipVertical,
        flipX,
        flipY,
        onToggleValueCheck,
        valueCheckEnabled,
        onResetZoom,
        onResetRotation,
        hasOpenDocument,
      },
    [
      commandHandlers,
      canUndo,
      canRedo,
      onUndo,
      onRedo,
      onToggleFlipHorizontal,
      onToggleFlipVertical,
      flipX,
      flipY,
      onToggleValueCheck,
      valueCheckEnabled,
      onResetZoom,
      onResetRotation,
      hasOpenDocument,
    ]
  );

  return (
    <header
      data-tauri-drag-region
      data-chrome-bar="header"
      data-theme-preset={themePreset}
      className="h-[36px] px-2 flex items-center justify-between gap-2 select-none z-40 shrink-0 bg-ui-header text-ui-primary border-b border-ui-subtle"
    >
      {/* Left: Brand Mark + Compact Home | Workspace Switcher + Menu Bar */}
      <div
        className="flex items-center gap-1.5 shrink-0"
        style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}
      >
        <div className="w-5 h-5 flex items-center justify-center shrink-0">
          <img
            src="/app-icon.png"
            alt="Ink Studio"
            className="w-3.5 h-3.5 object-contain pointer-events-none opacity-90"
          />
        </div>

        <div
          role="tablist"
          aria-label={t('App View')}
          data-testid="header-view-switcher"
          className="h-6 p-0.5 rounded-ui-sm bg-ui-input border border-ui-subtle flex items-center gap-0.5 shrink-0"
        >
          <button
            type="button"
            role="tab"
            aria-selected={appView === 'home'}
            data-testid="header-view-home"
            onClick={() => onSelectAppView?.('home')}
            title={t('Home & Recent Documents')}
            className={`h-full px-2 rounded-ui-xs text-[10.5px] leading-none flex items-center transition-colors cursor-pointer ${
              appView === 'home'
                ? 'bg-ui-control-active text-ui-primary font-medium'
                : 'text-ui-muted hover:text-ui-primary'
            }`}
          >
            {t('Home')}
          </button>
          <button
            type="button"
            role="tab"
            aria-selected={appView === 'workspace'}
            data-testid="header-view-workspace"
            onClick={() => onSelectAppView?.('workspace')}
            title={t('Drawing Workspace')}
            className={`h-full px-2 rounded-ui-xs text-[10.5px] leading-none flex items-center transition-colors cursor-pointer ${
              appView === 'workspace'
                ? 'bg-ui-control-active text-ui-primary font-medium'
                : 'text-ui-muted hover:text-ui-primary'
            }`}
          >
            {t('Workspace')}
          </button>
        </div>

        <MenuBar sections={menuSections} />
      </div>

      {/* Center: Customizable CSP Command Bar + Workspace Preset Selector */}
      <div className="flex-1 min-w-0 flex items-center justify-center px-1">
        {appView === 'workspace' && (
          <CommandBar handlers={resolvedHandlers} themePreset={themePreset} />
        )}
      </div>

      {/* Right: Mode Badges, Zoom Level, View Flip, Collab, Settings & Window Controls */}
      <div
        className="flex items-center gap-1.5 shrink-0"
        style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}
      >
        {hasOpenDocument && colorMode !== 'color' && (
          <span className="text-[10px] font-ui-mono uppercase px-1.5 py-0.5 rounded-ui-xs bg-ui-input border border-ui-subtle text-ui-muted">
            {colorMode === 'monochrome' ? t('1-bit') : t('Gray')}
          </span>
        )}

        {hasOpenDocument && softProofSettings?.enabled && (
          <span className="text-[10px] font-ui-mono uppercase px-1.5 py-0.5 rounded-ui-xs bg-ui-input border border-ui-accent-border text-ui-accent">
            CMYK
          </span>
        )}

        {hasOpenDocument && valueCheckEnabled && (
          <button
            type="button"
            onClick={onToggleValueCheck}
            data-testid="header-value-check-badge"
            title={t('Live 1-Click Value Check (B/W Tone Check) is active — display only, layer colors unchanged. Click or press Shift+Y to return to Color.')}
            className="h-6 px-1.5 rounded-ui-sm bg-ui-input hover:bg-ui-control-hover text-[10px] font-ui-mono uppercase text-ui-accent border border-ui-accent-border flex items-center gap-1 transition-colors cursor-pointer"
          >
            <Contrast className="w-3 h-3" />
            <span>{t('Value B/W')}</span>
          </button>
        )}

        {hasOpenDocument && (flipX || flipY) && (
          <button
            type="button"
            onClick={onResetViewFlip}
            data-testid="view-flip-badge"
            title={t('Non-destructive View Mirror active (display only — layer pixels unchanged). Click to reset view flip.')}
            className="h-6 px-1.5 rounded-ui-sm bg-ui-input hover:bg-ui-control-hover text-[10px] font-ui-mono uppercase text-ui-accent border border-ui-accent-border flex items-center gap-1 transition-colors cursor-pointer"
          >
            <span>
              {flipX && flipY ? t('View Flip H+V') : flipX ? t('View Flip H') : t('View Flip V')}
            </span>
          </button>
        )}

        {hasOpenDocument && isRotated && (
          <button
            type="button"
            onClick={onResetRotation}
            title={t('Reset Canvas Rotation (Ctrl+Alt+0)')}
            className="h-6 px-1.5 rounded-ui-sm bg-ui-input hover:bg-ui-control-hover border border-ui-subtle text-[11px] font-ui-mono tabular-nums text-ui-muted hover:text-ui-primary flex items-center gap-1 transition-colors cursor-pointer"
          >
            <RotateCcw className="w-3 h-3" />
            <span>{formatRotationDegrees(rotation)}</span>
          </button>
        )}

        {/* Compact Zoom Control */}
        <div
          className={`h-6 flex items-center rounded-ui-sm px-0.5 bg-ui-input border border-ui-subtle ${
            !hasOpenDocument ? 'opacity-40 pointer-events-none' : ''
          }`}
        >
          <button
            type="button"
            onClick={onZoomOut}
            title={t('Zoom Out (Ctrl+-)')}
            className="w-5 h-5 rounded-ui-xs flex items-center justify-center text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
          >
            <Minus className="w-3 h-3" />
          </button>
          <button
            type="button"
            onClick={() => onResetZoom()}
            title={t('Reset Zoom & Center (Ctrl+0)')}
            className="h-5 px-1.5 rounded-ui-xs font-ui-mono tabular-nums text-[11px] text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer min-w-[42px] text-center"
          >
            {formatZoomPercent(zoom)}
          </button>
          <button
            type="button"
            onClick={onZoomIn}
            title={t('Zoom In (Ctrl++)')}
            className="w-5 h-5 rounded-ui-xs flex items-center justify-center text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
          >
            <Plus className="w-3 h-3" />
          </button>
        </div>

        {/* Non-Destructive View Flip & 1-Click Value Check Controls */}
        {(onToggleFlipHorizontal || onToggleFlipVertical || onToggleValueCheck) && (
          <div
            className={`h-6 flex items-center rounded-ui-sm px-0.5 gap-0.5 bg-ui-input border border-ui-subtle ${
              !hasOpenDocument ? 'opacity-40 pointer-events-none' : ''
            }`}
          >
            {onToggleValueCheck && (
              <button
                type="button"
                data-testid="header-value-check-btn"
                onClick={onToggleValueCheck}
                aria-label={t('1-Click Value Check (Live B/W)')}
                aria-pressed={valueCheckEnabled}
                title={t('1-Click Value Check (Shift+Y) — Live B/W Tone Check (non-destructive)')}
                className={`w-5 h-5 rounded-ui-xs flex items-center justify-center transition-colors cursor-pointer ${
                  valueCheckEnabled
                    ? 'bg-ui-control-active text-ui-accent'
                    : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
                }`}
              >
                <Contrast className="w-3.5 h-3.5" />
              </button>
            )}
            {onToggleFlipHorizontal && (
              <button
                type="button"
                onClick={onToggleFlipHorizontal}
                aria-label={t('Flip View Horizontal')}
                aria-pressed={flipX}
                title={t('Flip View Horizontal (Shift+H) — non-destructive view mirror')}
                className={`w-5 h-5 rounded-ui-xs flex items-center justify-center transition-colors cursor-pointer ${
                  flipX
                    ? 'bg-ui-control-active text-ui-accent'
                    : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
                }`}
              >
                <FlipHorizontal className="w-3.5 h-3.5" />
              </button>
            )}
            {onToggleFlipVertical && (
              <button
                type="button"
                onClick={onToggleFlipVertical}
                aria-label={t('Flip View Vertical')}
                aria-pressed={flipY}
                title={t('Flip View Vertical (Shift+V) — non-destructive view mirror')}
                className={`w-5 h-5 rounded-ui-xs flex items-center justify-center transition-colors cursor-pointer ${
                  flipY
                    ? 'bg-ui-control-active text-ui-accent'
                    : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
                }`}
              >
                <FlipVertical className="w-3.5 h-3.5" />
              </button>
            )}
          </div>
        )}

        {/* Collab Session Button */}
        {onOpenCollabModal && (
          <button
            type="button"
            onClick={onOpenCollabModal}
            title={
              isCollabReconnecting
                ? t('Collab: Reconnecting...')
                : isCollabConnected
                  ? t('Collab ({count} online)', { count: collabPeerCount })
                  : t('Collaborative Session')
            }
            className={`h-6 px-1.5 rounded-ui-sm flex items-center gap-1 text-[11px] transition-colors cursor-pointer ${
              isCollabReconnecting
                ? 'bg-ui-warning/15 text-ui-warning border border-ui-warning/40 animate-pulse'
                : isCollabConnected
                  ? 'bg-ui-control-active text-ui-accent border border-ui-accent-border'
                  : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
            }`}
          >
            <Users className="w-3.5 h-3.5" />
            {isCollabReconnecting ? (
              <span className="font-ui-mono tabular-nums text-[10px]">...</span>
            ) : (
              isCollabConnected && <span className="font-ui-mono tabular-nums text-[10px]">{collabPeerCount}</span>
            )}
          </button>
        )}

        {/* Focus Mode (Tab) Quick Toggle */}
        {onToggleFocusMode && appView === 'workspace' && hasOpenDocument && (
          <button
            type="button"
            data-testid="header-focus-mode-btn"
            onClick={onToggleFocusMode}
            aria-label={isFocusMode ? t('Exit Focus Mode (Tab)') : t('Enter Focus Mode (Tab)')}
            aria-pressed={isFocusMode}
            title={isFocusMode ? t('Exit Focus Mode (Tab)') : t('Focus Mode — Hide Palettes (Tab)')}
            className={`w-6 h-6 rounded-ui-sm flex items-center justify-center transition-colors cursor-pointer ${
              isFocusMode
                ? 'bg-ui-control-active text-ui-accent border border-ui-accent-border'
                : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
            }`}
          >
            {isFocusMode ? (
              <Minimize2 className="w-3.5 h-3.5" />
            ) : (
              <Maximize2 className="w-3.5 h-3.5" />
            )}
          </button>
        )}

        {/* Live Appearance Studio Toggle */}
        {onToggleAppearanceStudio && (
          <button
            type="button"
            data-testid="header-appearance-btn"
            onClick={onToggleAppearanceStudio}
            aria-label={t('Live Appearance Studio')}
            aria-pressed={isAppearanceStudioOpen}
            title={t('Live Appearance Studio')}
            className={`w-6 h-6 rounded-ui-sm flex items-center justify-center transition-colors cursor-pointer ${
              isAppearanceStudioOpen
                ? 'bg-ui-control-active text-ui-accent border border-ui-accent-border'
                : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
            }`}
          >
            <Palette className="w-3.5 h-3.5" />
          </button>
        )}

        {/* Settings Button */}
        {onOpenSettings && (
          <button
            type="button"
            onClick={onOpenSettings}
            title={t('Preferences (Ctrl+,)')}
            className="w-6 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"
          >
            <Settings className="w-3.5 h-3.5" />
          </button>
        )}

        <WindowControls themePreset={themePreset} />
      </div>
    </header>
  );
});

Header.displayName = 'Header';