Better fix for multiple focuses in one frame

Conrad Irwin created

Change summary

crates/gpui2/src/app.rs    | 17 +++++++++++------
crates/gpui2/src/window.rs |  4 ----
2 files changed, 11 insertions(+), 10 deletions(-)

Detailed changes

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 };
 

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);
         }