|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { useState, useEffect } from 'react'; |
|
|
import ReactDOM from 'react-dom/client'; |
|
|
import { |
|
|
IonApp, |
|
|
IonButton, |
|
|
IonChip, |
|
|
IonIcon, |
|
|
IonText, |
|
|
} from '@ionic/react'; |
|
|
import { keypadOutline } from 'ionicons/icons'; |
|
|
import { useTranslation, I18nextProvider } from 'react-i18next'; |
|
|
import i18n from '../i18n/i18n'; |
|
|
import { EnhancedDialog } from './EnhancedDialog/'; |
|
|
|
|
|
export interface HotkeyInputDialogProps { |
|
|
|
|
|
isOpen?: boolean; |
|
|
|
|
|
open?: boolean; |
|
|
initialLetter?: string; |
|
|
initialModifier?: string; |
|
|
onClose: () => void; |
|
|
onConfirm: (letter: string, modifier: string) => void; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface HotkeyInputDialogOptions { |
|
|
|
|
|
initialLetter?: string; |
|
|
|
|
|
initialModifier?: string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface HotkeyInputDialogResult { |
|
|
|
|
|
letter: string; |
|
|
|
|
|
modifier: string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const HotkeyInputContent: React.FC<{ |
|
|
initialLetter: string; |
|
|
initialModifier: string; |
|
|
onConfirm: (letter: string, modifier: string) => void; |
|
|
}> = ({ initialLetter, initialModifier, onConfirm }) => { |
|
|
const { t } = useTranslation(); |
|
|
const [letter, setLetter] = useState(initialLetter); |
|
|
const [modifier, setModifier] = useState(initialModifier); |
|
|
const [isRecording, setIsRecording] = useState(false); |
|
|
|
|
|
useEffect(() => { |
|
|
const handleKeyDown = (e: KeyboardEvent) => { |
|
|
|
|
|
if (!isRecording) return; |
|
|
|
|
|
e.preventDefault(); |
|
|
e.stopPropagation(); |
|
|
e.stopImmediatePropagation(); |
|
|
|
|
|
|
|
|
if (['Control', 'Alt', 'Shift', 'Meta', 'Command'].includes(e.key)) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const capturedLetter = e.key.toUpperCase(); |
|
|
|
|
|
|
|
|
const modifiers: string[] = []; |
|
|
if (e.ctrlKey) modifiers.push('Ctrl'); |
|
|
if (e.altKey) modifiers.push('Alt'); |
|
|
if (e.shiftKey) modifiers.push('Shift'); |
|
|
if (e.metaKey) modifiers.push('Cmd'); |
|
|
|
|
|
setLetter(capturedLetter); |
|
|
setModifier(modifiers.join('+')); |
|
|
setIsRecording(false); |
|
|
}; |
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown, true); |
|
|
return () => window.removeEventListener('keydown', handleKeyDown, true); |
|
|
}, [isRecording]); |
|
|
|
|
|
const handleStartRecording = (e?: React.MouseEvent) => { |
|
|
e?.preventDefault(); |
|
|
e?.stopPropagation(); |
|
|
setIsRecording(true); |
|
|
}; |
|
|
|
|
|
const handleClear = (e?: React.MouseEvent) => { |
|
|
e?.preventDefault(); |
|
|
e?.stopPropagation(); |
|
|
setLetter(''); |
|
|
setModifier(''); |
|
|
}; |
|
|
|
|
|
const getHotkeyDisplay = () => { |
|
|
if (!letter && !modifier) return t('Not set'); |
|
|
if (!modifier) return letter; |
|
|
return `${modifier}+${letter}`; |
|
|
}; |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
(window as any).__hotkeyDialogConfirm = () => onConfirm(letter, modifier); |
|
|
return () => { |
|
|
delete (window as any).__hotkeyDialogConfirm; |
|
|
}; |
|
|
}, [letter, modifier, onConfirm]); |
|
|
|
|
|
return ( |
|
|
<div style={{ width: '450px', maxWidth: '90vw' }}> |
|
|
<h2 style={{ marginTop: 0, marginBottom: '24px', textAlign: 'center' }}> |
|
|
{t('Set Hotkey')} |
|
|
</h2> |
|
|
|
|
|
<IonText color="medium"> |
|
|
<p style={{ marginBottom: '16px' }}> |
|
|
{t('Click the button below and press the key combination you want to set')} |
|
|
</p> |
|
|
</IonText> |
|
|
|
|
|
<div |
|
|
style={{ |
|
|
display: 'flex', |
|
|
flexDirection: 'column', |
|
|
alignItems: 'center', |
|
|
gap: '16px', |
|
|
padding: '24px', |
|
|
border: `2px dashed ${isRecording ? 'var(--ion-color-primary)' : 'var(--ion-color-medium)'}`, |
|
|
borderRadius: '8px', |
|
|
backgroundColor: isRecording ? 'var(--ion-color-light)' : 'var(--ion-background-color)', |
|
|
transition: 'all 0.3s', |
|
|
}} |
|
|
> |
|
|
<IonIcon |
|
|
icon={keypadOutline} |
|
|
style={{ |
|
|
fontSize: '48px', |
|
|
color: isRecording ? 'var(--ion-color-primary)' : 'var(--ion-color-medium)' |
|
|
}} |
|
|
/> |
|
|
|
|
|
{isRecording ? ( |
|
|
<IonText color="primary"> |
|
|
<h3 style={{ margin: 0 }}>{t('Please press a key...')}</h3> |
|
|
</IonText> |
|
|
) : ( |
|
|
<IonChip |
|
|
color={letter ? 'primary' : 'medium'} |
|
|
style={{ fontSize: '1.1rem', padding: '12px 16px', height: '48px' }} |
|
|
> |
|
|
{getHotkeyDisplay()} |
|
|
</IonChip> |
|
|
)} |
|
|
</div> |
|
|
|
|
|
<div style={{ display: 'flex', gap: '8px', marginTop: '16px' }}> |
|
|
<IonButton |
|
|
fill="outline" |
|
|
onClick={handleStartRecording} |
|
|
disabled={isRecording} |
|
|
expand="block" |
|
|
style={{ flex: 1 }} |
|
|
> |
|
|
{isRecording |
|
|
? t('Recording...') |
|
|
: t('Record Hotkey') |
|
|
} |
|
|
</IonButton> |
|
|
<IonButton |
|
|
fill="outline" |
|
|
onClick={handleClear} |
|
|
disabled={isRecording} |
|
|
> |
|
|
{t('Clear')} |
|
|
</IonButton> |
|
|
</div> |
|
|
</div> |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const HotkeyInputDialog: React.FC<HotkeyInputDialogProps> = ({ |
|
|
isOpen, |
|
|
open, // Deprecated, for backward compatibility |
|
|
initialLetter = '', |
|
|
initialModifier = '', |
|
|
onClose, |
|
|
onConfirm, |
|
|
}) => { |
|
|
|
|
|
const dialogIsOpen = isOpen ?? open ?? false; |
|
|
|
|
|
const handleConfirm = () => { |
|
|
|
|
|
if ((window as any).__hotkeyDialogConfirm) { |
|
|
(window as any).__hotkeyDialogConfirm(); |
|
|
} |
|
|
onClose(); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
<I18nextProvider i18n={i18n}> |
|
|
<EnhancedDialog |
|
|
isOpen={dialogIsOpen} |
|
|
onCancel={onClose} |
|
|
onConfirm={handleConfirm} |
|
|
className="hotkey-dialog settings-dialog" |
|
|
> |
|
|
<HotkeyInputContent |
|
|
initialLetter={initialLetter} |
|
|
initialModifier={initialModifier} |
|
|
onConfirm={onConfirm} |
|
|
/> |
|
|
</EnhancedDialog> |
|
|
</I18nextProvider> |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const HotkeyInputDialogWrapper: React.FC<{ |
|
|
isOpen: boolean; |
|
|
onClose: () => void; |
|
|
onConfirm: (letter: string, modifier: string) => void; |
|
|
options: HotkeyInputDialogOptions; |
|
|
}> = ({ isOpen, onClose, onConfirm, options }) => { |
|
|
return ( |
|
|
<HotkeyInputDialog |
|
|
isOpen={isOpen} |
|
|
initialLetter={options.initialLetter} |
|
|
initialModifier={options.initialModifier} |
|
|
onClose={onClose} |
|
|
onConfirm={onConfirm} |
|
|
/> |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const hotkeyInputDialog = (options: HotkeyInputDialogOptions = {}): Promise<HotkeyInputDialogResult | null> => { |
|
|
return new Promise((resolve) => { |
|
|
|
|
|
const portalRoot = document.getElementById('dialog-portal-root'); |
|
|
const mountPoint = portalRoot || document.body; |
|
|
|
|
|
|
|
|
const container = document.createElement('div'); |
|
|
container.id = 'hotkey-input-dialog-container-' + Date.now(); |
|
|
mountPoint.appendChild(container); |
|
|
|
|
|
const root = ReactDOM.createRoot(container); |
|
|
|
|
|
const cleanup = () => { |
|
|
setTimeout(() => { |
|
|
root.unmount(); |
|
|
if (container.parentNode) { |
|
|
mountPoint.removeChild(container); |
|
|
} |
|
|
}, 300); |
|
|
}; |
|
|
|
|
|
const handleClose = () => { |
|
|
resolve(null); |
|
|
cleanup(); |
|
|
}; |
|
|
|
|
|
const handleConfirm = (letter: string, modifier: string) => { |
|
|
resolve({ letter, modifier }); |
|
|
cleanup(); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if (portalRoot) { |
|
|
|
|
|
root.render( |
|
|
<I18nextProvider i18n={i18n}> |
|
|
<HotkeyInputDialogWrapper |
|
|
isOpen={true} |
|
|
onClose={handleClose} |
|
|
onConfirm={handleConfirm} |
|
|
options={options} |
|
|
/> |
|
|
</I18nextProvider> |
|
|
); |
|
|
} else { |
|
|
|
|
|
root.render( |
|
|
<I18nextProvider i18n={i18n}> |
|
|
<IonApp> |
|
|
<HotkeyInputDialogWrapper |
|
|
isOpen={true} |
|
|
onClose={handleClose} |
|
|
onConfirm={handleConfirm} |
|
|
options={options} |
|
|
/> |
|
|
</IonApp> |
|
|
</I18nextProvider> |
|
|
); |
|
|
} |
|
|
}); |
|
|
}; |
|
|
|