From 9658310689b3ba4ef42cb849f1763949dd36884a Mon Sep 17 00:00:00 2001 From: PandaDEV <70103896+0PandaDEV@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:53:47 +1000 Subject: [PATCH] refactor: improve keybind saving logic and error handling --- pages/settings.vue | 104 +++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/pages/settings.vue b/pages/settings.vue index dbf51c4..cf4771e 100644 --- a/pages/settings.vue +++ b/pages/settings.vue @@ -10,7 +10,10 @@

Qopy

-
+

Save

@@ -23,7 +26,9 @@
-
+

Record a new Hotkey

>(new Set()); const isKeybindInputFocused = ref(false); @@ -61,6 +67,7 @@ const lastBlurTime = ref(0); const os = ref(""); const router = useRouter(); const keyboard = useKeyboard(); +const showEmptyKeybindError = ref(false); const keyToDisplayMap: Record = { " ": "Space", @@ -115,16 +122,17 @@ const updateKeybind = () => { const onBlur = () => { isKeybindInputFocused.value = false; lastBlurTime.value = Date.now(); + showEmptyKeybindError.value = false; }; const onFocus = () => { isKeybindInputFocused.value = true; activeModifiers.clear(); keybind.value = []; + showEmptyKeybindError.value = false; }; const onKeyDown = (event: KeyboardEvent) => { - event.preventDefault(); const key = event.code; if (key === "Escape") { @@ -142,57 +150,63 @@ const onKeyDown = (event: KeyboardEvent) => { } updateKeybind(); + showEmptyKeybindError.value = false; }; const saveKeybind = async () => { - console.log("New:", keybind.value); - const oldKeybind = await invoke("get_keybind"); - console.log("Old:", oldKeybind); - await invoke("save_keybind", { keybind: keybind.value }); + if (keybind.value.length > 0) { + console.log("Saving keybind", keybind.value); + await invoke("save_keybind", { keybind: keybind }); + } else { + showEmptyKeybindError.value = true; + } }; +os.value = platform(); + onMounted(() => { - os.value = platform(); - - keyboard.down("MetaLeft+Enter", (event) => { - if (os.value === "macos" && !isKeybindInputFocused.value) { - console.log("Save on macOS") - event.preventDefault() - saveKeybind() - } - }) - - keyboard.down("MetaRight+Enter", (event) => { - if (os.value === "macos" && !isKeybindInputFocused.value) { - console.log("Save on macOS") - event.preventDefault() - saveKeybind() - } - }) - - keyboard.down("ControlLeft+Enter", (event) => { - if (os.value !== "macos" && !isKeybindInputFocused.value) { - console.log("Save on other OS") - event.preventDefault() - saveKeybind() - } - }) - - keyboard.down("ControlRight+Enter", (event) => { - if (os.value !== "macos" && !isKeybindInputFocused.value) { - console.log("Save on other OS") - event.preventDefault() - saveKeybind() - } - }) - - keyboard.down("Escape", (event) => { - console.log("Escape"); - if (!isKeybindInputFocused.value) { - event.preventDefault(); + keyboard.down([Key.Escape], (event) => { + console.log("Escape key pressed"); + if (isKeybindInputFocused.value) { + keybindInput.value?.blur(); + } else { router.push("/"); } }); + + switch (os.value) { + case "macos": + keyboard.down([Key.LeftMeta, Key.Enter], (event) => { + if (!isKeybindInputFocused.value) { + saveKeybind(); + } + }); + + keyboard.down([Key.RightMeta, Key.Enter], (event) => { + if (!isKeybindInputFocused.value) { + saveKeybind(); + } + }); + break; + + case "linux" || "windows": + keyboard.down([Key.LeftControl, Key.Enter], (event) => { + if (!isKeybindInputFocused.value) { + saveKeybind(); + } + }); + + keyboard.down([Key.RightControl, Key.Enter], (event) => { + if (!isKeybindInputFocused.value) { + saveKeybind(); + } + }); + break; + } +}); + +onUnmounted(() => { + keyboard.unregisterAll(); });