Skip to content

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

5.1 KB
Raw Blame
import React, { useState, useRef } from 'react';
import { Smartphone, X, GripHorizontal } from 'lucide-react';
import { useI18n } from '../i18n';

interface SmartphoneViewportSimulatorProps {
  visible: boolean;
  onToggleVisible: () => void;
  viewportWidth?: number;
  viewportHeight?: number;
}

export const SmartphoneViewportSimulator: React.FC<SmartphoneViewportSimulatorProps> = ({
  visible,
  onToggleVisible,
}) => {
  const { t } = useI18n();
  // Mobile phone mockup dimensions (standard 9:16 smartphone viewport)
  const phoneWidth = 360;
  const phoneHeight = 640;

  // Floating position (defaults to bottom-right or center)
  const [position, setPosition] = useState<{ x: number; y: number }>(() => ({
    x: Math.max(20, (window.innerWidth - phoneWidth) / 2),
    y: Math.max(20, (window.innerHeight - phoneHeight) / 2),
  }));

  const isDraggingRef = useRef(false);
  const dragStartRef = useRef<{ clientX: number; clientY: number; initX: number; initY: number }>({
    clientX: 0,
    clientY: 0,
    initX: 0,
    initY: 0,
  });

  if (!visible) return null;

  const handlePointerDown = (e: React.PointerEvent) => {
    e.stopPropagation();
    (e.target as HTMLElement).setPointerCapture(e.pointerId);
    isDraggingRef.current = true;
    dragStartRef.current = {
      clientX: e.clientX,
      clientY: e.clientY,
      initX: position.x,
      initY: position.y,
    };
  };

  const handlePointerMove = (e: React.PointerEvent) => {
    if (!isDraggingRef.current) return;
    const dx = e.clientX - dragStartRef.current.clientX;
    const dy = e.clientY - dragStartRef.current.clientY;
    setPosition({
      x: Math.round(dragStartRef.current.initX + dx),
      y: Math.round(dragStartRef.current.initY + dy),
    });
  };

  const handlePointerUp = (e: React.PointerEvent) => {
    if (isDraggingRef.current) {
      try {
        (e.target as HTMLElement).releasePointerCapture(e.pointerId);
      } catch {
        // ignore
      }
      isDraggingRef.current = false;
    }
  };

  return (
    <div
      className="fixed z-40 select-none pointer-events-none"
      style={{
        left: `${position.x}px`,
        top: `${position.y}px`,
        width: `${phoneWidth}px`,
        height: `${phoneHeight}px`,
      }}
    >
      {/* Smartphone Outer Chassis & Bezel */}
      <div className="relative w-full h-full rounded-[42px] border-[5px] border-ui-strong ring-1 ring-ui-default shadow-ui-popover flex flex-col overflow-hidden pointer-events-auto">
        {/* Dynamic Island / Camera Notch */}
        <div
          onPointerDown={handlePointerDown}
          onPointerMove={handlePointerMove}
          onPointerUp={handlePointerUp}
          className="absolute top-2.5 left-1/2 -translate-x-1/2 w-28 h-6 bg-ui-floating border border-ui-default rounded-full flex items-center justify-between px-3 cursor-move z-30 shadow-ui-floating group"
          title={t('Drag to reposition smartphone preview frame')}
        >
          <div className="w-2.5 h-2.5 rounded-full bg-ui-input border border-ui-default" />
          <GripHorizontal className="w-3.5 h-3.5 text-ui-icon-muted group-hover:text-ui-primary transition-colors" />
          <div className="w-2 h-2 rounded-full bg-ui-input border border-ui-subtle" />
        </div>

        {/* Top Status Bar Mockup */}
        <div className="w-full h-8 pt-1.5 px-6 flex items-center justify-between text-[11px] font-semibold font-ui-mono tabular-nums text-ui-primary z-20 pointer-events-none bg-ui-floating border-b border-ui-subtle">
          <span>9:41</span>
          <div className="flex items-center space-x-1.5 text-[10px]">
            <span>5G</span>
            <div className="w-5 h-2.5 rounded-ui-xs border border-ui-strong p-0.5 flex items-center">
              <div className="h-full w-full bg-ui-primary rounded-[1px]" />
            </div>
            <button
              type="button"
              onClick={onToggleVisible}
              className="text-ui-icon-muted hover:text-ui-primary transition-colors pointer-events-auto ml-1 p-0.5 cursor-pointer"
              title={t('Hide Viewport Simulator')}
            >
              <X className="w-3.5 h-3.5" />
            </button>
          </div>
        </div>

        {/* Translucent Cutout Viewport showing Canvas through */}
        <div className="flex-1 w-full relative pointer-events-none">
          {/* Subtle 9:16 guide overlay */}
          <div className="absolute inset-0 border border-ui-accent/50 rounded-ui-md pointer-events-none" />

          {/* Webtoon Reader Badge */}
          <div className="absolute bottom-6 left-1/2 -translate-x-1/2 px-3 py-1 bg-ui-floating rounded-full border border-ui-default text-[10px] text-ui-primary font-medium flex items-center space-x-1.5 shadow-ui-floating">
            <Smartphone className="w-3 h-3 text-ui-accent" />
            <span className="tabular-nums">{t('9:16 Webtoon Simulator')}</span>
          </div>
        </div>

        {/* Bottom Home Indicator Bar */}
        <div className="w-full h-5 flex items-center justify-center pb-1.5 z-20 pointer-events-none bg-ui-floating border-t border-ui-subtle">
          <div className="w-32 h-1 bg-ui-strong rounded-full" />
        </div>
      </div>
    </div>
  );
};