Skip to content

Commit 16ac94e

Browse files
committed
fix(ui): Optimize Escape key handling in query box view
- Add debounce mechanism to prevent multiple hide app calls - Handle both keydown and keyup events for Escape key - Improve window hide behavior with timestamp-based throttling
1 parent 493bbf8 commit 16ac94e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

wox.ui.flutter/wox/lib/modules/launcher/views/wox_query_box_view.dart

+20-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'package:wox/utils/log.dart';
1010

1111
class WoxQueryBoxView extends GetView<WoxLauncherController> {
1212
const WoxQueryBoxView({super.key});
13+
14+
static int _lastHideAppTimestamp = 0;
1315

1416
@override
1517
Widget build(BuildContext context) {
@@ -23,11 +25,26 @@ class WoxQueryBoxView extends GetView<WoxLauncherController> {
2325
onKeyEvent: (FocusNode node, KeyEvent event) {
2426
var isAnyModifierPressed = WoxHotkey.isAnyModifierPressed();
2527
if (!isAnyModifierPressed) {
28+
// handle escape key
29+
// On Windows, the keydown and keyup events are triggered sequentially (strange behavior but true). If you only listen for the keydown event individually,
30+
// it will require pressing the Esc key twice for the keydown event to be captured.
31+
//
32+
// Solution:
33+
// 1. Listen for both keydown and keyup events
34+
// 2. Debounce the hide app action to prevent it from being called multiple times
35+
if (event is KeyDownEvent || event is KeyUpEvent) {
36+
if (event.logicalKey == LogicalKeyboardKey.escape) {
37+
int currentTimestamp = DateTime.now().millisecondsSinceEpoch;
38+
if (currentTimestamp - _lastHideAppTimestamp > 300) {
39+
_lastHideAppTimestamp = currentTimestamp;
40+
controller.hideApp(const UuidV4().generate());
41+
}
42+
return KeyEventResult.handled;
43+
}
44+
}
45+
2646
if (event is KeyDownEvent) {
2747
switch (event.logicalKey) {
28-
case LogicalKeyboardKey.escape:
29-
controller.hideApp(const UuidV4().generate());
30-
return KeyEventResult.handled;
3148
case LogicalKeyboardKey.arrowDown:
3249
controller.handleQueryBoxArrowDown();
3350
return KeyEventResult.handled;

0 commit comments

Comments
 (0)