From 0b1658228bd3d58f4185dc19c7608af89fb7911a Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Mon, 13 Jan 2025 19:42:07 -0500 Subject: [PATCH] Feature: register hotkey in main window (#60) * feat: enhance hotkey registration and initialization process * fix: jsr test (cherrypicked from another branch) * fix: fix nuxt tailwind version, the latest verison has bug * update pnpm lock --- apps/desktop/src/lib/utils/hotkey.ts | 4 ++-- apps/desktop/src/lib/utils/init.ts | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/lib/utils/hotkey.ts b/apps/desktop/src/lib/utils/hotkey.ts index 5a134bc..11a97ae 100644 --- a/apps/desktop/src/lib/utils/hotkey.ts +++ b/apps/desktop/src/lib/utils/hotkey.ts @@ -1,6 +1,6 @@ import { getAllWindows } from "@tauri-apps/api/window" import { isRegistered, register, unregister } from "@tauri-apps/plugin-global-shortcut" -import { debug, info } from "@tauri-apps/plugin-log" +import { debug, info, warn } from "@tauri-apps/plugin-log" import { sendNotificationWithPermission } from "./notification" /** @@ -16,7 +16,7 @@ export function mapKeyToTauriKey(key: string): string { export async function registerAppHotkey(hotkeyStr: string) { if (await isRegistered(hotkeyStr)) { - debug(`Hotkey (${hotkeyStr}) already registered`) + warn(`Hotkey (${hotkeyStr}) already registered`) await unregister(hotkeyStr) } info(`Registering hotkey: ${hotkeyStr}`) diff --git a/apps/desktop/src/lib/utils/init.ts b/apps/desktop/src/lib/utils/init.ts index d33bce9..dc6ef64 100644 --- a/apps/desktop/src/lib/utils/init.ts +++ b/apps/desktop/src/lib/utils/init.ts @@ -1,5 +1,8 @@ +import { appConfig } from "@/stores" import { getCurrentWindow } from "@tauri-apps/api/window" +import { info } from "@tauri-apps/plugin-log" import { dev } from "$app/environment" +import { mapKeyToTauriKey, registerAppHotkey } from "./hotkey" /** * Initialize the app @@ -19,11 +22,15 @@ export function init() { } export function initMainWindow() { - // const window = getCurrentWindow() - // if (window.label === "main") { - // window.onCloseRequested((event) => { - // event.preventDefault() - // window.hide() - // }) - // } + /* -------------------------------------------------------------------------- */ + /* Register App Hotkey */ + /* -------------------------------------------------------------------------- */ + const triggerHotkey = appConfig.get().triggerHotkey + if (triggerHotkey && triggerHotkey.length > 0) { + const hotkeyStr = triggerHotkey.map(mapKeyToTauriKey).join("+") + info(`Registering hotkey: ${hotkeyStr}`) + registerAppHotkey(hotkeyStr) + } else { + console.log("No hotkey found in confi") + } }