diff --git a/crates/ui-egui/src/find_font.rs b/crates/ui-egui/src/find_font.rs index e2f384cc95..65e2a030d2 100644 --- a/crates/ui-egui/src/find_font.rs +++ b/crates/ui-egui/src/find_font.rs @@ -332,7 +332,15 @@ fn run_button(app: &mut VectorcraftApp, d: &FindFontDialog, button: Action, from } } +#[cfg(test)] +thread_local! { + /// How many times this thread asked the engine for the document's fonts ([`fonts`]). + static FONT_LISTS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + fn fonts(app: &mut VectorcraftApp) -> Vec { + #[cfg(test)] + FONT_LISTS.set(FONT_LISTS.get() + 1); app.session.execute("text.fonts", &json!({})).ok().and_then(|v| v.as_array().cloned()).unwrap_or_default() } @@ -374,6 +382,10 @@ fn change(app: &mut VectorcraftApp, d: &FindFontDialog, from: &Value, all: bool) /// acts, and the copy goes back. pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &Context) { let Some(dialogs) = app.dialogs() else { return }; + // The document's fonts are listed only while the dialog is open. + if !dialogs.with(|host| host.get_mut::().is_some()) { + return; + } let (used, offer_folder) = current_fonts(app); let taken = dialogs.with(|host| { let d = host.get_mut::()?; @@ -661,6 +673,37 @@ mod tests { pump(app, &ctx); } + /// While the dialog is closed, the pump does not list the document's fonts, which walks the whole + /// document, after each edit. Once it is open, an edit lists them again. + #[test] + fn the_pump_lists_the_fonts_only_while_the_dialog_is_open() { + let mut app = new_app(); + app.session.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); + app.session.execute("text.create", &json!({"x": 10, "y": 40, "text": "Hi", "font": "Inter"})).unwrap(); + FONT_LISTS.set(0); + for i in 0..5 { + app.session.execute("shape.rectangle", &json!({"x": i * 10, "y": 0, "width": 5, "height": 5})).unwrap(); + pump_now(&mut app); + } + assert_eq!(FONT_LISTS.get(), 0, "fonts listed with the dialog closed"); + open(&mut app); + assert_eq!(FONT_LISTS.get(), 1, "opening lists the fonts once"); + // Another test thread that changes the available fonts during the two passes makes the + // second pass list them again, so the edit is repeated until the generation stays the same. + for i in 0..20 { + let generation = vectorcraft_text::FontDb::global().generation(); + let before = FONT_LISTS.get(); + app.session.execute("shape.rectangle", &json!({"x": 60 + i, "y": 0, "width": 5, "height": 5})).unwrap(); + pump_now(&mut app); + pump_now(&mut app); + if vectorcraft_text::FontDb::global().generation() == generation { + assert_eq!(FONT_LISTS.get(), before + 1, "an edit lists the fonts again once while the dialog is open"); + return; + } + } + panic!("the available fonts kept changing") + } + /// Find In passes Find the active artboard, or the selected layers (else the current /// layer), as `select.font`'s scope. The choice is set as automation sets it, and Find runs /// from the pump.