From 8a09cf99f859fef27cd0507a4df950b3b21f6e10 Mon Sep 17 00:00:00 2001 From: OverloadedTech Date: Tue, 17 Nov 2026 17:42:33 +0100 Subject: [PATCH] Restore focus after closing a modal When a modal closed, the focus path still pointed into it, so it got trimmed down to the root and the text area took the focus. The search bar only closes on Escape while it has the focus, so after cancelling a dialog Escape no longer closed it. Closes #614 --- crates/edit/src/tui.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/edit/src/tui.rs b/crates/edit/src/tui.rs index fe5b5216874..0a770fd130b 100644 --- a/crates/edit/src/tui.rs +++ b/crates/edit/src/tui.rs @@ -360,6 +360,9 @@ pub struct Tui { /// This way we can track if the focus changed, because then we /// need to scroll the node into view if it's within a scrollarea. focused_node_for_scrolling: u64, + /// For each open modal, its node ID and the focus path from before it opened. + /// Once the modal is gone, the focus is returned to that path. + modal_focus_stack: Vec<(u64, Vec)>, /// The menubar button whose menu is open and can be closed by clicking it again. /// Only set once the press that opened the menu has ended. @@ -417,6 +420,7 @@ impl Tui { focused_node_path: Vec::with_capacity(16), focused_node_for_scrolling: ROOT_ID, + modal_focus_stack: Vec::new(), menubar_toggle_id: 0, cached_text_buffers: Vec::with_capacity(16), @@ -772,6 +776,15 @@ impl Tui { focus_path_pop_min = 1; } + // If a modal was closed while it had the focus, return the focus to where it was before. + while let Some((id, path)) = + self.modal_focus_stack.pop_if(|(id, _)| self.prev_node_map.get(*id).is_none()) + { + if self.focused_node_path.contains(&id) { + self.focused_node_path = path; + } + } + // Remove any unknown nodes from the focus path. // It's important that we do this after the tree has been swapped out, // so that pop_focusable_node() has access to the newest version of the tree. @@ -1796,6 +1809,11 @@ impl<'a> Context<'a, '_> { self.attr_background_rgba(self.tui.modal_default_bg); self.attr_foreground_rgba(self.tui.modal_default_fg); self.attr_focus_well(); + + let id = self.tree.last_node.borrow().id; + if self.tui.prev_node_map.get(id).is_none() { + self.tui.modal_focus_stack.push((id, self.tui.focused_node_path.clone())); + } self.focus_on_first_present(); let mut last_node = self.tree.last_node.borrow_mut();