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, { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { AlertTriangle, Keyboard, Trash2, X } from 'lucide-react';
import {
CommandDefinition,
formatShortcutFromEvent,
formatShortcutLabel,
getOsModifierDisplayName,
findShortcutConflicts,
} from '../utils/shortcutManager';
import {
acquireNativeOsHotkeyRecording,
subscribeNativeOsHotkeys,
} from '../utils/nativeOsHotkeys';
import { ShortcutOverrides } from '../types/settings';
import { isImeComposing, useI18n } from '../i18n';
export interface SubToolShortcutModalProps {
commandId: string;
targetLabel: string;
targetKind: 'subtool' | 'group';
currentShortcut: string;
shortcuts: ShortcutOverrides;
allCommands: readonly CommandDefinition[];
onAssign: (commandId: string, shortcut: string, clearConflictIds?: string[]) => void;
onClear: (commandId: string) => void;
onClose: () => void;
}
export const SubToolShortcutModal: React.FC<SubToolShortcutModalProps> = ({
commandId,
targetLabel,
targetKind,
currentShortcut,
shortcuts,
allCommands,
onAssign,
onClear,
onClose,
}) => {
const { t, tc, td } = useI18n();
const [osModifierPinned, setOsModifierPinned] = useState(false);
const displayTargetLabel = td(targetLabel);
const [pendingConflict, setPendingConflict] = useState<{
shortcut: string;
conflicts: CommandDefinition[];
} | null>(null);
const osKeyName = getOsModifierDisplayName();
useEffect(() => {
const releaseNative = acquireNativeOsHotkeyRecording();
const processCapture = (ev: {
key: string;
code?: string;
ctrlKey?: boolean;
metaKey?: boolean;
shiftKey?: boolean;
altKey?: boolean;
}) => {
if (
ev.key === 'Escape' &&
!ev.ctrlKey &&
!ev.metaKey &&
!ev.altKey &&
!ev.shiftKey
) {
onClose();
return;
}
const captured = formatShortcutFromEvent({
...ev,
metaKey: Boolean(ev.metaKey || osModifierPinned),
});
if (!captured) return;
const conflicts = findShortcutConflicts(commandId, captured, shortcuts, allCommands);
if (conflicts.length > 0) {
setPendingConflict({ shortcut: captured, conflicts });
return;
}
onAssign(commandId, captured);
onClose();
};
const handleKeyDown = (e: KeyboardEvent) => {
if (isImeComposing(e)) return;
e.preventDefault();
e.stopPropagation();
processCapture(e);
};
const unsubNative = subscribeNativeOsHotkeys((ev) => {
if (ev.phase !== 'keydown' || ev.repeat) return;
processCapture(ev);
});
window.addEventListener('keydown', handleKeyDown, true);
return () => {
window.removeEventListener('keydown', handleKeyDown, true);
unsubNative();
releaseNative();
};
}, [commandId, shortcuts, allCommands, osModifierPinned, onAssign, onClose]);
const modalContent = (
<div
role="dialog"
aria-modal="true"
aria-label={t('Set Shortcut for {target}', { target: displayTargetLabel })}
data-testid="subtool-shortcut-modal"
className="fixed inset-0 z-[1100] flex items-center justify-center bg-ui-overlay p-4 select-none"
onClick={onClose}
onPointerDown={(e) => e.stopPropagation()}
>
<div
onClick={(e) => e.stopPropagation()}
className="w-full max-w-[340px] rounded-ui-lg bg-ui-panel border border-ui-default shadow-ui-popover overflow-hidden text-xs text-ui-primary"
>
<div className="h-10 px-4 flex items-center justify-between bg-ui-header border-b border-ui-subtle">
<div className="flex items-center gap-1.5 min-w-0">
<Keyboard className="w-3.5 h-3.5 text-ui-accent shrink-0" />
<span className="font-medium text-ui-primary truncate">
{targetKind === 'group' ? t('Group Shortcut') : t('Sub Tool Shortcut')}
</span>
</div>
<button
type="button"
onClick={onClose}
aria-label={t('Close shortcut recorder')}
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"
>
<X className="w-3.5 h-3.5" />
</button>
</div>
<div className="p-4 space-y-3">
<div className="space-y-0.5">
<div className="text-[11px] text-ui-muted">
{targetKind === 'group' ? t('Sub Tool Group') : t('Sub Tool')}
</div>
<div className="text-xs font-medium text-ui-primary truncate">{displayTargetLabel}</div>
</div>
<div className="rounded-ui-md bg-ui-header border border-ui-accent-border px-3 py-3 text-center space-y-1.5">
<div className="text-[11px] font-medium text-ui-accent">
{t('Press any key combination...')}
</div>
<div className="text-[10px] text-ui-muted">
{t('Supports Ctrl, {osKey}, Shift, Alt · Esc to cancel', { osKey: osKeyName })}
</div>
{currentShortcut && (
<div className="pt-1 text-[10px] text-ui-secondary">
{t('Current:')}{' '}
<span className="font-ui-mono text-ui-primary bg-ui-control border border-ui-subtle px-1.5 py-0.5 rounded-ui-xs">
{formatShortcutLabel(currentShortcut)}
</span>
</div>
)}
</div>
<div className="flex items-center justify-between gap-2">
<button
type="button"
data-testid="subtool-shortcut-os-toggle"
onClick={() => setOsModifierPinned((v) => !v)}
aria-pressed={osModifierPinned}
className={`px-2 py-1 rounded-ui-sm text-[10px] font-ui-mono transition-colors cursor-pointer border ${
osModifierPinned
? 'bg-ui-accent-subtle text-ui-accent border-ui-accent-border font-semibold'
: 'bg-ui-input text-ui-secondary border-ui-default hover:text-ui-primary hover:bg-ui-control-hover'
}`}
title={t('Toggle {osKey} key modifier for the next key press', { osKey: osKeyName })}
>
{osModifierPinned
? t('+ {osKey} Key (Active)', { osKey: osKeyName })
: t('+ {osKey} Key', { osKey: osKeyName })}
</button>
{currentShortcut && (
<button
type="button"
data-testid="subtool-shortcut-clear-btn"
onClick={() => {
onClear(commandId);
onClose();
}}
className="flex items-center gap-1 px-2 py-1 rounded-ui-sm bg-ui-control border border-ui-subtle hover:bg-ui-danger/15 hover:border-ui-danger/40 text-ui-secondary hover:text-ui-danger text-[10px] cursor-pointer transition-colors"
>
<Trash2 className="w-3 h-3" />
<span>{t('Clear')}</span>
</button>
)}
</div>
{pendingConflict && (
<div className="p-2.5 rounded-ui-sm bg-ui-warning/15 border border-ui-warning/40 space-y-2 text-[11px]">
<div className="flex items-start gap-2 text-ui-primary">
<AlertTriangle className="w-3.5 h-3.5 shrink-0 text-ui-warning mt-0.5" />
<span>
{t('{shortcut} is already assigned to {targets}. Replace it?')
.split(/(\{shortcut\}|\{targets\})/)
.map((part, idx) => {
if (part === '{shortcut}') {
return (
<strong key={idx} className="font-ui-mono text-ui-warning">
{formatShortcutLabel(pendingConflict.shortcut)}
</strong>
);
}
if (part === '{targets}') {
return (
<strong key={idx}>
{pendingConflict.conflicts.map((c) => td(c.label)).join(', ')}
</strong>
);
}
return <React.Fragment key={idx}>{part}</React.Fragment>;
})}
</span>
</div>
<div className="flex items-center justify-end gap-1.5">
<button
type="button"
onClick={() => setPendingConflict(null)}
className="px-2 py-1 rounded-ui-xs bg-ui-control border border-ui-default text-ui-primary hover:bg-ui-control-hover cursor-pointer text-[10px] transition-colors"
>
{t('Cancel')}
</button>
<button
type="button"
data-testid="subtool-shortcut-replace-btn"
onClick={() => {
onAssign(
commandId,
pendingConflict.shortcut,
pendingConflict.conflicts.map((c) => c.id)
);
onClose();
}}
className="px-2.5 py-1 rounded-ui-xs bg-ui-warning/20 hover:bg-ui-warning/30 border border-ui-warning/50 text-ui-warning font-medium cursor-pointer text-[10px] transition-colors"
>
{tc('shortcut', 'Replace')}
</button>
</div>
</div>
)}
</div>
</div>
</div>
);
return typeof document !== 'undefined' ? createPortal(modalContent, document.body) : modalContent;
};