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.
This commit is contained in:
dotCooCoo committed 2026-11-28 15:26:15 -08:00
1 parent 10f8411ac0
commit d63d6f4db1
1 file changed
+34
+34
View File
@@ -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<usize> = const { std::cell::Cell::new(0) };
}
fn fonts(app: &mut VectorcraftApp) -> Vec<Value> {
#[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::<FindFontDialog>().is_some()) {
return;
}
let (used, offer_folder) = current_fonts(app);
let taken = dialogs.with(|host| {
let d = host.get_mut::<FindFontDialog>()?;
@@ -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.