diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index a48de514..923c1069 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -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 {