refactor: integrate keyboard functionality and clean up event handling

This commit is contained in:
PandaDEV 2025-03-13 15:29:02 +01:00
parent b2395e19a9
commit b6269c60c9
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
5 changed files with 55 additions and 27 deletions

View file

@ -9,13 +9,13 @@ import { listen } from "@tauri-apps/api/event";
import { app, window } from "@tauri-apps/api"; import { app, window } from "@tauri-apps/api";
import { disable, enable } from "@tauri-apps/plugin-autostart"; import { disable, enable } from "@tauri-apps/plugin-autostart";
import { onMounted } from "vue"; import { onMounted } from "vue";
import { keyboard } from "wrdu-keyboard";
const keyboard = useKeyboard();
const { $settings } = useNuxtApp(); const { $settings } = useNuxtApp();
keyboard.init()
onMounted(async () => { onMounted(async () => {
await listen("settings", async () => { await listen("settings", async () => {
keyboard.clear();
await navigateTo("/settings"); await navigateTo("/settings");
await app.show(); await app.show();
await window.getCurrentWindow().show(); await window.getCurrentWindow().show();

View file

@ -3,7 +3,6 @@ export default defineNuxtConfig({
devtools: { enabled: false }, devtools: { enabled: false },
compatibilityDate: "2024-07-04", compatibilityDate: "2024-07-04",
ssr: false, ssr: false,
modules: ["wrdu-keyboard"],
vite: { vite: {
css: { css: {
preprocessorOptions: { preprocessorOptions: {

View file

@ -174,7 +174,7 @@ import type {
InfoColor, InfoColor,
InfoCode, InfoCode,
} from "~/types/types"; } from "~/types/types";
import { Key } from "wrdu-keyboard/key"; import { Key, keyboard } from "wrdu-keyboard";
interface GroupedHistory { interface GroupedHistory {
label: string; label: string;
@ -207,8 +207,6 @@ const imageLoading = ref<boolean>(false);
const pageTitle = ref<string>(""); const pageTitle = ref<string>("");
const pageOgImage = ref<string>(""); const pageOgImage = ref<string>("");
const keyboard = useKeyboard();
const isSameDay = (date1: Date, date2: Date): boolean => { const isSameDay = (date1: Date, date2: Date): boolean => {
return ( return (
date1.getFullYear() === date2.getFullYear() && date1.getFullYear() === date2.getFullYear() &&
@ -593,39 +591,70 @@ const setupEventListeners = async (): Promise<void> => {
} }
} }
focusSearchInput(); focusSearchInput();
// Re-register keyboard shortcuts on focus
keyboard.clear();
keyboard.prevent.down([Key.DownArrow], () => {
selectNext();
});
keyboard.prevent.down([Key.UpArrow], () => {
selectPrevious();
});
keyboard.prevent.down([Key.Enter], () => {
pasteSelectedItem();
});
keyboard.prevent.down([Key.Escape], () => {
hideApp();
});
switch (os.value) {
case "macos":
keyboard.prevent.down([Key.LeftMeta, Key.K], () => {});
keyboard.prevent.down([Key.RightMeta, Key.K], () => {});
break;
case "linux":
case "windows":
keyboard.prevent.down([Key.LeftControl, Key.K], () => {});
keyboard.prevent.down([Key.RightControl, Key.K], () => {});
break;
}
}); });
await listen("tauri://blur", () => { await listen("tauri://blur", () => {
searchInput.value?.blur(); searchInput.value?.blur();
keyboard.clear();
}); });
keyboard.prevent.down([Key.DownArrow], (event) => { keyboard.prevent.down([Key.DownArrow], () => {
selectNext(); selectNext();
}); });
keyboard.prevent.down([Key.UpArrow], (event) => { keyboard.prevent.down([Key.UpArrow], () => {
selectPrevious(); selectPrevious();
}); });
keyboard.prevent.down([Key.Enter], (event) => { keyboard.prevent.down([Key.Enter], () => {
pasteSelectedItem(); pasteSelectedItem();
}); });
keyboard.prevent.down([Key.Escape], (event) => { keyboard.prevent.down([Key.Escape], () => {
hideApp(); hideApp();
}); });
switch (os.value) { switch (os.value) {
case "macos": case "macos":
keyboard.prevent.down([Key.LeftMeta, Key.K], (event) => {}); keyboard.prevent.down([Key.LeftMeta, Key.K], () => {});
keyboard.prevent.down([Key.RightMeta, Key.K], () => {});
keyboard.prevent.down([Key.RightMeta, Key.K], (event) => {});
break; break;
case "linux" || "windows": case "linux":
keyboard.prevent.down([Key.LeftControl, Key.K], (event) => {}); case "windows":
keyboard.prevent.down([Key.LeftControl, Key.K], () => {});
keyboard.prevent.down([Key.RightControl, Key.K], (event) => {}); keyboard.prevent.down([Key.RightControl, Key.K], () => {});
break; break;
} }
}; };

View file

@ -93,9 +93,9 @@ 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";
import { KeyValues, KeyLabels } from "../types/keys"; import { KeyValues, KeyLabels } from "../types/keys";
import { disable, enable } from "@tauri-apps/plugin-autostart"; import { disable, enable } from "@tauri-apps/plugin-autostart";
import { Key, keyboard } from "wrdu-keyboard";
const activeModifiers = reactive<Set<KeyValues>>(new Set()); const activeModifiers = reactive<Set<KeyValues>>(new Set());
const isKeybindInputFocused = ref(false); const isKeybindInputFocused = ref(false);
@ -104,7 +104,6 @@ const keybindInput = ref<HTMLElement | null>(null);
const lastBlurTime = ref(0); const lastBlurTime = ref(0);
const os = ref(""); const os = ref("");
const router = useRouter(); const router = useRouter();
const keyboard = useKeyboard();
const showEmptyKeybindError = ref(false); const showEmptyKeybindError = ref(false);
const autostart = ref(false); const autostart = ref(false);
const { $settings } = useNuxtApp(); const { $settings } = useNuxtApp();
@ -189,13 +188,13 @@ const toggleAutostart = async () => {
os.value = platform(); os.value = platform();
onMounted(async () => { onMounted(async () => {
keyboard.down([Key.All], (event) => { keyboard.prevent.down([Key.All], (event: KeyboardEvent) => {
if (isKeybindInputFocused.value) { if (isKeybindInputFocused.value) {
onKeyDown(event); onKeyDown(event);
} }
}); });
keyboard.down([Key.Escape], (event) => { keyboard.prevent.down([Key.Escape], () => {
if (isKeybindInputFocused.value) { if (isKeybindInputFocused.value) {
keybindInput.value?.blur(); keybindInput.value?.blur();
} else { } else {
@ -205,27 +204,28 @@ onMounted(async () => {
switch (os.value) { switch (os.value) {
case "macos": case "macos":
keyboard.down([Key.LeftMeta, Key.Enter], (event) => { keyboard.prevent.down([Key.LeftMeta, Key.Enter], () => {
if (!isKeybindInputFocused.value) { if (!isKeybindInputFocused.value) {
saveKeybind(); saveKeybind();
} }
}); });
keyboard.down([Key.RightMeta, Key.Enter], (event) => { keyboard.prevent.down([Key.RightMeta, Key.Enter], () => {
if (!isKeybindInputFocused.value) { if (!isKeybindInputFocused.value) {
saveKeybind(); saveKeybind();
} }
}); });
break; break;
case "linux" || "windows": case "linux":
keyboard.down([Key.LeftControl, Key.Enter], (event) => { case "windows":
keyboard.prevent.down([Key.LeftControl, Key.Enter], () => {
if (!isKeybindInputFocused.value) { if (!isKeybindInputFocused.value) {
saveKeybind(); saveKeybind();
} }
}); });
keyboard.down([Key.RightControl, Key.Enter], (event) => { keyboard.prevent.down([Key.RightControl, Key.Enter], () => {
if (!isKeybindInputFocused.value) { if (!isKeybindInputFocused.value) {
saveKeybind(); saveKeybind();
} }