Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/edit/src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>)>,

/// 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.
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down