Skip to content

Commit

Permalink
Fix crash when closing project with undocked map editors
Browse files Browse the repository at this point in the history
This commit fixes several different types of crashes that can occur when
using Close Project while a map editor tab is undocked into a window.
  • Loading branch information
white-axe committed Oct 22, 2023
1 parent 6089b0a commit 5a16a73
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/tabs/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,65 @@ where
pub fn clean_tabs<F: FnMut(&mut T) -> bool>(&self, mut f: F) {
let mut i = 0;
let mut state = self.state.lock();

let focused_id = state.find_active_focused().map(|(_, tab)| tab.id());
let mut active_tab = None;

loop {
let Some(surface) = state.get_surface_mut(egui_dock::SurfaceIndex(i)) else {
break;
};

if let Some(tree) = surface.node_tree_mut() {
for node in tree.iter_mut() {
let mut is_empty = !egui_dock::SurfaceIndex(i).is_main();

for (j, node) in tree.iter_mut().enumerate() {
if let egui_dock::Node::Leaf { tabs, .. } = node {
tabs.retain_mut(&mut f);

if !tabs.is_empty() {
is_empty = false;
}

// Check if the originally focused tab is inside this tree
if let Some(k) = focused_id.and_then(|id| {
tabs.iter().enumerate().find_map(|(i, tab)| {
if tab.id() == id {
Some(i)
} else {
None
}
})
}) {
active_tab = Some((i, j, k));
}
}
}

// We need to remove empty windows or we'll cause a crash
if is_empty {
state.remove_surface(egui_dock::SurfaceIndex(i));
}
}
i += 1;
}

// If the previously focused tab hasn't been removed, refocus it since the index may have
// changed, otherwise the application may crash due to out-of-bounds vector access
if let Some((i, j, k)) = active_tab {
state.set_active_tab((
egui_dock::SurfaceIndex(i),
egui_dock::NodeIndex(j),
egui_dock::TabIndex(k),
));
}
// Otherwise we need to unfocus all tabs, again to prevent a crash
else {
state.set_focused_node_and_surface((
egui_dock::SurfaceIndex(usize::MAX),
egui_dock::NodeIndex(usize::MAX),
));
}
}

/// Returns the name of the focused tab.
Expand Down

0 comments on commit 5a16a73

Please sign in to comment.