From 18d5ff85a5bf1994d77e3af3218c1d9e0e14c4be Mon Sep 17 00:00:00 2001 From: JasonEWNL Date: Sat, 2 Mar 2024 15:40:33 +0800 Subject: [PATCH 1/5] Add desktop shortcut support --- app/components/sidebar.tsx | 45 ++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- src-tauri/Cargo.toml | 1 + src-tauri/tauri.conf.json | 3 +++ yarn.lock | 5 +++++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/components/sidebar.tsx b/app/components/sidebar.tsx index 69b2e71f871..758a54ee793 100644 --- a/app/components/sidebar.tsx +++ b/app/components/sidebar.tsx @@ -31,6 +31,8 @@ import { isIOS, useMobileScreen } from "../utils"; import dynamic from "next/dynamic"; import { showConfirm, showToast } from "./ui-lib"; +import { register } from "@tauri-apps/api/globalShortcut"; + const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, { loading: () => null, }); @@ -128,6 +130,45 @@ function useDragSideBar() { }; } +function useGlobalShortcut() { + const chatStore = useChatStore(); + const navigate = useNavigate(); + const config = useAppConfig(); + + const handleMasks = async () => { + await register("CommandOrControl+Shift+M", () => { + if (config.dontShowMaskSplashScreen !== true) { + navigate(Path.NewChat, { state: { fromHome: true } }); + } else { + navigate(Path.Masks, { state: { fromHome: true } }); + } + }); + }; + + const handleSettings = async () => { + await register("CommandOrControl+,", () => { + navigate(Path.Settings); + }); + }; + + const handleNewChat = async () => { + await register("CommandOrControl+N", () => { + if (config.dontShowMaskSplashScreen) { + chatStore.newSession(); + navigate(Path.Chat); + } else { + navigate(Path.NewChat); + } + }); + }; + + useEffect(() => { + handleMasks(); + handleSettings(); + handleNewChat(); + }, []); +} + export function SideBar(props: { className?: string }) { const chatStore = useChatStore(); @@ -143,6 +184,10 @@ export function SideBar(props: { className?: string }) { useHotKey(); + if (window.__TAURI__) { + useGlobalShortcut(); + } + return (
Date: Sat, 2 Mar 2024 16:21:23 +0800 Subject: [PATCH 2/5] Bypass eslint warning --- app/components/sidebar.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/app/components/sidebar.tsx b/app/components/sidebar.tsx index 758a54ee793..51b829b6074 100644 --- a/app/components/sidebar.tsx +++ b/app/components/sidebar.tsx @@ -166,6 +166,7 @@ function useGlobalShortcut() { handleMasks(); handleSettings(); handleNewChat(); + // eslint-disable-next-line react-hooks/exhaustive-deps }, []); } From 96f7db1e92bfec42cf1678a80e4182aca6520777 Mon Sep 17 00:00:00 2001 From: JasonEWNL <32767591+JasonEWNL@users.noreply.github.com> Date: Sat, 2 Mar 2024 17:03:21 +0800 Subject: [PATCH 3/5] Fix conditionally hook --- app/components/sidebar.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/components/sidebar.tsx b/app/components/sidebar.tsx index 51b829b6074..8322e2d24f3 100644 --- a/app/components/sidebar.tsx +++ b/app/components/sidebar.tsx @@ -131,6 +131,10 @@ function useDragSideBar() { } function useGlobalShortcut() { + if (!window.__TAURI__) { + return; + } + const chatStore = useChatStore(); const navigate = useNavigate(); const config = useAppConfig(); @@ -185,9 +189,7 @@ export function SideBar(props: { className?: string }) { useHotKey(); - if (window.__TAURI__) { - useGlobalShortcut(); - } + useGlobalShortcut(); return (
Date: Sat, 2 Mar 2024 17:24:09 +0800 Subject: [PATCH 4/5] Move tauri env check inside effect --- app/components/sidebar.tsx | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/components/sidebar.tsx b/app/components/sidebar.tsx index 8322e2d24f3..ca06fb3e0cb 100644 --- a/app/components/sidebar.tsx +++ b/app/components/sidebar.tsx @@ -131,10 +131,6 @@ function useDragSideBar() { } function useGlobalShortcut() { - if (!window.__TAURI__) { - return; - } - const chatStore = useChatStore(); const navigate = useNavigate(); const config = useAppConfig(); @@ -167,10 +163,11 @@ function useGlobalShortcut() { }; useEffect(() => { - handleMasks(); - handleSettings(); - handleNewChat(); - // eslint-disable-next-line react-hooks/exhaustive-deps + if (window.__TAURI__) { + handleMasks(); + handleSettings(); + handleNewChat(); + } }, []); } From 80ef6a7c96bff001a54530b07d7c04a46da32592 Mon Sep 17 00:00:00 2001 From: JasonEWNL <32767591+JasonEWNL@users.noreply.github.com> Date: Sat, 2 Mar 2024 17:39:14 +0800 Subject: [PATCH 5/5] Refactor: Add dependencies and ignore eslint warning --- app/components/sidebar.tsx | 67 ++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/app/components/sidebar.tsx b/app/components/sidebar.tsx index ca06fb3e0cb..6cb71790f52 100644 --- a/app/components/sidebar.tsx +++ b/app/components/sidebar.tsx @@ -135,40 +135,45 @@ function useGlobalShortcut() { const navigate = useNavigate(); const config = useAppConfig(); - const handleMasks = async () => { - await register("CommandOrControl+Shift+M", () => { - if (config.dontShowMaskSplashScreen !== true) { - navigate(Path.NewChat, { state: { fromHome: true } }); - } else { - navigate(Path.Masks, { state: { fromHome: true } }); - } - }); - }; + useEffect(() => { + // Early return if not in a Tauri environment + if (!window.__TAURI__) { + return; + } - const handleSettings = async () => { - await register("CommandOrControl+,", () => { - navigate(Path.Settings); - }); - }; + const handleMasks = async () => { + await register("CommandOrControl+Shift+M", () => { + if (config.dontShowMaskSplashScreen !== true) { + navigate(Path.NewChat, { state: { fromHome: true } }); + } else { + navigate(Path.Masks, { state: { fromHome: true } }); + } + }); + }; - const handleNewChat = async () => { - await register("CommandOrControl+N", () => { - if (config.dontShowMaskSplashScreen) { - chatStore.newSession(); - navigate(Path.Chat); - } else { - navigate(Path.NewChat); - } - }); - }; + const handleSettings = async () => { + await register("CommandOrControl+,", () => { + navigate(Path.Settings); + }); + }; - useEffect(() => { - if (window.__TAURI__) { - handleMasks(); - handleSettings(); - handleNewChat(); - } - }, []); + const handleNewChat = async () => { + await register("CommandOrControl+N", () => { + if (config.dontShowMaskSplashScreen) { + chatStore.newSession(); + navigate(Path.Chat); + } else { + navigate(Path.NewChat); + } + }); + }; + + // Shortcut registration + handleMasks(); + handleSettings(); + handleNewChat(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [chatStore, navigate, config]); } export function SideBar(props: { className?: string }) {