From 8401672c4c24ff7159d63510f146891c104c7ee8 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 27 Nov 2026 00:28:34 +0000 Subject: [PATCH] perf(ui-contract): menu titles share their held commands, and facts are read in place A menu title kept its held commands in a Vec, and commands::holds copied every command into a new Arc on each run of the title. MenuTitle keeps an Arc<[CommandRef]> and holds() shares it. Facts::of_last_pass cloned last pass's facts for every read; Facts::with_last_pass lends them to the reader instead, so the menu bar's is_open and is_focused answer without a copy. --- crates/menu/src/bar.rs | 2 +- .../ui-contract/src/runtime/commands/mod.rs | 35 +++++++++++-------- crates/ui-contract/src/runtime/memory.rs | 7 ++-- crates/ui-contract/src/testing/memory.rs | 2 +- crates/ui-contract/tests/it/runtime/shown.rs | 2 +- crates/ui-patterns/tests/it/panels/palette.rs | 3 +- crates/ui-widgets/src/menu/bar.rs | 13 +++---- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/crates/menu/src/bar.rs b/crates/menu/src/bar.rs index 3d1204ce5e..a852119d72 100644 --- a/crates/menu/src/bar.rs +++ b/crates/menu/src/bar.rs @@ -202,7 +202,7 @@ fn show_bar(props: &mut BarProps<'_>, ui: &mut Ui) -> MenuBarOutput { .zip(&labels.menus) .map(|((menu, title), level)| { // The commands the title holds, for the palette's callout; a built-in has no command of the app. - let held = menu.children.iter().flat_map(node_commands).filter(|command| Builtin::of(command).is_none()).collect(); + let held = menu.children.iter().flat_map(node_commands).filter(|command| Builtin::of(command).is_none()).collect::>(); let row = MenuTitle::new(title.label.clone(), move |ui| shared.show(ui, &menu.children, level)).holds(held); match title.hidden { Some(letter) => row.mnemonic(letter), diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index 9167d09123..f95bf0bde8 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -814,9 +814,9 @@ fn register_shown(ctx: &Context, layer: LayerKey, rect: egui::Rect, label: Label /// Records that a container at `rect` holds `commands`, for this pass and viewport. Not [`CommandInput::holds`], which /// turns hold bindings on. -pub fn holds(ctx: &Context, layer: LayerKey, rect: egui::Rect, commands: &[CommandRef], label: Label) { +pub fn holds(ctx: &Context, layer: LayerKey, rect: egui::Rect, commands: &Arc<[CommandRef]>, label: Label) { if !commands.is_empty() { - register_shown(ctx, layer, rect, label, ShownCommands::Held(commands.into())); + register_shown(ctx, layer, rect, label, ShownCommands::Held(Arc::clone(commands))); } } @@ -832,7 +832,7 @@ pub(crate) fn note_shown(ctx: &Context, layer: LayerKey, rect: egui::Rect, label #[cfg(feature = "testing")] #[doc(hidden)] pub fn shown_registrations(ctx: &Context) -> usize { - Facts::::of_last_pass(ctx).len() + Facts::::with_last_pass(ctx, <[ShownFact]>::len) } /// The commands the bound components of the running viewport's last pass showed, in registration order: the values @@ -840,13 +840,15 @@ pub fn shown_registrations(ctx: &Context) -> usize { #[cfg(feature = "testing")] #[doc(hidden)] pub fn shown_commands(ctx: &Context) -> Vec> { - Facts::::of_last_pass(ctx) - .into_iter() - .filter_map(|fact| match fact.commands { - ShownCommands::Bound(command) => Some(command), - ShownCommands::Held(_) => None, - }) - .collect() + Facts::::with_last_pass(ctx, |facts| { + facts + .iter() + .filter_map(|fact| match &fact.commands { + ShownCommands::Bound(command) => Some(Arc::clone(command)), + ShownCommands::Held(_) => None, + }) + .collect() + }) } /// Where `command` was shown in the last pass: a bound component's clipped rect or a holder's rect. `None` unless it @@ -854,11 +856,14 @@ pub fn shown_commands(ctx: &Context) -> Vec> { pub fn shown(ctx: &Context, command: &CommandRef) -> Option { let viewport = pass::current_viewport(ctx); let scrim = overlay::top_modal_in(ctx, viewport).and_then(|top| top.cover); - Facts::::of_last_pass(ctx) - .into_iter() - .filter(|fact| fact.shows(command)) - .find(|fact| !areas::rects_over(ctx, fact.layer, scrim).iter().any(|over| over.intersects(fact.rect))) - .map(|fact| Shown { rect: fact.rect, viewport, label: fact.label, holder: matches!(fact.commands, ShownCommands::Held(_)) }) + // The rects over a fact are read outside the facts' record, which `with_last_pass` holds. + let showing: Vec = Facts::::with_last_pass(ctx, |facts| facts.iter().filter(|fact| fact.shows(command)).cloned().collect()); + showing.into_iter().find(|fact| !areas::rects_over(ctx, fact.layer, scrim).iter().any(|over| over.intersects(fact.rect))).map(|fact| Shown { + rect: fact.rect, + viewport, + label: fact.label, + holder: matches!(fact.commands, ShownCommands::Held(_)), + }) } /// Where a command was shown, from [`shown`]. diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index c74fc6a036..a5fcd8cfaa 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -949,10 +949,11 @@ impl Facts { Self::with_record(ctx, |record| record.this.push(fact)); } - /// The facts the running viewport's previous pass registered, in registration order. + /// `f` on the facts the running viewport's previous pass registered, in registration order. `f` reaches no + /// service: the record is held while it runs. #[doc(hidden)] - pub fn of_last_pass(ctx: &Context) -> Vec { - Self::with_record(ctx, |record| record.last.clone()) + pub fn with_last_pass(ctx: &Context, f: impl FnOnce(&[T]) -> R) -> R { + Self::with_record(ctx, |record| f(&record.last)) } fn with_record(ctx: &Context, f: impl FnOnce(&mut FactRecord) -> R) -> R { diff --git a/crates/ui-contract/src/testing/memory.rs b/crates/ui-contract/src/testing/memory.rs index 5f029eb593..e2f9745b87 100644 --- a/crates/ui-contract/src/testing/memory.rs +++ b/crates/ui-contract/src/testing/memory.rs @@ -95,7 +95,7 @@ pub fn register_fact(ctx: &Context) -> Vec { - Facts::of_last_pass(ctx) + Facts::with_last_pass(ctx, <[T]>::to_vec) } /// Runs `f` on a child `Ui` of `ui`. diff --git a/crates/ui-contract/tests/it/runtime/shown.rs b/crates/ui-contract/tests/it/runtime/shown.rs index c0f3939f25..835a7639b2 100644 --- a/crates/ui-contract/tests/it/runtime/shown.rs +++ b/crates/ui-contract/tests/it/runtime/shown.rs @@ -16,7 +16,7 @@ use std::sync::Arc; #[trial] fn a_container_registers_its_commands_once() { let held: Vec = (0..100).map(|i| CommandRef::new(format!("test.held{i}"))).collect(); - let shown = held.clone(); + let shown: Arc<[CommandRef]> = held.clone().into(); let mut fixture = Fixture::new(move |ui| { let rect = egui::Rect::from_min_size(ui.max_rect().min, egui::vec2(40.0, 20.0)); commands::holds(ui.ctx(), ui.layer_id(), rect, &shown, Label::from("Image")); diff --git a/crates/ui-patterns/tests/it/panels/palette.rs b/crates/ui-patterns/tests/it/panels/palette.rs index ecfd5ea24a..4be3f59a23 100644 --- a/crates/ui-patterns/tests/it/panels/palette.rs +++ b/crates/ui-patterns/tests/it/panels/palette.rs @@ -14,6 +14,7 @@ use craft_ui::testing::{Fixture, Registry, trial}; use egui::{Key, Modifiers, Pos2, Ui, Vec2}; use std::cell::{Cell, RefCell}; use std::rc::Rc; +use std::sync::Arc; /// App commands with the default modal rule: blocked while a dialog is open. #[derive(Clone, Copy, Debug, PartialEq, Eq, CommandSet)] @@ -488,7 +489,7 @@ fn the_active_row_marks_its_shown_control_and_a_hidden_home_goes_to_the_footer() // Flip is held by a rect in the corner, the way a menu title holds its commands; Rotate has a bound button. let mut rig = rig(entries, Some(vec![TrialCommand::Flip.into()]), false, |ui| { let corner = egui::Rect::from_min_size(ui.max_rect().min, Vec2::new(60.0, 20.0)); - commands::holds(ui.ctx(), ui.layer_id(), corner, &[TrialCommand::Flip.into()], Label::from("Rail slot")); + commands::holds(ui.ctx(), ui.layer_id(), corner, &Arc::from([CommandRef::from(TrialCommand::Flip)]), Label::from("Rail slot")); ui.add_space(24.0); Button::command(TrialCommand::Rotate).show(ui); }); diff --git a/crates/ui-widgets/src/menu/bar.rs b/crates/ui-widgets/src/menu/bar.rs index 39d3fded6c..1348430404 100644 --- a/crates/ui-widgets/src/menu/bar.rs +++ b/crates/ui-widgets/src/menu/bar.rs @@ -45,6 +45,7 @@ use craft_host::target::MenuPlacement; use craft_keymap::mnemonic; use craft_theme::Flags; use egui::{Context, Pos2, Rect, Response, Ui, Vec2}; +use std::sync::Arc; const TITLE: PartKey = PartKey::new("title"); const MORE: PartKey = PartKey::new("more"); @@ -207,7 +208,7 @@ type TitleText = (String, Option<(char, Option)>); pub struct MenuTitle<'a> { label: Label, body: Box, - holds: Vec, + holds: Arc<[CommandRef]>, /// The letter that opens the menu in place of the bar's own pick. mnemonic: Option, } @@ -215,7 +216,7 @@ pub struct MenuTitle<'a> { impl<'a> MenuTitle<'a> { /// A menu titled `label` (a `&` marks the mnemonic) whose rows `body` shows. pub fn new(label: impl Into