From cc9fb9dea0817f6a3392b22e78a4e12eee2a9501 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 9 Nov 2023 22:23:36 -0700 Subject: [PATCH] Fix panic caused by focusing the same thing twice --- crates/command_palette2/src/command_palette.rs | 1 - crates/gpui2/src/window.rs | 4 ++++ crates/workspace2/src/modal_layer.rs | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/command_palette2/src/command_palette.rs b/crates/command_palette2/src/command_palette.rs index 385a3c875e..abba09519b 100644 --- a/crates/command_palette2/src/command_palette.rs +++ b/crates/command_palette2/src/command_palette.rs @@ -268,7 +268,6 @@ impl PickerDelegate for CommandPaletteDelegate { } fn dismissed(&mut self, cx: &mut ViewContext>) { - cx.focus(&self.previous_focus_handle); self.command_palette .update(cx, |_, cx| cx.emit(ModalEvent::Dismissed)) .log_err(); diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 0e60e28dc1..b020366ad0 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -407,6 +407,10 @@ impl<'a> WindowContext<'a> { /// Move focus to the element associated with the given `FocusHandle`. pub fn focus(&mut self, handle: &FocusHandle) { + if self.window.focus == Some(handle.id) { + return; + } + if self.window.last_blur.is_none() { self.window.last_blur = Some(self.window.focus); } diff --git a/crates/workspace2/src/modal_layer.rs b/crates/workspace2/src/modal_layer.rs index bffeec6c56..09ffa6c13f 100644 --- a/crates/workspace2/src/modal_layer.rs +++ b/crates/workspace2/src/modal_layer.rs @@ -36,8 +36,9 @@ impl ModalLayer { let previous_focus = cx.focused(); if let Some(active_modal) = &self.active_modal { - if active_modal.modal.clone().downcast::().is_ok() { - self.hide_modal(cx); + let is_close = active_modal.modal.clone().downcast::().is_ok(); + self.hide_modal(cx); + if is_close { return; } }