From d63d6f4db1e61c06af2bf170a0a6ab0088326c20 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 15:26:15 -0800 Subject: [PATCH 1/2] Find Font: the document's fonts are listed only while the dialog is open The dialog pump ran text.fonts and the missing-font check on every new version of the active document, with or without Find Font open, so each edit walked the whole document twice. On a 20,000-object document that was 2.2 ms and 23,443 allocations per edit frame. The pump now returns at once when the dialog is closed. Opening the dialog still lists the fonts, and an edit while it is open lists them again. --- crates/ui-egui/src/find_font.rs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/ui-egui/src/find_font.rs b/crates/ui-egui/src/find_font.rs index e2f384cc95..170c9df21e 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,28 @@ 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); + let opened_lists = FONT_LISTS.get(); + assert_eq!(opened_lists, 1, "opening lists the fonts once"); + app.session.execute("shape.rectangle", &json!({"x": 60, "y": 0, "width": 5, "height": 5})).unwrap(); + pump_now(&mut app); + pump_now(&mut app); + assert_eq!(FONT_LISTS.get(), opened_lists + 1, "an edit lists the fonts again once while the dialog is open"); + } + /// 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. From a1cdf0668a50c16f7c47357352d99592bdcd5dce Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 16:08:59 -0800 Subject: [PATCH 2/2] Find Font: repeat the pump test's edit when the font generation changes Another test thread that changes the font database between the two pump passes makes the second pass list the fonts again, so the one-listing assertion could fail. The test now repeats the edit until the generation stays the same across both passes, as kept_over_frames does. --- crates/ui-egui/src/find_font.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/find_font.rs b/crates/ui-egui/src/find_font.rs index 170c9df21e..65e2a030d2 100644 --- a/crates/ui-egui/src/find_font.rs +++ b/crates/ui-egui/src/find_font.rs @@ -687,12 +687,21 @@ mod tests { } assert_eq!(FONT_LISTS.get(), 0, "fonts listed with the dialog closed"); open(&mut app); - let opened_lists = FONT_LISTS.get(); - assert_eq!(opened_lists, 1, "opening lists the fonts once"); - app.session.execute("shape.rectangle", &json!({"x": 60, "y": 0, "width": 5, "height": 5})).unwrap(); - pump_now(&mut app); - pump_now(&mut app); - assert_eq!(FONT_LISTS.get(), opened_lists + 1, "an edit lists the fonts again once while the dialog is open"); + 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