From ebe2f430d8b63229e158a348fec07e87117018ed Mon Sep 17 00:00:00 2001 From: batistalucasv Date: Tue, 17 Nov 2026 05:40:28 -0300 Subject: [PATCH] UI: Invalidate Home Styles preview textures on font changes (#5957) (#6029) Co-authored-by: Shabir Ahmad --- crates/ui-egui/src/previews.rs | 24 +++++++++++++++++----- crates/ui-egui/src/previews/tests.rs | 30 ++++++++++++++++++++++++++++ docs/ui-parity.md | 1 + 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/previews.rs b/crates/ui-egui/src/previews.rs index 1ff99e1073..e618034cf8 100644 --- a/crates/ui-egui/src/previews.rs +++ b/crates/ui-egui/src/previews.rs @@ -180,13 +180,20 @@ fn font_key(name: &str, ppp: f32) -> String { } impl Previews { + /// Ensure the cached family lists and style preview textures match the current font generation. + fn check_fonts(&mut self) { + let generation = wordcraft_fonts::FontDb::global().generation(); + if self.font_gen != generation { + self.fonts_changed(); + } + } + /// The font list: installed families, each by its name in the interface language when it has /// one (宋体 for SimSun in Chinese, #505), like Word's. pub fn families(&mut self) -> Vec { + self.check_fonts(); let lang = crate::i18n::current().code(); - let generation = wordcraft_fonts::FontDb::global().generation(); - if self.families.as_ref().is_none_or(|(l, _)| *l != lang) || self.font_gen != generation { - self.font_gen = generation; + if self.families.as_ref().is_none_or(|(l, _)| *l != lang) { let db = wordcraft_fonts::FontDb::global(); let mut f: Vec = db.families_in(lang).into_iter().filter(|f| !f.starts_with('.')).collect(); for w in [wordcraft_doc::styles::BODY_FONT, wordcraft_doc::styles::HEADING_FONT] { @@ -237,11 +244,15 @@ impl Previews { self.english_names.get(shown).cloned().unwrap_or_else(|| shown.to_string()) } - /// Fonts were added or removed: list the families again. The font menus' entries see the - /// change themselves ([`FontAtlas`]), in every window. + /// Fonts were added or removed: list the families again and clear cached style preview + /// textures so any style rendered with a fallback face refreshes with newly available fonts. + /// The font menus' entries see the change themselves ([`FontAtlas`]), in every window. pub fn fonts_changed(&mut self) { self.families = None; self.east_asian = None; + // Style previews may have been rasterised with a fallback face before a font was installed. + self.tex.clear(); + self.font_gen = wordcraft_fonts::FontDb::global().generation(); } /// A closure drawing a font-menu entry in its own face, from the fonts' atlas. @@ -299,7 +310,10 @@ impl Previews { }) } + /// Look up a cached preview texture under `key`, or render and insert it if missing. + /// Invalidates the cache first if the font generation has changed. pub(crate) fn get_or(&mut self, ctx: &egui::Context, key: &str, make: impl FnOnce() -> Option) -> Option { + self.check_fonts(); if let Some(t) = self.tex.get(key) { return Some(t.clone()); } diff --git a/crates/ui-egui/src/previews/tests.rs b/crates/ui-egui/src/previews/tests.rs index 7a56434542..271196437b 100644 --- a/crates/ui-egui/src/previews/tests.rs +++ b/crates/ui-egui/src/previews/tests.rs @@ -64,6 +64,36 @@ fn gallery_style(name: &str) -> wordcraft_doc::styles::Style { wordcraft_doc::styles::Style { id: "Backfill".into(), name: name.into(), quick: true, priority: Some(0), ..Default::default() } } +/// Seeded style preview textures are invalidated when fonts change so that newly available +/// faces replace fallback fonts used in earlier renders (#5957). +#[test] +fn font_changes_invalidate_cached_style_preview_textures() { + let ctx = egui::Context::default(); + let mut previews = Previews::default(); + let texture = ctx.load_texture("test-style-preview", egui::ColorImage::new([1, 1], vec![egui::Color32::BLACK]), egui::TextureOptions::LINEAR); + previews.tex.insert("style:Normal:cached".into(), texture); + previews.fonts_changed(); + assert!(previews.tex.is_empty(), "a new font can replace the fallback used by cached style previews"); +} + +/// Previews instances (such as other open windows) that missed an active `watch_fonts` call +/// invalidate their cached preview textures on their next lookup once the font generation advances (#5957). +#[test] +fn stale_previews_invalidate_on_next_lookup() { + let ctx = egui::Context::default(); + let mut previews = Previews::default(); + let texture = ctx.load_texture("test-style-preview", egui::ColorImage::new([1, 1], vec![egui::Color32::BLACK]), egui::TextureOptions::LINEAR); + previews.tex.insert("style:Normal:cached".into(), texture); + // Simulate this Previews instance having rendered at an older font generation. + previews.font_gen = wordcraft_fonts::FontDb::global().generation().wrapping_sub(1); + let mut made = false; + let _ = previews.get_or(&ctx, "style:Normal:cached", || { + made = true; + Some(egui::ColorImage::new([1, 1], vec![egui::Color32::WHITE])) + }); + assert!(made, "the cached preview was invalidated and re-rendered because the font generation changed"); +} + /// Regression test for #2: a style gallery label longer than its tile was painted at full width /// and ran into the neighbouring tiles; it is now cut with an ellipsis to fit the tile. #[test] diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 2fb2379831..c4b899b397 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -134,6 +134,7 @@ Page Setup's margin, paper-size and document-grid number boxes scroll long text | 2026-11-14 | trivial | Save As and Export (also the formats on the Print page) offer Browse in WordCraft… on Linux/BSD, as Open does; exporting there lists Export's formats as the types, whose extension replaces a known document extension on the name; an export refusal or failure shows on the Export page, as Save As's does (#3535) | | 2026-11-16 | trivial | Language dialog whole-document OK: Do not check sent only when clicked; `review.language` document scope returns the caret result with `scope`; glossary skipped while tracking formatting (result says `glossarySkipped`) (#3518) | | 2026-11-14 | trivial | Language dialog: Apply to the whole document sets the proofing language or Do not check for every story (body, headers, footers, notes, comments, text boxes, tables, glossary) in one undo step; `review.language` takes `scope: "document"` (#3518) | +| 2026-11-10 | trivial | Home Styles gallery previews invalidate cached raster textures when the font database changes (#5957) | | 2026-10-26 | trivial | Dialog sections: 13 dialogs group related controls in theme-aware panels with indented settings; tall dialogs scroll only when needed (#4035, #4038) | | 2026-11-12 | trivial | Clipboard: a copied picture is published as PNG (and HTML where the host writes it); WordCraft's own paste still keeps the original fragment, and a copy of only shapes carries no HTML or RTF (#6205). Native external-app paste remains untested. | | 2026-11-07 | trivial | Vertical scrollbar track: clicking above the elevator moves one screen up and clicking below it moves one screen down, as in Word, instead of jumping to that position in the document (#5853) |