From 8e31fb06de54a46257000024823e59f1363efdf3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 13:47:59 -0800 Subject: [PATCH 1/2] Properties: idle frames replay the Document sections' bodies With nothing selected, the Properties panel laid out the bodies of its Document, Rulers & Grids, Guides, Snap Options, Preferences and Quick Actions sections again in every frame. Each body now shows in a MemoView keyed on what it reads: the units, the artboard count, the snap settings, the keyboard increment and its unit, Scale Strokes & Effects and the interface language. A change in the command state of one of its buttons runs it again. The Units choice, the snap checkboxes and the preference edits come back through an Outbox and run after the section, as before. The section headers and the Appearance section still run in every frame. The new test counts the body runs: none over 10 idle frames, and runs again after a command changes what one shows. --- .../panels/properties/document_sections.rs | 311 ++++++++++++++---- 1 file changed, 251 insertions(+), 60 deletions(-) diff --git a/crates/ui-egui/src/panels/properties/document_sections.rs b/crates/ui-egui/src/panels/properties/document_sections.rs index e8c14ba4dd..4774bdb750 100644 --- a/crates/ui-egui/src/panels/properties/document_sections.rs +++ b/crates/ui-egui/src/panels/properties/document_sections.rs @@ -1,10 +1,10 @@ //! The Properties panel's Document sections, shown with nothing selected. -use craft::behavior::IconId; +use craft::behavior::{EditEvent, IconId}; use craft::keymap::CommandRef; use craft::ui::buttons::{Button, ToggleButton}; use craft::ui::choice::Checkbox; -use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, Sizing}; +use craft::ui::contract::{Bind, Entry, MemoProps, MemoView, Outbox, Props as _, Show as _, ShowContainer as _, Sizing}; use craft::ui::form::Field; use craft::ui::layout_components::{Grid, Row}; use craft::ui::pickers::Select; @@ -17,84 +17,212 @@ use vectorcraft_doc::Unit; use super::artboard_sections::rearrange_button; use super::object_sections::HALVES; use crate::VectorcraftApp; +use crate::edits::EditPath; use crate::panels::transform; /// The panel with nothing selected: the document's units, Document Setup and artboards, the /// Appearance fill and stroke, the view's rulers, grids, guides and snapping, the keyboard /// increment and Scale Strokes & Effects, and Quick Actions. +/// +/// The body of every section but Appearance shows in a `MemoView` keyed on what it reads, so a +/// frame that changes none of it replays the body. What a body picks comes back through an `Outbox` +/// and runs after its section. pub(super) fn document_sections(app: &mut VectorcraftApp, ui: &mut Ui) { + let lang = crate::i18n::current(); let units = app.session.general_unit(); + let unit = Unit::ALL.iter().position(|u| *u == units).unwrap_or(usize::MAX); let artboards = app.session.active().map_or(0, |d| d.doc.artboards.len()); + let mut picked_units = Vec::new(); super::section(tl!("Document")).show(ui, |ui, _| { - Field::new(tl!("Units")).show(ui, |ui, _| { - let entries: Vec> = Unit::ALL.iter().map(|u| Entry::new(*u, u.label())).collect(); - let out = Select::new(tl!("Units"), Bind::given(units), &entries).show(ui); - if let Some(u) = out.events.committed() { - app.run("document.setUnits", json!({ "units": u.label() })).ok(); - } - }); - Grid::new(&HALVES).show(ui, |ui, _| { - Button::command(CommandRef::new("file.documentSetup")).label(tl!("Document Setup")).sizing(Sizing::FillWidth).show(ui); - Button::command(CommandRef::with_params("tool.select", json!({ "tool": "artboard" }))) - .label(tl!("Edit Artboards")) - .sizing(Sizing::FillWidth) - .show(ui); - if artboards > 1 { - rearrange_button(ui, artboards); - } - }); + let document = DocumentProps { unit, units, artboards, lang, picked: Outbox::new(&mut picked_units) }; + let _ = MemoView::new("properties-document", document, document_body).show(ui); }); + if let Some(u) = picked_units.pop() { + app.run("document.setUnits", json!({ "units": u.label() })).ok(); + } super::section(tl!("Appearance")).show(ui, |ui, _| super::appearance_section::fill_stroke_rows(app, ui)); super::section(tl!("Rulers & Grids")).show(ui, |ui, _| { - Row::new().show(ui, |ui, _| { - // The checked and enabled states and the tooltips come from the commands (Trim View turns the grid off). - for (command, icon) in [("view.rulers", "ruler"), ("view.grid", "grid-3x3"), ("view.transparencyGrid", "square-dashed")] { - ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui); - } - }); + let _ = MemoView::new("properties-rulers", Language { lang }, rulers_body).show(ui); }); super::section(tl!("Guides")).show(ui, |ui, _| { - Row::new().show(ui, |ui, _| { - for (command, icon) in [("view.guides", "layout-grid"), ("view.smartGuides", "sparkles")] { - ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui); - } - }); + let _ = MemoView::new("properties-guides", Language { lang }, guides_body).show(ui); }); + let mut snaps = Vec::new(); + let (point, grid) = (app.ui.view.snap_to_point, app.ui.view.snap_to_grid); super::section(tl!("Snap Options")).show(ui, |ui, _| { - let (point, grid) = (app.ui.view.snap_to_point, app.ui.view.snap_to_grid); - for (command, label, on) in [("view.snapToPoint", tl!("Snap to Point"), point), ("view.snapToGrid", tl!("Snap to Grid"), grid)] { - if Checkbox::new(Bind::given(on)).label(label).show(ui).value().is_some() { - app.run(command, json!({})).ok(); - } - } + let snap = SnapProps { point, grid, lang, picked: Outbox::new(&mut snaps) }; + let _ = MemoView::new("properties-snap", snap, snap_body).show(ui); }); + for command in snaps { + app.run(command, json!({})).ok(); + } + let mut prefs = Vec::new(); + let (increment, scale) = (app.session.prefs.keyboard_increment, app.session.prefs.scale_strokes); + let (bare_points, lengths) = (app.session.prefs.numbers_without_units_are_points, crate::canvas::rulers::general_lengths(app)); super::section(tl!("Preferences")).show(ui, |ui, _| { - Field::new(tl!("Keyboard Increment")).show(ui, |ui, _| { - // PLAN: assumed `canvas::rulers::general_lengths(app) -> LengthUnits` (cv.B5b) - let lengths: LengthUnits = crate::canvas::rulers::general_lengths(app); - // The engine's own range for the preference (`prefscmds.rs`): a larger value is an error. - let out = NumberField::length(tl!("Keyboard Increment"), Bind::given(Some(app.session.prefs.keyboard_increment)), lengths) - .range(0.001..=1296.0) - .show(ui); - crate::edits::apply_edits(app, &out.edit, crate::edits::EditPath::Scope, |app, v: &Option, _| { - if let Some(value) = *v { - app.run("prefs.set", json!({ "key": "keyboardIncrement", "value": value })).ok(); - } - }); - }); - let scale = app.session.prefs.scale_strokes; - let out = Checkbox::new(Bind::given(scale)).label(tl!("Scale Strokes & Effects")).show(ui); - if let Some(on) = out.value().copied() { - transform::set_pref(app, "scaleStrokes", on); + let picked = Outbox::new(&mut prefs); + let preferences = PreferencesProps { increment, unit, bare_points, lengths, scale, lang, picked }; + let _ = MemoView::new("properties-preferences", preferences, preferences_body).show(ui); + }); + for pick in prefs { + match pick { + PreferencePick::Increment(event) => { + crate::edits::apply_event(app, &event, &EditPath::Scope, &mut |app, v: &Option, _| { + if let Some(value) = *v { + app.run("prefs.set", json!({ "key": "keyboardIncrement", "value": value })).ok(); + } + }); + } + PreferencePick::ScaleStrokes(on) => transform::set_pref(app, "scaleStrokes", on), + } + } + super::section(tl!("Quick Actions")).show(ui, |ui, _| { + let _ = MemoView::new("properties-quick-actions", Language { lang }, quick_actions_body).show(ui); + }); +} + +/// What a section that reads only command states reads besides them: the interface language of its labels. +#[derive(MemoProps)] +struct Language { + #[memo_props(hash = "an interface language: an index into the languages offered")] + lang: crate::i18n::Lang, +} + +/// What the Document section reads: the document's units and how many artboards it has. +#[derive(MemoProps)] +struct DocumentProps<'a> { + /// The index of `units` in [`Unit::ALL`]. + unit: usize, + #[memo_props(covered_by = "unit", reason = "the unit's index in Unit::ALL")] + units: Unit, + /// How many artboards the document has (Rearrange All shows for more than one). + artboards: usize, + #[memo_props(hash = "an interface language: an index into the languages offered")] + lang: crate::i18n::Lang, + /// The units chosen in the Units menu. + picked: Outbox<'a, Unit>, +} + +/// The Document section's body: Units, Document Setup, Edit Artboards and Rearrange All. +fn document_body(p: &mut DocumentProps<'_>, ui: &mut Ui) { + #[cfg(test)] + tests::count_run(); + Field::new(tl!("Units")).show(ui, |ui, _| { + let entries: Vec> = Unit::ALL.iter().map(|u| Entry::new(*u, u.label())).collect(); + let out = Select::new(tl!("Units"), Bind::given(p.units), &entries).show(ui); + if let Some(u) = out.events.committed() { + p.picked.send(*u); } }); - super::section(tl!("Quick Actions")).show(ui, |ui, _| { - Grid::new(&HALVES).show(ui, |ui, _| { - // The reference panel's buttons name the dialog without the menu's ellipsis. - for (command, label) in [("file.documentSetup", tl!("Document Setup")), ("edit.preferences", tl!("Preferences"))] { - Button::command(CommandRef::new(command)).label(label).sizing(Sizing::FillWidth).show(ui); - } - }); + Grid::new(&HALVES).show(ui, |ui, _| { + Button::command(CommandRef::new("file.documentSetup")).label(tl!("Document Setup")).sizing(Sizing::FillWidth).show(ui); + Button::command(CommandRef::with_params("tool.select", json!({ "tool": "artboard" }))) + .label(tl!("Edit Artboards")) + .sizing(Sizing::FillWidth) + .show(ui); + if p.artboards > 1 { + rearrange_button(ui, p.artboards); + } + }); +} + +/// The Rulers & Grids section's body. The checked and enabled states and the tooltips come from the commands (Trim +/// View turns the grid off). +fn rulers_body(_: &mut Language, ui: &mut Ui) { + #[cfg(test)] + tests::count_run(); + Row::new().show(ui, |ui, _| { + for (command, icon) in [("view.rulers", "ruler"), ("view.grid", "grid-3x3"), ("view.transparencyGrid", "square-dashed")] { + ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui); + } + }); +} + +/// The Guides section's body: Show Guides and Smart Guides, from their commands. +fn guides_body(_: &mut Language, ui: &mut Ui) { + #[cfg(test)] + tests::count_run(); + Row::new().show(ui, |ui, _| { + for (command, icon) in [("view.guides", "layout-grid"), ("view.smartGuides", "sparkles")] { + ToggleButton::command(CommandRef::new(command)).icon(IconId::new(icon)).icon_only().show(ui); + } + }); +} + +/// What the Snap Options section reads: whether Snap to Point and Snap to Grid are on. +#[derive(MemoProps)] +struct SnapProps<'a> { + point: bool, + grid: bool, + #[memo_props(hash = "an interface language: an index into the languages offered")] + lang: crate::i18n::Lang, + /// The command of each checkbox clicked. + picked: Outbox<'a, &'static str>, +} + +/// The Snap Options section's body: Snap to Point and Snap to Grid. +fn snap_body(p: &mut SnapProps<'_>, ui: &mut Ui) { + #[cfg(test)] + tests::count_run(); + for (command, label, on) in [("view.snapToPoint", tl!("Snap to Point"), p.point), ("view.snapToGrid", tl!("Snap to Grid"), p.grid)] { + if Checkbox::new(Bind::given(on)).label(label).show(ui).value().is_some() { + p.picked.send(command); + } + } +} + +/// What the Preferences section reads: the keyboard increment in the General unit, and Scale Strokes & Effects. +#[derive(MemoProps)] +struct PreferencesProps<'a> { + /// The keyboard increment, in points. + increment: f64, + /// The index of the General unit in [`Unit::ALL`]. + unit: usize, + /// Numbers without units are points (Preferences › Units). + bare_points: bool, + #[memo_props(covered_by = "unit", reason = "made from the General unit and bare_points alone (canvas::rulers::lengths)")] + lengths: LengthUnits, + /// Scale Strokes & Effects is on. + scale: bool, + #[memo_props(hash = "an interface language: an index into the languages offered")] + lang: crate::i18n::Lang, + /// The keyboard increment's edits and the Scale Strokes & Effects choice. + picked: Outbox<'a, PreferencePick>, +} + +/// What the Preferences section reports: an edit of the keyboard increment, or Scale Strokes & Effects turned on or +/// off. +enum PreferencePick { + Increment(EditEvent>), + ScaleStrokes(bool), +} + +/// The Preferences section's body: Keyboard Increment and Scale Strokes & Effects. +fn preferences_body(p: &mut PreferencesProps<'_>, ui: &mut Ui) { + #[cfg(test)] + tests::count_run(); + Field::new(tl!("Keyboard Increment")).show(ui, |ui, _| { + // The engine's own range for the preference (`prefscmds.rs`): a larger value is an error. + let out = NumberField::length(tl!("Keyboard Increment"), Bind::given(Some(p.increment)), p.lengths).range(0.001..=1296.0).show(ui); + for event in out.edit.events.iter() { + p.picked.send(PreferencePick::Increment(event.clone())); + } + }); + let out = Checkbox::new(Bind::given(p.scale)).label(tl!("Scale Strokes & Effects")).show(ui); + if let Some(on) = out.value().copied() { + p.picked.send(PreferencePick::ScaleStrokes(on)); + } +} + +/// The Quick Actions section's body: Document Setup and Preferences. The reference panel's buttons name the dialog +/// without the menu's ellipsis. +fn quick_actions_body(_: &mut Language, ui: &mut Ui) { + #[cfg(test)] + tests::count_run(); + Grid::new(&HALVES).show(ui, |ui, _| { + for (command, label) in [("file.documentSetup", tl!("Document Setup")), ("edit.preferences", tl!("Preferences"))] { + Button::command(CommandRef::new(command)).label(label).sizing(Sizing::FillWidth).show(ui); + } }); } @@ -104,6 +232,19 @@ mod tests { use crate::tests::panel_harness::Panel; use craft::ui::contract::Role; + thread_local! { + /// How many times this thread ran a memoized Document section. + static SECTION_RUNS: std::cell::Cell = const { std::cell::Cell::new(0) }; + } + + pub(super) fn count_run() { + SECTION_RUNS.with(|n| n.set(n.get() + 1)); + } + + fn runs() -> u64 { + SECTION_RUNS.with(std::cell::Cell::get) + } + #[test] fn canceling_the_units_popup_leaves_the_document_unchanged() { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); @@ -135,4 +276,54 @@ mod tests { assert_eq!(panel.app().session.active().unwrap().doc.units, next, "Enter applies the chosen document unit"); assert_eq!(panel.app().session.general_unit(), next); } + + /// Frames that change nothing the sections show replay them instead of laying them out again. A change one of + /// them reads, from a command run elsewhere, runs it again, and it shows the change. + #[test] + fn idle_frames_replay_the_document_sections() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + let mut panel = Panel::new(app, document_sections); + panel.room(egui::vec2(400.0, 1000.0)); + panel.frame(); + panel.frame(); + let before = runs(); + for _ in 0..10 { + panel.frame(); + } + assert_eq!(runs() - before, 0, "idle frames run the memoized sections no more"); + let point = panel.app().ui.view.snap_to_point; + panel.app().run("view.snapToPoint", json!({})).unwrap(); + panel.frame(); + panel.frame(); + assert!(runs() > before, "a new snap setting runs the sections again"); + assert_eq!(panel.app().ui.view.snap_to_point, !point); + assert_eq!(panel.toggled("Snap to Point"), Some(craft::behavior::Mixed::Same(!point)), "and the checkbox shows it"); + panel.app().run("view.rulers", json!({})).unwrap(); + let mut per_frame = Vec::new(); + for _ in 0..12 { + let at = runs(); + panel.frame(); + per_frame.push(runs() - at); + } + // A toggled button animates over the next frames, and a section runs while an animation in it has not ended. + assert_eq!(per_frame[6..], [0; 6], "idle frames after a command run them no more: {per_frame:?}"); + } + + /// The memoized sections still take input: a click on Snap to Grid turns it on, once, and Scale Strokes & Effects + /// sets its preference. + #[test] + fn the_memoized_sections_run_their_picks() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + let grid = app.ui.view.snap_to_grid; + let scale = app.session.prefs.scale_strokes; + let mut panel = Panel::new(app, document_sections); + panel.room(egui::vec2(400.0, 1000.0)); + panel.frame(); + panel.click("Snap to Grid"); + assert_eq!(panel.app().ui.view.snap_to_grid, !grid, "the click toggles Snap to Grid once"); + panel.click("Scale Strokes & Effects"); + assert_eq!(panel.app().session.prefs.scale_strokes, !scale); + } } From 2f508db904b14e42d36d5ddd47d6c648ebf9c9bd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:55:41 -0800 Subject: [PATCH 2/2] Properties: the Preferences key reads the General unit after a Units pick The Preferences section's memo key took the General unit's index before the Units commit ran, while its lengths were read after it. On the frame that picked new units the key still matched the previous frame, so the section replayed and the Keyboard Increment showed the old unit for that frame. The key now reads the unit next to the lengths. --- .../panels/properties/document_sections.rs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/crates/ui-egui/src/panels/properties/document_sections.rs b/crates/ui-egui/src/panels/properties/document_sections.rs index 4774bdb750..cff44b4f44 100644 --- a/crates/ui-egui/src/panels/properties/document_sections.rs +++ b/crates/ui-egui/src/panels/properties/document_sections.rs @@ -57,6 +57,9 @@ pub(super) fn document_sections(app: &mut VectorcraftApp, ui: &mut Ui) { app.run(command, json!({})).ok(); } let mut prefs = Vec::new(); + // Read again after the Units commit above, so the key and `lengths` come from the same General unit. + let general = app.session.general_unit(); + let unit = Unit::ALL.iter().position(|u| *u == general).unwrap_or(usize::MAX); let (increment, scale) = (app.session.prefs.keyboard_increment, app.session.prefs.scale_strokes); let (bare_points, lengths) = (app.session.prefs.numbers_without_units_are_points, crate::canvas::rulers::general_lengths(app)); super::section(tl!("Preferences")).show(ui, |ui, _| { @@ -310,6 +313,31 @@ mod tests { assert_eq!(per_frame[6..], [0; 6], "idle frames after a command run them no more: {per_frame:?}"); } + /// The frame that picks new units in the Units menu shows the Keyboard Increment in them: the Preferences + /// section's key reads the General unit after the pick runs, as its lengths do. + #[test] + fn the_frame_that_picks_units_shows_the_increment_in_them() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + let initial = app.session.general_unit(); + let mut panel = Panel::new(app, document_sections); + panel.room(egui::vec2(400.0, 1000.0)); + panel.frame(); + let before = panel.value("Keyboard Increment"); + panel.click_role("Units", Role::COMBO_BOX); + panel.key(egui::Key::ArrowDown, egui::Modifiers::NONE); + panel.key(egui::Key::ArrowDown, egui::Modifiers::NONE); + let enter = + |pressed| egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; + panel.frame_with(vec![enter(true), enter(false)]); + assert_ne!(panel.app().session.general_unit(), initial, "Enter applies the chosen unit"); + let picked = panel.value("Keyboard Increment"); + panel.frame(); + let settled = panel.value("Keyboard Increment"); + assert_ne!(settled, before, "the increment shows in the new unit"); + assert_eq!(picked, settled, "the frame of the pick already shows the increment in the new unit"); + } + /// The memoized sections still take input: a click on Snap to Grid turns it on, once, and Scale Strokes & Effects /// sets its preference. #[test]