From 3e23509e681452b0043d70bb586b3e7a6f8b336e Mon Sep 17 00:00:00 2001 From: Sakamoto Shiina <68018796+ShiinaSakamoto@users.noreply.github.com> Date: Fri, 17 Jan 2025 05:18:09 +0900 Subject: [PATCH] [Update] Prevent to register the same hotkey that is already in use. --- .../hotkeys_entry/HotkeysEntry.jsx | 3 +- src-ui/logics/configs/hotkeys/useHotkeys.js | 36 +++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src-ui/app/config_page/setting_section/setting_box/_components/hotkeys_entry/HotkeysEntry.jsx b/src-ui/app/config_page/setting_section/setting_box/_components/hotkeys_entry/HotkeysEntry.jsx index 1e71a069..750095e0 100644 --- a/src-ui/app/config_page/setting_section/setting_box/_components/hotkeys_entry/HotkeysEntry.jsx +++ b/src-ui/app/config_page/setting_section/setting_box/_components/hotkeys_entry/HotkeysEntry.jsx @@ -20,7 +20,8 @@ export const HotkeysEntry = (props) => { const updateHotkeys = (keys) => { entryRef.current.blur(); - props.setHotkeys({ [props.hotkey_id]: keys }); + const result = props.setHotkeys({ [props.hotkey_id]: keys }); + if (result === false) setDisplayValue(""); }; const processKey = (key) => { diff --git a/src-ui/logics/configs/hotkeys/useHotkeys.js b/src-ui/logics/configs/hotkeys/useHotkeys.js index 358dd183..a80cfb78 100644 --- a/src-ui/logics/configs/hotkeys/useHotkeys.js +++ b/src-ui/logics/configs/hotkeys/useHotkeys.js @@ -2,6 +2,7 @@ import { appWindow } from "@tauri-apps/api/window"; import { store, useStore_Hotkeys } from "@store"; import { useStdoutToPython } from "@logics/useStdoutToPython"; +import { useNotificationStatus } from "@logics_common"; import { useMainFunction } from "@logics_main"; import { register, unregisterAll, isRegistered } from "@tauri-apps/api/globalShortcut"; @@ -19,14 +20,37 @@ export const useHotkeys = () => { pendingHotkeys(); asyncStdoutToPython("/get/data/hotkeys"); }; + const { showNotification_Success, showNotification_Error, closeNotification } = useNotificationStatus(); const setHotkeys = (hotkeys) => { pendingHotkeys(); - const send_obj = { - ...currentHotkeys.data, - ...hotkeys, - }; - asyncStdoutToPython("/set/data/hotkeys", send_obj); + + const updatedHotkeys = { ...currentHotkeys.data, ...hotkeys }; + const usedShortcuts = new Set(); + const conflictingKeys = []; + + for (const [actionKey, hotkey] of Object.entries(updatedHotkeys)) { + if (!hotkey) continue; + + const shortcut = parseHotkey(hotkey); + if (usedShortcuts.has(shortcut)) { + showNotification_Error(`The hotkey ${shortcut} is already in use.`); + updatedHotkeys[actionKey] = null; + conflictingKeys.push(actionKey); + } else { + usedShortcuts.add(shortcut); + } + } + + updateHotkeys(updatedHotkeys); + + if (conflictingKeys.length === 0) { + asyncStdoutToPython("/set/data/hotkeys", updatedHotkeys); + closeNotification(); + return true; + } else { + return false; + } }; const registerShortcuts = async () => { @@ -73,7 +97,7 @@ export const useHotkeys = () => { } } }); - // console.log(`Registered global shortcut: ${shortcut} for action: ${actionKey}`); + } } } catch (error) {