From 2625051f75adf52c046a1ffd404ef3d2c669c373 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 13 Nov 2023 11:32:05 -0700 Subject: [PATCH] Better fix for multiple focuses in one frame --- crates/gpui2/src/app.rs | 17 +++++++++++------ crates/gpui2/src/window.rs | 4 ---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rs index 356cf1b76bf5bcf40e15cb7ae9f10e3511db73d1..61c6195d903095105b741caed25e408a49519cf1 100644 --- a/crates/gpui2/src/app.rs +++ b/crates/gpui2/src/app.rs @@ -641,14 +641,19 @@ impl AppContext { // The window might change focus multiple times in an effect cycle. // We only honor effects for the most recently focused handle. if cx.window.focus == focused { + // if someone calls focus multiple times in one frame with the same handle + // the first apply_focus_changed_effect will have taken the last blur already + // and run the rest of this, so we can return. + let Some(last_blur) = cx.window.last_blur.take() else { + return; + }; + let focused = focused .map(|id| FocusHandle::for_id(id, &cx.window.focus_handles).unwrap()); - let blurred = cx - .window - .last_blur - .take() - .unwrap() - .and_then(|id| FocusHandle::for_id(id, &cx.window.focus_handles)); + + let blurred = + last_blur.and_then(|id| FocusHandle::for_id(id, &cx.window.focus_handles)); + let focus_changed = focused.is_some() || blurred.is_some(); let event = FocusEvent { focused, blurred }; diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index f574d7eb5f5c6ee14031a99bd79aabd59bae1424..11878c15fa29de5ea38584ad0c739a924d8a25cc 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -389,10 +389,6 @@ impl<'a> WindowContext<'a> { pub fn focus(&mut self, handle: &FocusHandle) { let focus_id = handle.id; - if self.window.focus == Some(focus_id) { - return; - } - if self.window.last_blur.is_none() { self.window.last_blur = Some(self.window.focus); }