SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
UI: recover chrome after sleep wakes a black window (#8749) (#8952)
SilentWolf7518e200f2628f
diff --git a/crates/ui-egui/src/icons.rs b/crates/ui-egui/src/icons.rsindex 05715320ba..3478838e9e 100644--- a/crates/ui-egui/src/icons.rs+++ b/crates/ui-egui/src/icons.rs@@ -32,6 +32,14 @@ enum Finish {#[derive(Clone, Default)]struct Cache(Arc<Mutex<CacheEntries>>);+/// Drop rasterised icon textures so the next paint uploads them again (wake from sleep, #8749).+pub(crate) fn clear_texture_cache(ctx: &egui::Context) {+ let cache = ctx.data_mut(|d| d.get_temp_mut_or_insert_with(egui::Id::new("sc_icon_cache"), Cache::default).clone());+ if let Ok(mut entries) = cache.0.lock() {+ entries.textures.clear();+ }+}+#[derive(Default)]struct CacheEntries {palette: Option<icon_svg::Palette>,diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex d997055090..7798a61c9c 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -216,6 +216,7 @@ pub mod recovery_ui;pub mod ref_images;mod render_ui;pub mod render_workspace;+pub mod resume_ui;#[cfg(test)]mod revolve_edit_tests;#[cfg(test)]@@ -2603,6 +2604,12 @@ impl SolveApp {/// Per-frame logic before layout.pub fn logic(&mut self, ctx: &egui::Context) {+ // Sleep/wake often skips frames without a device-lost signal; refresh egui GPU textures+ // when the frame clock jumps so the chrome is not stuck blank (#8749).+ let frame_time = ctx.input(|i| i.time);+ if resume_ui::is_long_pause(self.now, frame_time) {+ resume_ui::after_long_pause(ctx);+ }frame_watch::begin(self);gpu::tick_tessellation(self);file_picker::tick(self, ctx);diff --git a/crates/ui-egui/src/resume_ui.rs b/crates/ui-egui/src/resume_ui.rsnew file mode 100644index 0000000000..47927df283--- /dev/null+++ b/crates/ui-egui/src/resume_ui.rs@@ -0,0 +1,70 @@+//! Recover the egui chrome after the display wakes from sleep (#8749).+//!+//! On Linux (Wayland + NVIDIA especially) the window can stay black after wake: the device is not+//! reported lost, so SolidCraft does not restart, but GPU-backed egui textures (font atlas, icons)+//! are stale. The viewport often comes back on scroll or a tab change because it rebuilds its own+//! meshes; the ribbon and panels stay blank until those textures are uploaded again.+//!+//! When egui's frame clock jumps by more than [`LONG_PAUSE`], treat that as a wake and refresh.++use std::sync::Arc;++use egui::{ImageData, TextureId, TextureOptions};++/// A gap this long between frames means the process was not painting (sleep, lock screen, …).+pub const LONG_PAUSE: f64 = 1.5;++/// Re-upload the font atlas, drop cached icon GPU textures, and ask for another frame.+pub fn after_long_pause(ctx: &egui::Context) {+ refresh_font_atlas(ctx);+ crate::icons::clear_texture_cache(ctx);+ ctx.forget_all_images();+ ctx.request_repaint();+}++/// Push the full font atlas so the backend replaces TextureId::default on the GPU.+fn refresh_font_atlas(ctx: &egui::Context) {+ let image = ctx.fonts(|fonts| fonts.image());+ let delta = egui::epaint::ImageDelta::full(ImageData::Color(Arc::new(image)), TextureOptions::default());+ ctx.tex_manager().write().set(TextureId::default(), delta);+}++/// True when `now` is more than [`LONG_PAUSE`] after `previous` (and both are finite and ordered).+pub fn is_long_pause(previous: f64, now: f64) -> bool {+ previous.is_finite() && now.is_finite() && previous > 0.0 && now > previous && (now - previous) > LONG_PAUSE+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn long_pause_is_a_jump_in_frame_time() {+ assert!(!is_long_pause(0.0, 10.0), "first frame has no previous");+ assert!(!is_long_pause(1.0, 1.2));+ assert!(is_long_pause(1.0, 1.0 + LONG_PAUSE + 0.01));+ assert!(!is_long_pause(f64::NAN, 10.0));+ }++ #[test]+ fn after_long_pause_queues_a_full_font_atlas_upload() {+ let ctx = egui::Context::default();+ crate::fonts::setup_bundled_fonts(&ctx);+ // Build the atlas once so TextureId::default exists.+ let mut primed = ctx.run_ui(egui::RawInput::default(), |ui| {+ ui.label("wake");+ });+ primed.textures_delta.clear();+ after_long_pause(&ctx);+ // The refresh is applied as a tex_manager delta taken at the start of the next pass.+ let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {+ ui.label("again");+ });+ let font = out.textures_delta.set.get(&TextureId::default());+ assert!(+ font.is_some_and(|deltas| deltas.iter().any(|d| d.pos.is_none() && matches!(&d.image, ImageData::Color(img) if !img.pixels.is_empty()))),+ "full font atlas delta with pixels"+ );+ out.textures_delta.clear();+ }+}