refactor: improve keybind saving logic and error handling

This commit is contained in:
PandaDEV 2025-01-02 14:53:47 +10:00
parent 6514abcdb3
commit 9658310689
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C

View file

@ -10,7 +10,10 @@
<p>Qopy</p> <p>Qopy</p>
</div> </div>
<div class="right"> <div class="right">
<div @click="saveKeybind" class="actions"> <div
@click="saveKeybind"
class="actions"
:class="{ disabled: keybind.length === 0 }">
<p>Save</p> <p>Save</p>
<div> <div>
<img alt="" src="../public/cmd.svg" v-if="os === 'macos'" /> <img alt="" src="../public/cmd.svg" v-if="os === 'macos'" />
@ -23,7 +26,9 @@
</div> </div>
</div> </div>
</div> </div>
<div class="keybind-container"> <div
class="keybind-container"
:class="{ 'empty-keybind': showEmptyKeybindError }">
<h2 class="title">Record a new Hotkey</h2> <h2 class="title">Record a new Hotkey</h2>
<div <div
@blur="onBlur" @blur="onBlur"
@ -52,6 +57,7 @@ import { invoke } from "@tauri-apps/api/core";
import { onMounted, onUnmounted, reactive, ref } from "vue"; import { onMounted, onUnmounted, reactive, ref } from "vue";
import { platform } from "@tauri-apps/plugin-os"; import { platform } from "@tauri-apps/plugin-os";
import { useRouter } from "vue-router"; import { useRouter } from "vue-router";
import { Key } from "wrdu-keyboard/key";
const activeModifiers = reactive<Set<string>>(new Set()); const activeModifiers = reactive<Set<string>>(new Set());
const isKeybindInputFocused = ref(false); const isKeybindInputFocused = ref(false);
@ -61,6 +67,7 @@ const lastBlurTime = ref(0);
const os = ref(""); const os = ref("");
const router = useRouter(); const router = useRouter();
const keyboard = useKeyboard(); const keyboard = useKeyboard();
const showEmptyKeybindError = ref(false);
const keyToDisplayMap: Record<string, string> = { const keyToDisplayMap: Record<string, string> = {
" ": "Space", " ": "Space",
@ -115,16 +122,17 @@ const updateKeybind = () => {
const onBlur = () => { const onBlur = () => {
isKeybindInputFocused.value = false; isKeybindInputFocused.value = false;
lastBlurTime.value = Date.now(); lastBlurTime.value = Date.now();
showEmptyKeybindError.value = false;
}; };
const onFocus = () => { const onFocus = () => {
isKeybindInputFocused.value = true; isKeybindInputFocused.value = true;
activeModifiers.clear(); activeModifiers.clear();
keybind.value = []; keybind.value = [];
showEmptyKeybindError.value = false;
}; };
const onKeyDown = (event: KeyboardEvent) => { const onKeyDown = (event: KeyboardEvent) => {
event.preventDefault();
const key = event.code; const key = event.code;
if (key === "Escape") { if (key === "Escape") {
@ -142,57 +150,63 @@ const onKeyDown = (event: KeyboardEvent) => {
} }
updateKeybind(); updateKeybind();
showEmptyKeybindError.value = false;
}; };
const saveKeybind = async () => { const saveKeybind = async () => {
console.log("New:", keybind.value); if (keybind.value.length > 0) {
const oldKeybind = await invoke<string[]>("get_keybind"); console.log("Saving keybind", keybind.value);
console.log("Old:", oldKeybind); await invoke("save_keybind", { keybind: keybind });
await invoke("save_keybind", { keybind: keybind.value }); } else {
showEmptyKeybindError.value = true;
}
}; };
os.value = platform();
onMounted(() => { onMounted(() => {
os.value = platform(); keyboard.down([Key.Escape], (event) => {
console.log("Escape key pressed");
keyboard.down("MetaLeft+Enter", (event) => { if (isKeybindInputFocused.value) {
if (os.value === "macos" && !isKeybindInputFocused.value) { keybindInput.value?.blur();
console.log("Save on macOS") } else {
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();
router.push("/"); 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();
}); });
</script> </script>