From 6aec9cbae25d70cf4005443f4512f22107bb894c Mon Sep 17 00:00:00 2001 From: ayangweb <75017711+ayangweb@users.noreply.github.com> Date: Sat, 11 Oct 2025 14:46:24 +0800 Subject: [PATCH] fix: resolve pinned window shortcut not working (#917) * fix: fix pinned window shortcut not working * docs: update changelog * refactor: update * Update _index.md --------- Co-authored-by: Medcl --- docs/content.en/docs/release-notes/_index.md | 1 + src/components/Assistant/ChatHeader.tsx | 31 ++---------- src/components/Common/TogglePin.tsx | 50 ++++++++++++++++++++ src/components/Common/UI/Footer.tsx | 41 ++++------------ src/components/Common/VisibleKey.tsx | 15 +++++- 5 files changed, 78 insertions(+), 60 deletions(-) create mode 100644 src/components/Common/TogglePin.tsx diff --git a/docs/content.en/docs/release-notes/_index.md b/docs/content.en/docs/release-notes/_index.md index 97289085..b1d6a170 100644 --- a/docs/content.en/docs/release-notes/_index.md +++ b/docs/content.en/docs/release-notes/_index.md @@ -20,6 +20,7 @@ feat: support opening logs from about page #915 fix: automatic update of service list #913 fix: duplicate chat content #916 +fix: resolve pinned window shortcut not working (#917) ### ✈️ Improvements diff --git a/src/components/Assistant/ChatHeader.tsx b/src/components/Assistant/ChatHeader.tsx index 1f19e39d..97e7f59d 100644 --- a/src/components/Assistant/ChatHeader.tsx +++ b/src/components/Assistant/ChatHeader.tsx @@ -1,9 +1,6 @@ import { MessageSquarePlus } from "lucide-react"; -import clsx from "clsx"; import HistoryIcon from "@/icons/History"; -import PinOffIcon from "@/icons/PinOff"; -import PinIcon from "@/icons/Pin"; import WindowsFullIcon from "@/icons/WindowsFull"; import { useAppStore } from "@/stores/appStore"; import type { Chat } from "@/types/chat"; @@ -12,6 +9,7 @@ import { useShortcutsStore } from "@/stores/shortcutsStore"; import { HISTORY_PANEL_ID } from "@/constants"; import { AssistantList } from "./AssistantList"; import { ServerList } from "./ServerList"; +import TogglePin from "../Common/TogglePin"; interface ChatHeaderProps { clearChat: () => void; @@ -34,21 +32,9 @@ export function ChatHeader({ showChatHistory = true, assistantIDs, }: ChatHeaderProps) { - const { isPinned, setIsPinned, isTauri } = useAppStore(); + const { isTauri } = useAppStore(); - const { historicalRecords, newSession, fixedWindow, external } = - useShortcutsStore(); - - const togglePin = async () => { - try { - const { isPinned } = useAppStore.getState(); - - setIsPinned(!isPinned); - } catch (err) { - console.error("Failed to toggle window pin state:", err); - setIsPinned(isPinned); - } - }; + const { historicalRecords, newSession, external } = useShortcutsStore(); return (
- + diff --git a/src/components/Common/TogglePin.tsx b/src/components/Common/TogglePin.tsx new file mode 100644 index 00000000..36d9ff19 --- /dev/null +++ b/src/components/Common/TogglePin.tsx @@ -0,0 +1,50 @@ +import { useAppStore } from "@/stores/appStore"; +import { useShortcutsStore } from "@/stores/shortcutsStore"; +import clsx from "clsx"; +import VisibleKey from "./VisibleKey"; +import { FC, HTMLAttributes } from "react"; +import PinOffIcon from "@/icons/PinOff"; +import PinIcon from "@/icons/Pin"; + +interface TogglePinProps extends HTMLAttributes { + setIsPinnedWeb?: (value: boolean) => void; +} + +const TogglePin: FC = (props) => { + const { className, setIsPinnedWeb } = props; + const { isPinned, setIsPinned } = useAppStore(); + const { fixedWindow } = useShortcutsStore(); + + const togglePin = async () => { + const { isTauri, isPinned } = useAppStore.getState(); + + try { + const nextPinned = !isPinned; + + if (!isTauri) { + setIsPinnedWeb?.(nextPinned); + } + + setIsPinned(nextPinned); + } catch (err) { + setIsPinned(isPinned); + + console.error("Failed to toggle window pin state:", err); + } + }; + + return ( + + ); +}; + +export default TogglePin; diff --git a/src/components/Common/UI/Footer.tsx b/src/components/Common/UI/Footer.tsx index 38330605..1c5ff295 100644 --- a/src/components/Common/UI/Footer.tsx +++ b/src/components/Common/UI/Footer.tsx @@ -5,13 +5,10 @@ import clsx from "clsx"; import CommonIcon from "@/components/Common/Icons/CommonIcon"; import Copyright from "@/components/Common/Copyright"; -import PinOffIcon from "@/icons/PinOff"; -import PinIcon from "@/icons/Pin"; import logoImg from "@/assets/icon.svg"; import { useAppStore } from "@/stores/appStore"; import { useSearchStore } from "@/stores/searchStore"; import { useUpdateStore } from "@/stores/updateStore"; -import VisibleKey from "../VisibleKey"; import { useShortcutsStore } from "@/stores/shortcutsStore"; import { formatKey } from "@/utils/keyboardUtils"; import source_default_img from "@/assets/images/source_default.png"; @@ -19,6 +16,7 @@ import source_default_dark_img from "@/assets/images/source_default_dark.png"; import { useThemeStore } from "@/stores/themeStore"; import platformAdapter from "@/utils/platformAdapter"; import FontIcon from "../Icons/FontIcon"; +import TogglePin from "../TogglePin"; interface FooterProps { setIsPinnedWeb?: (value: boolean) => void; @@ -37,28 +35,11 @@ export default function Footer({ setIsPinnedWeb }: FooterProps) { const isDark = useThemeStore((state) => state.isDark); - const { isTauri, isPinned, setIsPinned } = useAppStore(); + const { isTauri } = useAppStore(); const { setVisible, updateInfo, skipVersions } = useUpdateStore(); - const { fixedWindow, modifierKey } = useShortcutsStore(); - - const togglePin = async () => { - try { - const { isTauri, isPinned } = useAppStore.getState(); - - const nextPinned = !isPinned; - - if (!isTauri) { - setIsPinnedWeb?.(nextPinned); - } - - setIsPinned(nextPinned); - } catch (err) { - console.error("Failed to toggle window pin state:", err); - setIsPinned(isPinned); - } - }; + const { modifierKey } = useShortcutsStore(); const openSetting = useCallback(() => { return platformAdapter.emitEvent("open_settings", ""); @@ -88,7 +69,10 @@ export default function Footer({ setIsPinnedWeb }: FooterProps) { if (visibleExtensionDetail && selectedExtension) { return (
- + {selectedExtension.name}
); @@ -139,17 +123,12 @@ export default function Footer({ setIsPinnedWeb }: FooterProps) {
{renderLeft()} - + setIsPinnedWeb={setIsPinnedWeb} + />
) : ( diff --git a/src/components/Common/VisibleKey.tsx b/src/components/Common/VisibleKey.tsx index 1287cc59..5099ede2 100644 --- a/src/components/Common/VisibleKey.tsx +++ b/src/components/Common/VisibleKey.tsx @@ -6,6 +6,9 @@ import { last } from "lodash-es"; import { POPOVER_PANEL_SELECTOR } from "@/constants"; import { useShortcutsStore } from "@/stores/shortcutsStore"; import { useAppStore } from "@/stores/appStore"; +import { KeyType } from "ahooks/lib/useKeyPress"; + +const keyTriggerMap = new Map(); interface VisibleKeyProps extends HTMLAttributes { shortcut: string; @@ -60,8 +63,16 @@ const VisibleKey: FC = (props) => { setVisibleShortcut(isChildInPopover && modifierKeyPressed); }, [openPopover, modifierKeyPressed]); - useKeyPress(`${modifierKey}.${shortcut}`, (event) => { - if (!visibleShortcut) return; + useKeyPress(`${modifierKey}.${shortcut}`, (event, key) => { + if (!visibleShortcut || event.repeat) return; + + const now = Date.now(); + const last = keyTriggerMap.get(key) ?? 0; + const wait = 100; + + if (now - last < wait) return; + + keyTriggerMap.set(key, now); event.stopPropagation(); event.preventDefault();