From ae0adc1fff76729f0d520239051a94b84c0bba1e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 12:12:55 -0800 Subject: [PATCH 1/2] Menus: keep the shortcut catalog when a Scripts read leaves its rows as they were The catalog's generation hashed the Scripts menu's revision and the font database's generation. Each startup step that moved either one (the Scripts note before the first listing, the listing itself, the font scan) made the catalog's rows again and built the window's keymap again, even when the rows were the same. On a fresh start that happened three times in the first frames. The catalog is now kept when its rows are unchanged: the rows the scripts and fonts add are compared as rows, and the catalog's generation hashes each row's key, label and kind. A menu that shows or hides an item the rows already have no longer moves the generation either. On Windows (release build, fresh settings folder, 3 runs), the UI thread's work in frames 0 to 2 went from a median of 226 ms to 130 ms, and catalog and keymap builds in the first 6 seconds went from 4 to 2. --- crates/ui-egui/src/menus/model.rs | 53 ++++++++++++++++++++++++++++-- crates/ui-egui/src/scripts_menu.rs | 6 ++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index 56b6d2ce80..e4b09fc43f 100644 --- a/crates/ui-egui/src/menus/model.rs +++ b/crates/ui-egui/src/menus/model.rs @@ -310,14 +310,27 @@ fn role(title: &str) -> MenuRole { /// The hash of what the catalog's rows depend on, which moves its generation: `plays` are the /// actions' function keys, a row each, `type_keys` the chords the Type tool's commands take and `tools` the set the -/// tool rows' icons come from. +/// tool rows' icons come from. The rows the scripts and the fonts add are compared as rows ([`CatalogInputs`]) and +/// hashed into the catalog's generation as rows ([`rows_generation`]), so a new listing or font scan that leaves them +/// as they were keeps the catalog and the window's keymap. fn generation(listed: &Listed, plays: &[(String, craft::keymap::Chord)], type_keys: &[(&str, Vec)], tools: ToolIcons) -> u64 { let mut h = DefaultHasher::new(); - (crate::i18n::current().code(), plugin_revision(), crate::scripts_menu::revision(), vectorcraft_text::FontDb::global().generation()).hash(&mut h); + (crate::i18n::current().code(), plugin_revision()).hash(&mut h); (listed, plays, type_keys, tools).hash(&mut h); h.finish() } +/// The catalog's generation: `base` ([`generation`]) with each row's key, label and kind, so it moves when a row comes, +/// goes or is renamed, and not when a menu shows or hides an item the rows already had. +fn rows_generation(base: u64, defs: &[CommandDef]) -> u64 { + let mut h = DefaultHasher::new(); + base.hash(&mut h); + for def in defs { + (def.command.key(), &def.label, &def.kind).hash(&mut h); + } + h.finish() +} + /// Hashes the names of the saved workspaces and the custom toolbars, which the Window menu lists as rows. fn hash_listed_names(ui: &UiState, h: &mut DefaultHasher) { for w in &ui.workspaces.set.saved { @@ -476,7 +489,8 @@ fn catalog_defs(inputs: &CatalogInputs) -> (Vec, HashSet) { fn build_catalog(app: &VectorcraftApp, listed: &Listed, plays: &[(String, craft::keymap::Chord)], defs: Vec) -> Option { let type_keys = crate::type_editing::app_keys(&defs, &app.shortcut_overrides); let (defs, linked_keys) = without_linked(defs); - match AppCatalog::new(defs, generation(listed, plays, &type_keys, ToolIcons::of(&app.session.prefs))) { + let generation = rows_generation(generation(listed, plays, &type_keys, ToolIcons::of(&app.session.prefs)), &defs); + match AppCatalog::new(defs, generation) { Ok(catalog) => { // While a text field has the keys no shortcut runs, as in production; the text-editing chords still edit it, // and the Type tool's commands run while it types. @@ -805,6 +819,39 @@ mod tests { assert!(app.menus.states.get(&CommandRef::new("object.group")).is_some_and(CommandState::is_enabled)); } + /// A new Scripts revision that leaves the menu's rows as they were (the note before the first listing, or a read + /// that finds the same scripts) lays the menus out again and keeps the catalog, so the window's keymap is not + /// built again. The fonts and plug-ins are held steady against tests running alongside, and the steps run again, + /// at most 20 times, until the shortcut editor's snapshot (which a test switching languages replaces) held still + /// through them. + #[test] + fn a_scripts_revision_with_the_same_rows_keeps_the_catalog() { + let fonts = vectorcraft_text::FontDb::global(); + let _steady = (vectorcraft_text::test_fonts::steady_fonts(fonts), vectorcraft_plugins::steady::steady_plugins()); + let snapshot = snapshot_addr; + for _ in 0..20 { + let (mut app, ctx) = app_with_document(); + refresh(&mut app, &ctx); + let before = snapshot(); + let (rebuilds, builds, generation) = (app.menus.rebuilds, CATALOG_BUILDS.get(), app.menus.catalog_generation()); + crate::scripts_menu::bump_revision(); + refresh(&mut app, &ctx); + if snapshot() != before { + continue; + } + assert_eq!(app.menus.rebuilds, rebuilds + 1, "the new revision lays the menus out again"); + assert_eq!(CATALOG_BUILDS.get(), builds, "the catalog's rows are not made again"); + assert_eq!(app.menus.catalog_generation(), generation, "the catalog, which the keymap is built from, stays"); + return; + } + panic!("the shortcut editor's snapshot changed during each of 20 tries"); + } + + /// The address of the shortcut editor's current snapshot. + fn snapshot_addr() -> usize { + std::sync::Arc::as_ptr(&crate::shortcut_editor::snapshot()).addr() + } + /// A new window keymap with nothing else changed (the framework builds the first one after the first frame's /// menus) sets the panel rows' shortcuts from it, in the bar and in the laid-out bar, without building the menus /// again. The menus are also built again when a process-wide catalog changes, so the fonts and plug-ins are held diff --git a/crates/ui-egui/src/scripts_menu.rs b/crates/ui-egui/src/scripts_menu.rs index e8b1f229d9..db6cc9ca71 100644 --- a/crates/ui-egui/src/scripts_menu.rs +++ b/crates/ui-egui/src/scripts_menu.rs @@ -155,6 +155,12 @@ pub fn revision() -> u64 { state.revision * 2 + u64::from(state.note_at(now()) == Some(SLOW)) } +/// Moves [`revision`] on this test's thread with the listing left as it was, as a read that finds the same scripts does. +#[cfg(test)] +pub(crate) fn bump_revision() { + state().revision += 1; +} + /// Read the Scripts folder again, unless the session has no folder (the web, and a run without /// preferences) or the context has no host. A read asked for while one runs waits for it (the /// queue runs one job per folder). `ctx` is asked to draw when the read ends. From f6255a1d8aceb967bfd498051180a1116ec8e5ef Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 13:10:35 -0800 Subject: [PATCH 2/2] Menus: start the Scripts revision test from the app's own snapshot The test compared the shared shortcut editor snapshot before and after the steps. Another test switching languages can replace that snapshot after the app's catalog was built, so the refresh rebuilt the catalog while the guard saw no change and the assertion failed. The guard now starts from the snapshot the app's catalog holds. --- crates/ui-egui/src/menus/model.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index e4b09fc43f..460b394b5c 100644 --- a/crates/ui-egui/src/menus/model.rs +++ b/crates/ui-egui/src/menus/model.rs @@ -823,7 +823,7 @@ mod tests { /// that finds the same scripts) lays the menus out again and keeps the catalog, so the window's keymap is not /// built again. The fonts and plug-ins are held steady against tests running alongside, and the steps run again, /// at most 20 times, until the shortcut editor's snapshot (which a test switching languages replaces) held still - /// through them. + /// through them, starting from the snapshot the app's catalog was built from. #[test] fn a_scripts_revision_with_the_same_rows_keeps_the_catalog() { let fonts = vectorcraft_text::FontDb::global(); @@ -832,7 +832,11 @@ mod tests { for _ in 0..20 { let (mut app, ctx) = app_with_document(); refresh(&mut app, &ctx); - let before = snapshot(); + // The snapshot the app's catalog was built from, not the shared one, which another thread may have + // replaced since. + let Some(before) = app.menus.rows.as_ref().map(|r| std::sync::Arc::as_ptr(&r.inputs.snapshot).addr()) else { + panic!("the first refresh made the catalog's rows"); + }; let (rebuilds, builds, generation) = (app.menus.rebuilds, CATALOG_BUILDS.get(), app.menus.catalog_generation()); crate::scripts_menu::bump_revision(); refresh(&mut app, &ctx);