mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
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.
This commit is contained in:
1 parent
090a789c3b
commit
8401672c4c
7 files changed
+36
-28
No files matched your search
@@ -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::<Vec<_>>();
|
||||
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),
|
||||
|
||||
@@ -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::<ShownFact>::of_last_pass(ctx).len()
|
||||
Facts::<ShownFact>::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<Arc<CommandRef>> {
|
||||
Facts::<ShownFact>::of_last_pass(ctx)
|
||||
.into_iter()
|
||||
.filter_map(|fact| match fact.commands {
|
||||
ShownCommands::Bound(command) => Some(command),
|
||||
ShownCommands::Held(_) => None,
|
||||
})
|
||||
.collect()
|
||||
Facts::<ShownFact>::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<Arc<CommandRef>> {
|
||||
pub fn shown(ctx: &Context, command: &CommandRef) -> Option<Shown> {
|
||||
let viewport = pass::current_viewport(ctx);
|
||||
let scrim = overlay::top_modal_in(ctx, viewport).and_then(|top| top.cover);
|
||||
Facts::<ShownFact>::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<ShownFact> = Facts::<ShownFact>::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`].
|
||||
|
||||
@@ -949,10 +949,11 @@ impl<T: Clone + Send + Sync + 'static> Facts<T> {
|
||||
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<T> {
|
||||
Self::with_record(ctx, |record| record.last.clone())
|
||||
pub fn with_last_pass<R>(ctx: &Context, f: impl FnOnce(&[T]) -> R) -> R {
|
||||
Self::with_record(ctx, |record| f(&record.last))
|
||||
}
|
||||
|
||||
fn with_record<R>(ctx: &Context, f: impl FnOnce(&mut FactRecord<T>) -> R) -> R {
|
||||
|
||||
@@ -95,7 +95,7 @@ pub fn register_fact<T: Clone + PartialEq + Debug + crate::runtime::Shift + Send
|
||||
|
||||
/// The facts the running viewport's previous pass registered.
|
||||
pub fn facts_of_last_pass<T: Clone + Send + Sync + 'static>(ctx: &Context) -> Vec<T> {
|
||||
Facts::of_last_pass(ctx)
|
||||
Facts::with_last_pass(ctx, <[T]>::to_vec)
|
||||
}
|
||||
|
||||
/// Runs `f` on a child `Ui` of `ui`.
|
||||
|
||||
@@ -16,7 +16,7 @@ use std::sync::Arc;
|
||||
#[trial]
|
||||
fn a_container_registers_its_commands_once() {
|
||||
let held: Vec<CommandRef> = (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"));
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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<usize>)>);
|
||||
pub struct MenuTitle<'a> {
|
||||
label: Label,
|
||||
body: Box<dyn FnMut(&mut Ui) + 'a>,
|
||||
holds: Vec<CommandRef>,
|
||||
holds: Arc<[CommandRef]>,
|
||||
/// The letter that opens the menu in place of the bar's own pick.
|
||||
mnemonic: Option<char>,
|
||||
}
|
||||
@@ -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<Label>, body: impl FnMut(&mut Ui) + 'a) -> Self {
|
||||
MenuTitle { label: label.into(), body: Box::new(body), holds: Vec::new(), mnemonic: None }
|
||||
MenuTitle { label: label.into(), body: Box::new(body), holds: Arc::default(), mnemonic: None }
|
||||
}
|
||||
|
||||
/// The letter that opens the menu, in place of the bar's own pick. A letter the title lacks still opens it, with
|
||||
@@ -228,8 +229,8 @@ impl<'a> MenuTitle<'a> {
|
||||
|
||||
/// Records the title's rect for `commands` with `commands::holds` in every pass it is shown.
|
||||
#[must_use]
|
||||
pub fn holds(mut self, commands: Vec<CommandRef>) -> Self {
|
||||
self.holds = commands;
|
||||
pub fn holds(mut self, commands: impl Into<Arc<[CommandRef]>>) -> Self {
|
||||
self.holds = commands.into();
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -314,12 +315,12 @@ struct BarFact {
|
||||
/// Whether a menu of a bar is open. The app leaves the keys to it meanwhile: its shortcuts wait until the menu
|
||||
/// closes.
|
||||
pub fn is_open(ctx: &Context) -> bool {
|
||||
Facts::<BarFact>::of_last_pass(ctx).iter().any(|fact| fact.open)
|
||||
Facts::<BarFact>::with_last_pass(ctx, |facts| facts.iter().any(|fact| fact.open))
|
||||
}
|
||||
|
||||
/// Whether the keyboard is on a menu bar's titles.
|
||||
pub fn is_focused(ctx: &Context) -> bool {
|
||||
Facts::<BarFact>::of_last_pass(ctx).iter().any(|fact| fact.focused)
|
||||
Facts::<BarFact>::with_last_pass(ctx, |facts| facts.iter().any(|fact| fact.focused))
|
||||
}
|
||||
|
||||
/// The titles a bar shows in a given room.
|
||||
|
||||
Reference in new issue
Block a user