diff --git a/App.tsx b/App.tsx index 731a4005c..0095872e2 100755 --- a/App.tsx +++ b/App.tsx @@ -153,6 +153,7 @@ function App({ settings }: { settings: SettingsState }) { terminalSettings, hotkeyScheme, keyBindings, + isHotkeyRecording, } = settings; const { @@ -427,7 +428,7 @@ function App({ settings }: { settings: SettingsState }) { // Global hotkey handler useEffect(() => { - if (hotkeyScheme === 'disabled') return; + if (hotkeyScheme === 'disabled' || isHotkeyRecording) return; const isMac = hotkeyScheme === 'mac'; if (IS_DEV) console.log('[Hotkeys] Registering global hotkey handler, scheme:', hotkeyScheme, 'bindings count:', keyBindings.length); @@ -442,6 +443,9 @@ function App({ settings }: { settings: SettingsState }) { return; } + const isTerminalElement = + target instanceof HTMLElement && !!target.closest?.(".xterm"); + // Check each key binding for (const binding of keyBindings) { const keyStr = isMac ? binding.mac : binding.pc; @@ -451,7 +455,10 @@ function App({ settings }: { settings: SettingsState }) { // Don't handle them at app level const terminalActions = ['copy', 'paste', 'selectAll', 'clearBuffer', 'searchTerminal']; if (terminalActions.includes(binding.action)) { - return; // Let terminal handle it + if (isTerminalElement) { + return; // Let terminal handle it + } + continue; // Ignore terminal actions outside terminal } e.preventDefault(); @@ -464,7 +471,7 @@ function App({ settings }: { settings: SettingsState }) { window.addEventListener('keydown', handleGlobalKeyDown, true); return () => window.removeEventListener('keydown', handleGlobalKeyDown, true); - }, [hotkeyScheme, keyBindings, executeHotkeyAction]); + }, [hotkeyScheme, keyBindings, isHotkeyRecording, executeHotkeyAction]); useEffect(() => { const onKeyDown = (e: KeyboardEvent) => { diff --git a/application/state/useSettingsState.ts b/application/state/useSettingsState.ts index abc616781..ca78a993a 100644 --- a/application/state/useSettingsState.ts +++ b/application/state/useSettingsState.ts @@ -10,6 +10,7 @@ STORAGE_KEY_TERM_FONT_SIZE, STORAGE_KEY_TERM_SETTINGS, STORAGE_KEY_HOTKEY_SCHEME, STORAGE_KEY_CUSTOM_KEY_BINDINGS, +STORAGE_KEY_HOTKEY_RECORDING, STORAGE_KEY_CUSTOM_CSS, STORAGE_KEY_UI_LANGUAGE, } from '../../infrastructure/config/storageKeys'; @@ -99,6 +100,7 @@ export const useSettingsState = () => { const [customKeyBindings, setCustomKeyBindings] = useState(() => localStorageAdapter.read(STORAGE_KEY_CUSTOM_KEY_BINDINGS) || {} ); + const [isHotkeyRecording, setIsHotkeyRecordingState] = useState(false); const [customCSS, setCustomCSS] = useState(() => localStorageAdapter.readString(STORAGE_KEY_CUSTOM_CSS) || '' ); @@ -160,6 +162,20 @@ export const useSettingsState = () => { if (key === STORAGE_KEY_HOTKEY_SCHEME && (value === 'disabled' || value === 'mac' || value === 'pc')) { setHotkeyScheme(value); } + if (key === STORAGE_KEY_CUSTOM_KEY_BINDINGS) { + if (typeof value === 'string') { + try { + setCustomKeyBindings(JSON.parse(value) as CustomKeyBindings); + } catch { + // ignore parse errors + } + } else if (value && typeof value === 'object') { + setCustomKeyBindings(value as CustomKeyBindings); + } + } + if (key === STORAGE_KEY_HOTKEY_RECORDING && typeof value === 'boolean') { + setIsHotkeyRecordingState(value); + } }); return () => { try { @@ -285,7 +301,13 @@ export const useSettingsState = () => { useEffect(() => { localStorageAdapter.write(STORAGE_KEY_CUSTOM_KEY_BINDINGS, customKeyBindings); - }, [customKeyBindings]); + notifySettingsChanged(STORAGE_KEY_CUSTOM_KEY_BINDINGS, customKeyBindings); + }, [customKeyBindings, notifySettingsChanged]); + + const setIsHotkeyRecording = useCallback((isRecording: boolean) => { + setIsHotkeyRecordingState(isRecording); + notifySettingsChanged(STORAGE_KEY_HOTKEY_RECORDING, isRecording); + }, [notifySettingsChanged]); // Apply and persist custom CSS useEffect(() => { @@ -397,6 +419,8 @@ export const useSettingsState = () => { updateKeyBinding, resetKeyBinding, resetAllKeyBindings, + isHotkeyRecording, + setIsHotkeyRecording, customCSS, setCustomCSS, }; diff --git a/components/SettingsPage.tsx b/components/SettingsPage.tsx index ceada88c3..3bd989d9b 100644 --- a/components/SettingsPage.tsx +++ b/components/SettingsPage.tsx @@ -17,11 +17,10 @@ import { Tabs, TabsList, TabsTrigger } from "./ui/tabs"; const isMac = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform); -export default function SettingsPage() { - const settings = useSettingsState(); +type SettingsState = ReturnType; - const Inner = () => { - const { t } = useI18n(); +const SettingsPageContent: React.FC<{ settings: SettingsState }> = ({ settings }) => { + const { t } = useI18n(); const { notifyRendererReady, closeSettingsWindow } = useWindowControls(); useEffect(() => { @@ -122,6 +121,7 @@ export default function SettingsPage() { updateKeyBinding={settings.updateKeyBinding} resetKeyBinding={settings.resetKeyBinding} resetAllKeyBindings={settings.resetAllKeyBindings} + setIsHotkeyRecording={settings.setIsHotkeyRecording} /> ); - }; +}; + +export default function SettingsPage() { + const settings = useSettingsState(); return ( - + ); } diff --git a/components/TerminalLayer.tsx b/components/TerminalLayer.tsx index e6bb27c2a..20d27b4f2 100644 --- a/components/TerminalLayer.tsx +++ b/components/TerminalLayer.tsx @@ -767,6 +767,9 @@ const terminalLayerAreEqual = (prev: TerminalLayerProps, next: TerminalLayerProp prev.terminalTheme === next.terminalTheme && prev.terminalSettings === next.terminalSettings && prev.fontSize === next.fontSize && + prev.hotkeyScheme === next.hotkeyScheme && + prev.keyBindings === next.keyBindings && + prev.onHotkeyAction === next.onHotkeyAction && prev.onUpdateHost === next.onUpdateHost && prev.onToggleWorkspaceViewMode === next.onToggleWorkspaceViewMode && prev.onSetWorkspaceFocusedSession === next.onSetWorkspaceFocusedSession && diff --git a/components/settings/tabs/SettingsShortcutsTab.tsx b/components/settings/tabs/SettingsShortcutsTab.tsx index 838f8c729..d5862786b 100644 --- a/components/settings/tabs/SettingsShortcutsTab.tsx +++ b/components/settings/tabs/SettingsShortcutsTab.tsx @@ -14,8 +14,17 @@ export default function SettingsShortcutsTab(props: { updateKeyBinding?: (bindingId: string, scheme: "mac" | "pc", newKey: string) => void; resetKeyBinding?: (bindingId: string, scheme?: "mac" | "pc") => void; resetAllKeyBindings: () => void; + setIsHotkeyRecording?: (isRecording: boolean) => void; }) { - const { hotkeyScheme, setHotkeyScheme, keyBindings, updateKeyBinding, resetKeyBinding, resetAllKeyBindings } = props; + const { + hotkeyScheme, + setHotkeyScheme, + keyBindings, + updateKeyBinding, + resetKeyBinding, + resetAllKeyBindings, + setIsHotkeyRecording, + } = props; const { t } = useI18n(); const [recordingBindingId, setRecordingBindingId] = useState(null); @@ -98,6 +107,14 @@ export default function SettingsShortcutsTab(props: { }; }, [recordingBindingId, recordingScheme, updateKeyBinding, cancelRecording, getSpecialSuffix]); + useEffect(() => { + const isRecording = Boolean(recordingBindingId && recordingScheme); + setIsHotkeyRecording?.(isRecording); + return () => { + setIsHotkeyRecording?.(false); + }; + }, [recordingBindingId, recordingScheme, setIsHotkeyRecording]); + const categories = useMemo(() => ["tabs", "terminal", "navigation", "app"] as const, []); return ( diff --git a/infrastructure/config/storageKeys.ts b/infrastructure/config/storageKeys.ts index 3167dbb75..9eeef2b47 100644 --- a/infrastructure/config/storageKeys.ts +++ b/infrastructure/config/storageKeys.ts @@ -13,6 +13,7 @@ export const STORAGE_KEY_TERM_FONT_SIZE = 'netcatty_term_font_size_v1'; export const STORAGE_KEY_TERM_SETTINGS = 'netcatty_term_settings_v1'; export const STORAGE_KEY_HOTKEY_SCHEME = 'netcatty_hotkey_scheme_v1'; export const STORAGE_KEY_CUSTOM_KEY_BINDINGS = 'netcatty_custom_key_bindings_v1'; +export const STORAGE_KEY_HOTKEY_RECORDING = 'netcatty_hotkey_recording_v1'; export const STORAGE_KEY_CUSTOM_CSS = 'netcatty_custom_css_v1'; export const STORAGE_KEY_UI_LANGUAGE = 'netcatty_ui_language_v1'; export const STORAGE_KEY_PORT_FORWARDING = 'netcatty_port_forwarding_v1';