From 1474c17b0d436f0c39dbbbae1d59b7f872f9ac12 Mon Sep 17 00:00:00 2001 From: rustbasic <127506429+rustbasic@users.noreply.github.com> Date: Wed, 18 Sep 2024 02:49:53 +0900 Subject: [PATCH] Fix: panic when dragging window between monitors of different pixels_per_point (#4868) Fix: panic when dragging window between monitors of different pixels_per_point This will continue to help us as we develop `egui`. I hope you agree with my defense of `panic`. * Relate #3959 * Relate #4088 * Closes #4178 * Closes #4179 There is also a way to add log if necessary. ``` log::debug!("Anti-panic behavior occurs"); ``` --------- Co-authored-by: Emil Ernerfeldt --- crates/egui/src/context.rs | 18 ++++++++++++------ crates/egui/src/layers.rs | 6 ++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 53ce7661531..ec899461a19 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -2418,12 +2418,18 @@ impl Context { self.write(|ctx| { let tessellation_options = ctx.memory.options.tessellation_options; - let texture_atlas = ctx - .fonts - .get(&pixels_per_point.into()) - .expect("tessellate called with a different pixels_per_point than the font atlas was created with. \ - You should use egui::FullOutput::pixels_per_point when tessellating.") - .texture_atlas(); + let texture_atlas = if let Some(fonts) = ctx.fonts.get(&pixels_per_point.into()) { + fonts.texture_atlas() + } else { + #[cfg(feature = "log")] + log::warn!("No font size matching {pixels_per_point} pixels per point found."); + ctx.fonts + .iter() + .next() + .expect("No fonts loaded") + .1 + .texture_atlas() + }; let (font_tex_size, prepared_discs) = { let atlas = texture_atlas.lock(); (atlas.size(), atlas.prepared_discs()) diff --git a/crates/egui/src/layers.rs b/crates/egui/src/layers.rs index 66bbd338387..9105ece2591 100644 --- a/crates/egui/src/layers.rs +++ b/crates/egui/src/layers.rs @@ -153,6 +153,12 @@ impl PaintList { /// and then later setting it using `paint_list.set(idx, cr, frame);`. #[inline(always)] pub fn set(&mut self, idx: ShapeIdx, clip_rect: Rect, shape: Shape) { + if self.0.len() <= idx.0 { + #[cfg(feature = "log")] + log::warn!("Index {} is out of bounds for PaintList", idx.0); + return; + } + self.0[idx.0] = ClippedShape { clip_rect, shape }; }