Make sure we always track the root viewport (#7450)

Some sanity checks I added while working on another bug
This commit is contained in:
Emil Ernerfeldt 2025-08-14 12:03:38 +02:00 committed by GitHub
parent a7ae1012e5
commit 839ee3eaf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 1 deletions

View File

@ -770,6 +770,7 @@ impl Default for Context {
fn default() -> Self {
let ctx_impl = ContextImpl {
embed_viewports: true,
viewports: std::iter::once((ViewportId::ROOT, ViewportState::default())).collect(),
..Default::default()
};
let ctx = Self(Arc::new(RwLock::new(ctx_impl)));
@ -1617,7 +1618,14 @@ impl Context {
self.read(|ctx| {
ctx.viewports
.get(&id)
.map_or(0, |v| v.repaint.cumulative_frame_nr)
.map(|v| v.repaint.cumulative_frame_nr)
.unwrap_or_else(|| {
if cfg!(debug_assertions) {
panic!("cumulative_frame_nr_for failed to find the viewport {id:?}");
} else {
0
}
})
})
}
@ -2516,6 +2524,10 @@ impl ContextImpl {
self.last_viewport = ended_viewport_id;
self.viewports.retain(|&id, viewport| {
if id == ViewportId::ROOT {
return true; // never remove the root
}
let parent = *self.viewport_parents.entry(id).or_default();
if !all_viewport_ids.contains(&parent) {
@ -2586,6 +2598,10 @@ impl ContextImpl {
if is_last {
// Remove dead viewports:
self.viewports.retain(|id, _| all_viewport_ids.contains(id));
debug_assert!(
self.viewports.contains_key(&ViewportId::ROOT),
"Bug in egui: we removed the root viewport"
);
self.viewport_parents
.retain(|id, _| all_viewport_ids.contains(id));
} else {