[Update] Prevent to register the same hotkey that is already in use.
This commit is contained in:
@@ -20,7 +20,8 @@ export const HotkeysEntry = (props) => {
|
|||||||
|
|
||||||
const updateHotkeys = (keys) => {
|
const updateHotkeys = (keys) => {
|
||||||
entryRef.current.blur();
|
entryRef.current.blur();
|
||||||
props.setHotkeys({ [props.hotkey_id]: keys });
|
const result = props.setHotkeys({ [props.hotkey_id]: keys });
|
||||||
|
if (result === false) setDisplayValue("");
|
||||||
};
|
};
|
||||||
|
|
||||||
const processKey = (key) => {
|
const processKey = (key) => {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { appWindow } from "@tauri-apps/api/window";
|
|||||||
|
|
||||||
import { store, useStore_Hotkeys } from "@store";
|
import { store, useStore_Hotkeys } from "@store";
|
||||||
import { useStdoutToPython } from "@logics/useStdoutToPython";
|
import { useStdoutToPython } from "@logics/useStdoutToPython";
|
||||||
|
import { useNotificationStatus } from "@logics_common";
|
||||||
import { useMainFunction } from "@logics_main";
|
import { useMainFunction } from "@logics_main";
|
||||||
import { register, unregisterAll, isRegistered } from "@tauri-apps/api/globalShortcut";
|
import { register, unregisterAll, isRegistered } from "@tauri-apps/api/globalShortcut";
|
||||||
|
|
||||||
@@ -19,14 +20,37 @@ export const useHotkeys = () => {
|
|||||||
pendingHotkeys();
|
pendingHotkeys();
|
||||||
asyncStdoutToPython("/get/data/hotkeys");
|
asyncStdoutToPython("/get/data/hotkeys");
|
||||||
};
|
};
|
||||||
|
const { showNotification_Success, showNotification_Error, closeNotification } = useNotificationStatus();
|
||||||
|
|
||||||
const setHotkeys = (hotkeys) => {
|
const setHotkeys = (hotkeys) => {
|
||||||
pendingHotkeys();
|
pendingHotkeys();
|
||||||
const send_obj = {
|
|
||||||
...currentHotkeys.data,
|
const updatedHotkeys = { ...currentHotkeys.data, ...hotkeys };
|
||||||
...hotkeys,
|
const usedShortcuts = new Set();
|
||||||
};
|
const conflictingKeys = [];
|
||||||
asyncStdoutToPython("/set/data/hotkeys", send_obj);
|
|
||||||
|
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 () => {
|
const registerShortcuts = async () => {
|
||||||
@@ -73,7 +97,7 @@ export const useHotkeys = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// console.log(`Registered global shortcut: ${shortcut} for action: ${actionKey}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user