Skip to content

Commit 5943843

Browse files
committed
Fix esc key press
1 parent df8a179 commit 5943843

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub enum Message {
5151
RunFunction(Function),
5252
OpenFocused,
5353
ReturnFocus,
54+
EscKeyPressed(Id),
5455
ClearSearchResults,
5556
WindowFocusChanged(Id, bool),
5657
ClearSearchQuery,

src/app/tile.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use global_hotkey::{GlobalHotKeyEvent, HotKeyState};
1515

1616
use iced::futures::SinkExt;
1717
use iced::futures::channel::mpsc::{Sender, channel};
18-
use iced::window;
1918
use iced::{
2019
Element, Subscription, Task, Theme, futures,
2120
keyboard::{self, key::Named},
2221
stream,
2322
};
23+
use iced::{event, window};
2424

2525
use objc2::rc::Retained;
2626
use objc2_app_kit::NSRunningApplication;
@@ -136,8 +136,18 @@ impl Tile {
136136
/// - Keypresses (escape to close the window)
137137
/// - Window focus changes
138138
pub fn subscription(&self) -> Subscription<Message> {
139+
let keyboard = event::listen_with(|event, _, id| match event {
140+
event::Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => match key {
141+
keyboard::Key::Named(keyboard::key::Named::Escape) => {
142+
Some(Message::EscKeyPressed(id))
143+
}
144+
_ => None,
145+
},
146+
_ => None,
147+
});
139148
Subscription::batch([
140149
Subscription::run(handle_hotkeys),
150+
keyboard,
141151
Subscription::run(handle_recipient),
142152
Subscription::run(handle_hot_reloading),
143153
Subscription::run(handle_clipboard_history),

src/app/tile/update.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
6363
Task::none()
6464
}
6565

66+
Message::EscKeyPressed(id) => {
67+
if tile.query_lc.is_empty() {
68+
Task::done(Message::HideWindow(id))
69+
} else {
70+
Task::batch(vec![
71+
Task::done(Message::ClearSearchQuery),
72+
Task::done(Message::ClearSearchResults),
73+
])
74+
}
75+
}
76+
6677
Message::SearchQueryChanged(input, id) => {
6778
tile.focus_id = 0;
6879
#[cfg(target_os = "macos")]

0 commit comments

Comments
 (0)