From 005076bbf01be18cb03551b33bada7e0d7ef52ee Mon Sep 17 00:00:00 2001 From: Jared Moulton Date: Sun, 20 Oct 2024 09:11:48 -0600 Subject: [PATCH] add clear app focus message and command (#632) this is like id.clear_focus exepct that it will clear the focus regardless of the id that is clearing the focus --- src/action.rs | 5 +++++ src/app_state.rs | 4 +++- src/update.rs | 1 + src/window_handle.rs | 7 +++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/action.rs b/src/action.rs index da32a721..f0f364c5 100644 --- a/src/action.rs +++ b/src/action.rs @@ -204,6 +204,11 @@ pub fn focus_window() { add_update_message(UpdateMessage::FocusWindow); } +/// Clear the app focus +pub fn clear_app_focus() { + add_update_message(UpdateMessage::ClearAppFocus); +} + /// Set whether ime input is shown pub fn set_ime_allowed(allowed: bool) { add_update_message(UpdateMessage::SetImeAllowed { allowed }); diff --git a/src/app_state.rs b/src/app_state.rs index 35eb4c80..ca9e885a 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -259,7 +259,9 @@ impl AppState { } } - self.prev_focus = self.focus; + if self.focus.is_some() { + self.prev_focus = self.focus; + } self.focus = None; } diff --git a/src/update.rs b/src/update.rs index 0b5fab26..2d264aa8 100644 --- a/src/update.rs +++ b/src/update.rs @@ -29,6 +29,7 @@ type DeferredUpdateMessages = HashMap)>>; pub(crate) enum UpdateMessage { Focus(ViewId), ClearFocus(ViewId), + ClearAppFocus, Active(ViewId), ClearActive(ViewId), WindowScale(f64), diff --git a/src/window_handle.rs b/src/window_handle.rs index 055fa0f7..d99c6c37 100644 --- a/src/window_handle.rs +++ b/src/window_handle.rs @@ -825,6 +825,13 @@ impl WindowHandle { cx.app_state.focus_changed(Some(id), None); } } + UpdateMessage::ClearAppFocus => { + let focus = cx.app_state.focus; + cx.app_state.clear_focus(); + if let Some(id) = focus { + cx.app_state.focus_changed(Some(id), None); + } + } UpdateMessage::Active(id) => { let old = cx.app_state.active; cx.app_state.active = Some(id);