diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 2e34e36dc6..7e3caa1a43 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -25,6 +25,8 @@ pub struct PropertiesUi { /// The cache of `appearance_section::edited_strokes_profiled`, keyed by (document uid, revision, /// active Appearance row). pub edited_profiled: Option<(ProfiledKey, bool)>, + /// The Quick Actions last listed, with the [`quick_actions_key`] they were listed at. + quick_actions: Option<(u64, Vec)>, } /// The key of [`PropertiesUi::edited_profiled`]: (document uid, revision, active Appearance row). @@ -34,6 +36,7 @@ type ProfiledKey = (u64, u64, Option); const HALVES: [ColumnWidth; 2] = [ColumnWidth::fill(1.0); 2]; /// One Quick Actions button: its label and the command it runs. +#[derive(Clone, Debug)] pub(crate) struct QuickAction { /// The button's label. pub label: &'static str, @@ -169,12 +172,42 @@ fn quick_action_buttons(app: &mut VectorcraftApp, ui: &mut Ui) { } } } - for action in quick_actions(app) { + for action in shown_quick_actions(app) { Button::command(action.command).label(action.label).show(ui); } }); } +/// What the Quick Actions are listed from: the menus' state ([`crate::menus::model::state_key`]), which +/// moves with every command run, the document and its revision, the tool and isolation mode. +fn quick_actions_key(app: &VectorcraftApp) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + crate::menus::model::state_key(app, None).hash(&mut h); + app.session.tool_id().hash(&mut h); + app.session.active().map(|st| (st.uid, st.revision, st.isolation)).hash(&mut h); + h.finish() +} + +#[cfg(test)] +thread_local! { + /// How many times this thread has listed the Quick Actions. + pub(crate) static QUICK_ACTION_LISTS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// [`quick_actions`], listed again only when [`quick_actions_key`] changed. +fn shown_quick_actions(app: &mut VectorcraftApp) -> Vec { + let key = quick_actions_key(app); + if let Some((kept, actions)) = &app.ui.properties_panel.quick_actions + && *kept == key + { + return actions.clone(); + } + let actions = quick_actions(app); + app.ui.properties_panel.quick_actions = Some((key, actions.clone())); + actions +} + /// The ellipsis button that shows the panel `panel` for the section's further options. fn more_options(ui: &mut Ui, panel: &str, tip: &'static str) { IconButton::command(IconId::new("ellipsis"), CommandRef::with_params("window.panel", json!({ "panel": panel }))).label(tip).show(ui); @@ -183,6 +216,8 @@ fn more_options(ui: &mut Ui, panel: &str, tip: &'static str) { /// The Quick Actions the selection offers, in order. Lock and Isolate Group are left out while their commands are /// off (in isolation mode, while an opacity mask is edited). pub(crate) fn quick_actions(app: &mut VectorcraftApp) -> Vec { + #[cfg(test)] + QUICK_ACTION_LISTS.with(|n| n.set(n.get() + 1)); let n_sel = super::selection_len(app); let first = first_selected(app); let kind = first.as_deref().map(|n| &n.kind); diff --git a/crates/ui-egui/src/panels/properties/tests.rs b/crates/ui-egui/src/panels/properties/tests.rs index bb90289aef..4a8158364a 100644 --- a/crates/ui-egui/src/panels/properties/tests.rs +++ b/crates/ui-egui/src/panels/properties/tests.rs @@ -1084,6 +1084,37 @@ fn quick_actions_follow_the_selection() { } } +/// The panel lists the Quick Actions once per change of what they depend on, not in every frame. +#[test] +fn quick_actions_are_listed_again_only_after_a_change() { + let mut app = new_app(); + rect(&mut app, 10.0, 10.0, 40.0, 40.0); + rect(&mut app, 80.0, 10.0, 40.0, 40.0); + let ctx = crate::testing::ui_context(); + let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0))); + let mut time = 0.0; + let mut frame = |app: &mut VectorcraftApp| { + time += 0.05; + crate::testing::app_frame(app, &ctx, egui::RawInput { time: Some(time), screen_rect, ..Default::default() }); + }; + for _ in 0..3 { + frame(&mut app); + } + let lists = || QUICK_ACTION_LISTS.with(|n| n.get()); + let shown = |app: &VectorcraftApp| app.ui.properties_panel.quick_actions.as_ref().map(|(_, a)| a.iter().map(|a| a.command).collect::>()); + assert!(shown(&app).is_some_and(|a| !a.contains(&"object.group")), "one object selected: no Group"); + let before = lists(); + frame(&mut app); + frame(&mut app); + assert_eq!(lists() - before, 0, "frames with nothing changed list no Quick Actions"); + app.run("select.all", json!({})).unwrap(); + let before = lists(); + frame(&mut app); + frame(&mut app); + assert_eq!(lists() - before, 1, "a new selection lists them once"); + assert!(shown(&app).is_some_and(|a| a.contains(&"object.group")), "two objects selected: Group"); +} + /// A brushed stroke with a variable width: the Width Profile list lists only Uniform, which removes /// the profile. #[test] diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs index d90dbd96c0..1f1f6615fb 100644 --- a/crates/ui-egui/src/toolbar.rs +++ b/crates/ui-egui/src/toolbar.rs @@ -184,7 +184,15 @@ pub(crate) fn slots(app: &VectorcraftApp) -> Vec<(Option<&'static str>, Vec<&'st /// The slot a group of tools is in the rail's last-used memory: the index of its first tool in the /// catalog, which stays put while the user edits the toolbar. pub(crate) fn slot_id(first: &str) -> SlotId { - vectorcraft_tools::catalog::all_tools().position(|t| t.id == first).and_then(|i| SlotId::try_from(i).ok()).unwrap_or(SlotId::MAX) + static IDS: std::sync::OnceLock> = std::sync::OnceLock::new(); + let ids = IDS.get_or_init(|| { + let mut ids = std::collections::HashMap::new(); + for (i, tool) in vectorcraft_tools::catalog::all_tools().enumerate() { + ids.entry(tool.id).or_insert_with(|| SlotId::try_from(i).unwrap_or(SlotId::MAX)); + } + ids + }); + ids.get(first).copied().unwrap_or(SlotId::MAX) } /// A rail tool that selects `id`: the tool's command in the catalog, which gives the button its name, its icon and @@ -196,6 +204,8 @@ pub(crate) fn rail_tool(id: &str) -> RailTool { /// The rail's slots for `slots`: a category's first slot carries its caption, and its last ends the /// group. pub(crate) fn rail_slots(slots: &[(Option<&'static str>, Vec<&'static str>)]) -> Vec> { + #[cfg(test)] + SLOT_BUILDS.with(|n| n.set(n.get() + slots.len())); slots .iter() .enumerate() @@ -344,19 +354,54 @@ pub(crate) fn rail_view(app: &mut VectorcraftApp, ui: &mut Ui, owner: Owner, at: height } +/// The tool groups a rail's slots are built from, with the language their captions are in. +#[derive(Clone, Debug, PartialEq, Eq)] +struct SlotSource { + /// A strip's slots hold one tool each. + strip: bool, + /// The UI language. + lang: crate::i18n::Lang, + /// Each slot's caption and tools. + groups: Vec<(Option<&'static str>, Vec<&'static str>)>, +} + +impl SlotSource { + /// The rail's slots for these groups. + fn slots(&self) -> Vec> { + if !self.strip { + return rail_slots(&self.groups); + } + #[cfg(test)] + SLOT_BUILDS.with(|n| n.set(n.get() + self.groups.len())); + self.groups.iter().flat_map(|(_, tools)| tools).map(|tool| ToolSlot::new(slot_id(tool), vec![rail_tool(tool)])).collect() + } +} + +#[cfg(test)] +thread_local! { + /// How many rail slots this thread has built. + pub(crate) static SLOT_BUILDS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + /// What a rail reads across passes: its slots and the tools they showed last, each edited only when -/// it changed, so a pass where neither changed reads neither. +/// it changed, so a pass where neither changed reads neither. The slots are built again only when +/// the groups they come from change. #[derive(Default)] pub(crate) struct RailModel { + source: Option, slots: Tracked>>, last_used: Tracked>, } impl RailModel { - /// Brings the model up to `slots` and `last_used`. - fn refresh(&mut self, slots: Vec>, last_used: LastUsed) { - if *self.slots != slots { - self.slots.edit(|kept| *kept = slots); + /// Brings the model up to `source` and `last_used`. + fn refresh(&mut self, source: SlotSource, last_used: LastUsed) { + if self.source.as_ref() != Some(&source) { + let slots = source.slots(); + if *self.slots != slots { + self.slots.edit(|kept| *kept = slots); + } + self.source = Some(source); } if *self.last_used != last_used { self.last_used.edit(|kept| *kept = last_used); @@ -366,21 +411,22 @@ impl RailModel { /// [`rail_view`] over `model`, the rail's own. fn show_rail(app: &mut VectorcraftApp, ui: &mut Ui, owner: Owner, at: GroupAt, model: &mut RailModel) -> f32 { - let (slots, last_used) = match &owner { - Owner::Tools => (rail_slots(&slots(app)), app.ui.tools_last_used.clone()), + let lang = crate::i18n::current(); + let (source, last_used) = match &owner { + Owner::Tools => (SlotSource { strip: false, lang, groups: slots(app) }, app.ui.tools_last_used.clone()), Owner::Toolbar(id) => { let Some(bar) = bar_of(&app.ui, *id) else { return 0.0 }; - let groups: Vec<_> = groups_of(app, &owner).into_iter().map(|g| (None, g)).collect(); - (rail_slots(&groups), bar.last_used.clone()) + let groups = groups_of(app, &owner).into_iter().map(|g| (None, g)).collect(); + (SlotSource { strip: false, lang, groups }, bar.last_used.clone()) } // One slot per tool, so a strip has no flyouts. A group no longer in the toolbar draws // nothing; `keep_strips` closes its panel. Owner::Strip(parent, first) => { - let tools = strip_tools(app, parent, first); - (tools.iter().map(|tool| ToolSlot::new(slot_id(tool), vec![rail_tool(tool)])).collect(), Default::default()) + let groups = vec![(None, strip_tools(app, parent, first))]; + (SlotSource { strip: true, lang, groups }, Default::default()) } }; - model.refresh(slots, last_used); + model.refresh(source, last_used); let current = app.session.tool_id().to_string(); // PLAN: the sheet's `Cards<'a> { app }` cannot be held beside the footer's `&mut app`; Cards copies the two prefs. let cards = card::Cards::new(app); diff --git a/crates/ui-egui/src/toolbar/tests.rs b/crates/ui-egui/src/toolbar/tests.rs index 6e2eeea570..a6dc944317 100644 --- a/crates/ui-egui/src/toolbar/tests.rs +++ b/crates/ui-egui/src/toolbar/tests.rs @@ -473,3 +473,24 @@ fn a_tool_card_shows_what_the_tool_does_and_what_its_keys_do() { app.run("prefs.set", json!({"key": "showToolTips", "value": false})).unwrap(); assert!(!card::Cards::new(&app).has(&"selection".to_string()), "tool tips off: no card"); } + +#[test] +fn the_rail_builds_its_slots_only_when_its_tool_groups_change() { + let mut app = app_with(300, 300); + let ctx = reader(); + settled(&mut app, &ctx, 0.0, 800.0); + let built = || SLOT_BUILDS.with(|n| n.get()); + let before = built(); + frame(&mut app, &ctx, 0.2, vec![], 800.0); + frame(&mut app, &ctx, 0.25, vec![], 800.0); + assert_eq!(built() - before, 0, "frames with the same tool groups build no slots"); + let basic = rail_slots(&slots(&app)).len(); + app.run("window.toolbarAdvanced", json!({"advanced": true})).unwrap(); + let before = built(); + frame(&mut app, &ctx, 0.3, vec![], 800.0); + let advanced = slots(&app).len(); + assert_ne!(advanced, basic); + assert_eq!(built() - before, advanced, "the Advanced toolbar's slots are built once"); + let kept = app.dock_rt.rail_models.get(&Owner::Tools.panel()).map(|model| model.slots.len()); + assert_eq!(kept, Some(advanced), "the rail shows the Advanced toolbar's slots"); +}