From 54de7e2c90da0b9b8a82715848c35643c52b56d0 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 00:35:43 +0100 Subject: [PATCH] draw: keep the brush picker's app rows through the head's measuring run The compact Brush Preset picker took its app rows out in the head's measuring run, so the visible head drew none of them. The rows are now FnMut and drawn in every run. Ported from storytold/craft-libs#2761 (needed for the Color Replacement tool's compact popup, storytold/photocraft#11931). Co-authored-by: skylartaylor Co-Authored-By: Claude Opus 5.5 --- crates/draw/src/ui/brush_presets/picker.rs | 11 +++--- crates/draw/tests/it/brush_presets.rs | 45 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/crates/draw/src/ui/brush_presets/picker.rs b/crates/draw/src/ui/brush_presets/picker.rs index 23966619a8..9b8da79650 100644 --- a/crates/draw/src/ui/brush_presets/picker.rs +++ b/crates/draw/src/ui/brush_presets/picker.rs @@ -373,8 +373,9 @@ pub struct BrushPresetPicker<'a, K: Value + Eq + Hash + MemoProps> { /// Whether the picker takes input: off disables its field, its head and its popover. It is enabled by default. #[prop] enabled: bool, - /// App rows drawn under the head. - #[prop(name = "rows", boxed = "FnOnce(&mut Ui) + 'a")] + /// App rows drawn under the head. The head may draw them more than once in a pass (its measuring run, then the + /// visible one), so the callback keeps its captured values for each call. + #[prop(name = "rows", boxed = "FnMut(&mut Ui) + 'a")] app_rows: Option>, /// Paints the brush's current tip on the chip, as the app's engine draws it (its falloff, angle and roundness, a /// sampled tip's shape); without it the chip shows the picked preset's tip. @@ -386,8 +387,8 @@ pub struct BrushPresetPicker<'a, K: Value + Eq + Hash + MemoProps> { recent_source: Option<&'a PresetSource<'a, K>>, } -/// The app's own rows of the picker's gear menu. -type AppRows<'a> = Box; +/// The app's rows under the picker's head. +type AppRows<'a> = Box; impl<'a, K: Value + Eq + Hash + MemoProps> BrushPresetPicker<'a, K> { /// A picker named `label` over the presets of `source` (build each row with [`brush_row`](super::brush_row)), @@ -474,7 +475,7 @@ impl<'a, K: Value + Eq + Hash + MemoProps> BrushPresetPicker<'a, K> { for edits in sent { (diameter, hardness, tip, out_of_range) = (edits.diameter, edits.hardness, edits.tip, edits.out_of_range); } - if let Some(rows) = app_rows.take() { + if let Some(rows) = app_rows.as_mut() { rows(ui); } }; diff --git a/crates/draw/tests/it/brush_presets.rs b/crates/draw/tests/it/brush_presets.rs index bd9c0de9d3..8558e9d47f 100644 --- a/crates/draw/tests/it/brush_presets.rs +++ b/crates/draw/tests/it/brush_presets.rs @@ -461,3 +461,48 @@ fn show_recent_brushes_hides_the_row() { assert!(!host.borrow().state.recent_row, "the box turned the row off"); assert!(fixture.by_label("Recent brushes").is_none(), "the row is gone"); } + +/// App rows belong to the visible compact head, not only to its measuring run: they show and their controls edit the +/// app's values. +#[trial] +fn compact_app_rows_replay_measurement_and_commit_visible_values() { + use craft_ui::contract::Show as _; + use craft_ui::form::SliderField; + + let host = Rc::new(RefCell::new((None::, BrushPickerState::new(), 25.0_f64))); + let calls = Rc::new(RefCell::new(Vec::>::new())); + let (held, seen) = (Rc::clone(&host), Rc::clone(&calls)); + let mut fixture = Fixture::new(move |ui| { + let mut host = held.borrow_mut(); + let (current, state, tolerance) = &mut *host; + let trace = Rc::new(RefCell::new(Vec::new())); + let record = Rc::clone(&trace); + // A captured owned label stays usable without consuming the callback's environment. + let label = String::from("Tolerance"); + let rows = PresetSource::new((), PresetFns::new(|_, _| Vec::>::new())); + let _ = BrushPresetPicker::new("Compact brush", Bind::bound(current), &rows, Bind::bound(state), OPENED) + .compact(true) + .rows(move |ui| { + record.borrow_mut().push(ui.is_sizing_pass()); + let _ = SliderField::new(label.clone(), Bind::bound(tolerance), 0.0..=100.0).step(1.0).show(ui); + }) + .show(ui); + seen.borrow_mut().push(trace.borrow().clone()); + }); + fixture.room(Vec2::new(640.0, 760.0)); + fixture.settle(); + let field = fixture.by_role(Role::COMBO_BOX).first().copied().expect("the compact field shows"); + fixture.click(&field); + fixture.settle(); + assert!(fixture.by_label("Size").is_some(), "the built-in head is visible"); + assert!(calls.borrow().iter().any(|trace| trace.contains(&true)), "the head was measured: {:?}", calls.borrow()); + assert!(fixture.by_label("Tolerance").is_some(), "the app row survives the head's measuring run: {:?}", calls.borrow()); + let slider = fixture + .by_role(Role::SLIDER) + .into_iter() + .find(|target| fixture.tree().name_of(target.node) == Some("Tolerance")) + .expect("the app row's slider is accessible"); + fixture.click_at(slider.rect.left_center() + Vec2::new(slider.rect.width() * 0.8, 0.0)); + fixture.settle(); + assert!(host.borrow().2 > 50.0, "the visible control commits to the app's value: {}", host.borrow().2); +}