A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import React, { useCallback, useEffect, useState } from 'react';
import { FocusHudPoint, FocusHudWidgetId } from '../../types/workspace';
import { useWorkspaceStore, workspaceStoreActions } from '../../stores';
export const FOCUS_HUD_MARGIN = 12;
export function clampFocusHudPosition(
x: number,
y: number,
width: number,
height: number,
viewport?: { width: number; height: number }
): FocusHudPoint {
const vw = viewport?.width ?? (typeof window !== 'undefined' ? window.innerWidth : 1280);
const vh = viewport?.height ?? (typeof window !== 'undefined' ? window.innerHeight : 800);
const safeW = Math.max(40, width || 40);
const safeH = Math.max(28, height || 28);
const maxX = Math.max(FOCUS_HUD_MARGIN, vw - safeW - FOCUS_HUD_MARGIN);
const maxY = Math.max(FOCUS_HUD_MARGIN, vh - safeH - FOCUS_HUD_MARGIN);
return {
x: Math.max(FOCUS_HUD_MARGIN, Math.min(maxX, Math.round(x))),
y: Math.max(FOCUS_HUD_MARGIN, Math.min(maxY, Math.round(y))),
};
}
/**
* Tactile matte floating HUD surface style:
* - Background strictly driven by `--ui-bg-floating` (which tracks user's `floatingOpacity`)
* - Gentle `blur(8px)` backdrop filter ONLY when `floatingOpacity < 0.98`
* - Optional auto-fade to `0.22` opacity with non-blocking pointer pass-through during strokes
*/
export function getFocusHudSurfaceStyle(
floatingOpacity: number,
isAutoFaded: boolean,
extra?: React.CSSProperties
): React.CSSProperties {
const useBlur = floatingOpacity < 0.98;
return {
backgroundColor: 'var(--ui-bg-floating)',
backdropFilter: useBlur ? 'blur(8px)' : 'none',
WebkitBackdropFilter: useBlur ? 'blur(8px)' : 'none',
opacity: isAutoFaded ? 0.22 : 1,
pointerEvents: isAutoFaded ? 'none' : 'auto',
...extra,
};
}
/**
* Reusable pointer-drag + viewport-resize clamping hook for Focus Mode floating HUD widgets.
*/
export function useFocusHudDrag(
widgetId: FocusHudWidgetId,
containerRef: React.RefObject<HTMLDivElement | null>,
customPos: FocusHudPoint | undefined,
onDragStartExtra?: () => void
) {
const [isDragging, setIsDragging] = useState(false);
// Clamp saved custom position inside viewport on window resize
useEffect(() => {
if (!customPos) return;
const clampNow = () => {
const el = containerRef.current;
const curPos = useWorkspaceStore.getState().focusHud.hudPositions[widgetId];
if (!el || !curPos) return;
const rect = el.getBoundingClientRect();
const clamped = clampFocusHudPosition(curPos.x, curPos.y, rect.width, rect.height);
if (clamped.x !== curPos.x || clamped.y !== curPos.y) {
workspaceStoreActions.setFocusHudPosition(widgetId, clamped, false);
}
};
const raf =
typeof requestAnimationFrame === 'function' ? requestAnimationFrame(clampNow) : null;
window.addEventListener('resize', clampNow);
return () => {
if (raf !== null && typeof cancelAnimationFrame === 'function') {
cancelAnimationFrame(raf);
}
window.removeEventListener('resize', clampNow);
};
}, [containerRef, customPos, widgetId]);
const handleStartDrag = useCallback(
(e: React.PointerEvent<HTMLElement>) => {
if (e.button !== 0) return;
const target = e.target as HTMLElement | null;
if (target?.closest?.('button, input, select, [role="slider"]')) return;
const el = containerRef.current;
if (!el) return;
e.preventDefault();
e.stopPropagation();
const rect = el.getBoundingClientRect();
const startX = e.clientX;
const startY = e.clientY;
const origX = rect.left;
const origY = rect.top;
const w = rect.width;
const h = rect.height;
let moved = false;
const onMove = (ev: PointerEvent) => {
const dx = ev.clientX - startX;
const dy = ev.clientY - startY;
if (!moved) {
if (Math.hypot(dx, dy) < 3) return;
moved = true;
setIsDragging(true);
onDragStartExtra?.();
}
const nextPos = clampFocusHudPosition(origX + dx, origY + dy, w, h);
workspaceStoreActions.setFocusHudPosition(widgetId, nextPos, false);
};
const cleanup = () => {
window.removeEventListener('pointermove', onMove);
window.removeEventListener('pointerup', onUp);
window.removeEventListener('pointercancel', onCancel);
setIsDragging(false);
};
const onUp = (ev: PointerEvent) => {
const wasMoved = moved;
cleanup();
if (wasMoved) {
const finalPos = clampFocusHudPosition(
origX + (ev.clientX - startX),
origY + (ev.clientY - startY),
w,
h
);
workspaceStoreActions.setFocusHudPosition(widgetId, finalPos, true);
}
};
const onCancel = () => {
cleanup();
};
window.addEventListener('pointermove', onMove);
window.addEventListener('pointerup', onUp);
window.addEventListener('pointercancel', onCancel);
},
[containerRef, onDragStartExtra, widgetId]
);
const handleResetWidgetPosition = useCallback(() => {
workspaceStoreActions.setFocusHudPosition(widgetId, null, true);
}, [widgetId]);
return {
isDragging,
handleStartDrag,
handleResetWidgetPosition,
};
}
/** Tactile 6-dot micro grip icon for draggable Focus HUD handles. */
export const FocusGripDots: React.FC<{ orientation?: 'horizontal' | 'vertical' }> = React.memo(
({ orientation = 'horizontal' }) => {
const dots =
orientation === 'horizontal'
? [
[2, 2],
[6, 2],
[10, 2],
[2, 6],
[6, 6],
[10, 6],
]
: [
[2, 2],
[6, 2],
[2, 6],
[6, 6],
[2, 10],
[6, 10],
];
const vb = orientation === 'horizontal' ? '0 0 12 8' : '0 0 8 12';
const w = orientation === 'horizontal' ? 12 : 8;
const h = orientation === 'horizontal' ? 8 : 12;
return (
<svg
width={w}
height={h}
viewBox={vb}
aria-hidden="true"
className="text-ui-icon-muted opacity-65 group-hover:opacity-100 transition-opacity shrink-0"
>
{dots.map(([cx, cy], idx) => (
<circle key={idx} cx={cx} cy={cy} r="1" fill="currentColor" />
))}
</svg>
);
}
);
FocusGripDots.displayName = 'FocusGripDots';