From f4cfe72b4b406187e67ef331651278943bf298c8 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sun, 29 Sep 2024 08:53:37 +0200 Subject: [PATCH] allow auto reassign duplicate keybinding closes #2154 --- .../src/components/KeyboardShortcuts.tsx | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/renderer/src/components/KeyboardShortcuts.tsx b/src/renderer/src/components/KeyboardShortcuts.tsx index 4ec2732f..8d83e944 100644 --- a/src/renderer/src/components/KeyboardShortcuts.tsx +++ b/src/renderer/src/components/KeyboardShortcuts.tsx @@ -707,25 +707,37 @@ const KeyboardShortcuts = memo(({ const stringifyKeys = (keys: string[]) => keys.join('+'); - const onNewKeyBindingConfirmed = useCallback((action: KeyboardAction, keys: string[]) => { + const onNewKeyBindingConfirmed = useCallback(async (action: KeyboardAction, keys: string[]) => { const fixedKeys = fixKeys(keys); if (fixedKeys.length === 0) return; const keysStr = stringifyKeys(fixedKeys); console.log('new key binding', action, keysStr); - setKeyBindings((existingBindings) => { - const duplicate = existingBindings.find((existingBinding) => existingBinding.keys === keysStr); - if (duplicate) { - Swal.fire({ icon: 'error', title: t('Duplicate keyboard combination'), text: t('Combination is already bound to "{{alreadyBoundKey}}"', { alreadyBoundKey: actionsMap[duplicate.action]?.name }) }); - console.log('trying to add duplicate'); - return existingBindings; + const duplicate = keyBindings.find((existingBinding) => existingBinding.keys === keysStr); + let shouldReplaceDuplicate: KeyBinding | undefined; + if (duplicate) { + const { isConfirmed } = await Swal.fire({ + icon: 'warning', + title: t('Duplicate keyboard combination'), + text: t('Combination is already bound to "{{alreadyBoundKey}}". Do you want to replace the existing binding?', { alreadyBoundKey: actionsMap[duplicate.action]?.name }), + confirmButtonText: t('Replace'), + focusCancel: true, + showCancelButton: true, + }); + if (isConfirmed) { + shouldReplaceDuplicate = duplicate; + } else { + return; } + } - console.log('saving key binding'); + setKeyBindings((existingBindings) => { + console.log('Saving key binding'); setCreatingBinding(undefined); - return [...existingBindings, { action, keys: keysStr }]; + const filtered = !shouldReplaceDuplicate ? existingBindings : existingBindings.filter((existing) => existing.keys !== shouldReplaceDuplicate.keys); + return [...filtered, { action, keys: keysStr }]; }); - }, [actionsMap, setKeyBindings, t]); + }, [actionsMap, keyBindings, setKeyBindings, t]); return ( <>