File size: 10,142 Bytes
07bbbbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
/**
* HotkeyInputDialog Component
*
* A dialog component for capturing hotkey combinations.
* Supports both component-based and imperative Promise-based API.
* Listens for key presses and captures modifier keys + letter.
*
* Features:
* - Interactive hotkey recording
* - Visual feedback during recording
* - Clear button to reset hotkey
* - Promise-based API for easy async/await usage
*/
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 {
/** Whether the dialog is open */
isOpen?: boolean;
/** @deprecated Use isOpen instead */
open?: boolean;
initialLetter?: string;
initialModifier?: string;
onClose: () => void;
onConfirm: (letter: string, modifier: string) => void;
}
/**
* Options for imperative hotkey input dialog
*/
export interface HotkeyInputDialogOptions {
/** Initial hotkey letter */
initialLetter?: string;
/** Initial hotkey modifier (e.g., "Ctrl+Alt") */
initialModifier?: string;
}
/**
* Result from hotkey input dialog
*/
export interface HotkeyInputDialogResult {
/** The captured letter/key */
letter: string;
/** The captured modifier combination (e.g., "Ctrl+Alt") */
modifier: string;
}
/**
* Internal content component that manages its own state
* This prevents the parent dialog from re-rendering when state changes
*/
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) => {
// Only intercept when recording
if (!isRecording) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation(); // Stop all other listeners
// Ignore modifier keys alone
if (['Control', 'Alt', 'Shift', 'Meta', 'Command'].includes(e.key)) {
return;
}
// Capture the letter/key
const capturedLetter = e.key.toUpperCase();
// Capture modifiers
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);
};
// Use capture phase to intercept keys before EnhancedDialog's handler
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}`;
};
// Expose confirm handler to parent via ref or callback
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>
);
};
/**
* Main HotkeyInputDialog component
* Wraps content with I18nextProvider to support useTranslation() in HotkeyInputContent
*/
export const HotkeyInputDialog: React.FC<HotkeyInputDialogProps> = ({
isOpen,
open, // Deprecated, for backward compatibility
initialLetter = '',
initialModifier = '',
onClose,
onConfirm,
}) => {
// Support both isOpen (new) and open (deprecated) props
const dialogIsOpen = isOpen ?? open ?? false;
const handleConfirm = () => {
// Call the exposed confirm handler from content component
if ((window as any).__hotkeyDialogConfirm) {
(window as any).__hotkeyDialogConfirm();
}
onClose();
};
// Wrap with I18nextProvider because HotkeyInputContent uses useTranslation()
// This handles both declarative usage (from SettingsView) and imperative usage
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>
);
};
/**
* Internal Wrapper Component for Imperative API
*/
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}
/>
);
};
/**
* Imperative API for showing hotkey input dialog
*
* @param options - Dialog configuration options
* @returns Promise that resolves to HotkeyInputDialogResult with letter and modifier, or null if cancelled
*
* @example
* ```tsx
* const result = await hotkeyInputDialog({
* initialLetter: "F",
* initialModifier: "Ctrl+Alt"
* });
*
* if (result !== null) {
* // User confirmed with hotkey
* await setHotkey(result.letter, result.modifier);
* }
* ```
*/
export const hotkeyInputDialog = (options: HotkeyInputDialogOptions = {}): Promise<HotkeyInputDialogResult | null> => {
return new Promise((resolve) => {
// Try to mount to the portal root (inside React context) first, fallback to body
const portalRoot = document.getElementById('dialog-portal-root');
const mountPoint = portalRoot || document.body;
// Create a temporary container
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); // Wait for modal animation
};
const handleClose = () => {
resolve(null);
cleanup();
};
const handleConfirm = (letter: string, modifier: string) => {
resolve({ letter, modifier });
cleanup();
};
// Always wrap with I18nextProvider because HotkeyInputContent uses useTranslation()
// Render based on mount point for IonApp wrapper
if (portalRoot) {
// Mount inside React context - no need for IonApp wrapper but need I18nextProvider
root.render(
<I18nextProvider i18n={i18n}>
<HotkeyInputDialogWrapper
isOpen={true}
onClose={handleClose}
onConfirm={handleConfirm}
options={options}
/>
</I18nextProvider>
);
} else {
// Fallback: mount to body with full context
root.render(
<I18nextProvider i18n={i18n}>
<IonApp>
<HotkeyInputDialogWrapper
isOpen={true}
onClose={handleClose}
onConfirm={handleConfirm}
options={options}
/>
</IonApp>
</I18nextProvider>
);
}
});
};
|