diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index 56b6d2ce80..460b394b5c 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,43 @@ 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, 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(); + 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); + // 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); + 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.