From d63d6f4db1e61c06af2bf170a0a6ab0088326c20 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 15:26:15 -0800 Subject: [PATCH] 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.