Skip to content

Commit 5b0fdbc

Browse files
authored
feat: support esc to exit right-click menu (#415)
1 parent 88955e0 commit 5b0fdbc

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

src/components/Search/DropdownList.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useRef, useState, useCallback, MouseEvent } from "react";
22
import { CircleAlert, Bolt, X, ArrowBigRight } from "lucide-react";
33
import { isNil } from "lodash-es";
4-
import { useUnmount } from "ahooks";
4+
import { useDebounceFn, useUnmount } from "ahooks";
55

66
import { useSearchStore } from "@/stores/searchStore";
77
import ThemedIcon from "@/components/Common/Icons/ThemedIcon";
@@ -56,6 +56,7 @@ function DropdownList({
5656
};
5757

5858
useUnmount(() => {
59+
setSelectedItem(null);
5960
setSelectedSearchContent(void 0);
6061
});
6162

@@ -75,9 +76,13 @@ function DropdownList({
7576
}
7677
}, [isChatMode]);
7778

79+
const { run } = useDebounceFn(() => setSelectedItem(0), { wait: 200 });
80+
7881
useEffect(() => {
79-
setSelectedItem(0);
80-
}, [suggests]);
82+
setSelectedItem(null);
83+
84+
run();
85+
}, [SearchData]);
8186

8287
const openPopover = useShortcutsStore((state) => state.openPopover);
8388

src/components/Search/InputBox.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,23 @@ export default function ChatInput({
183183

184184
useKeyPress(`${modifierKey}.${returnToInput}`, handleToggleFocus);
185185

186+
const visibleContextMenu = useSearchStore((state) => {
187+
return state.visibleContextMenu;
188+
});
189+
const setVisibleContextMenu = useSearchStore((state) => {
190+
return state.setVisibleContextMenu;
191+
});
192+
186193
const handleKeyDown = useCallback(
187194
(e: KeyboardEvent) => {
188195
// console.log("handleKeyDown", e.code, e.key);
189196

190197
if (e.key === "Escape") {
191-
handleEscapeKey();
192-
return;
198+
if (visibleContextMenu) {
199+
return setVisibleContextMenu(false);
200+
}
201+
202+
return handleEscapeKey();
193203
}
194204

195205
pressedKeys.add(e.key);
@@ -239,6 +249,7 @@ export default function ChatInput({
239249
setIsCommandPressed,
240250
disabledChange,
241251
curChatEnd,
252+
visibleContextMenu,
242253
]
243254
);
244255

src/hooks/useEscape.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
import { useEffect } from "react";
1+
import { useCallback, useEffect } from "react";
22

33
import platformAdapter from "@/utils/platformAdapter";
4+
import { useSearchStore } from "@/stores/searchStore";
45

56
const useEscape = () => {
6-
const handleEscape = async (event: KeyboardEvent) => {
7-
if (event.key === "Escape") {
8-
console.log("Escape key pressed.");
7+
const visibleContextMenu = useSearchStore((state) => {
8+
return state.visibleContextMenu;
9+
});
10+
const setVisibleContextMenu = useSearchStore((state) => {
11+
return state.setVisibleContextMenu;
12+
});
913

10-
event.preventDefault();
14+
const handleEscape = useCallback(() => {
15+
async (event: KeyboardEvent) => {
16+
if (event.key === "Escape") {
17+
console.log("Escape key pressed.");
1118

12-
// Hide the Tauri app window when 'Esc' is pressed
13-
await platformAdapter.invokeBackend("hide_coco");
19+
event.preventDefault();
1420

15-
console.log("App window hidden successfully.");
16-
}
17-
};
21+
if (visibleContextMenu) {
22+
return setVisibleContextMenu(false);
23+
}
24+
25+
// Hide the Tauri app window when 'Esc' is pressed
26+
await platformAdapter.invokeBackend("hide_coco");
27+
28+
console.log("App window hidden successfully.");
29+
}
30+
};
31+
}, [visibleContextMenu]);
1832

1933
useEffect(() => {
2034
const unlisten = platformAdapter.listenEvent("tauri://focus", () => {

0 commit comments

Comments
 (0)