Merge #2399: Let adjacent parameter fields share a row

This commit is contained in:
David Mann committed 2026-11-20 12:51:42 +01:00
commit 2020862ecf
3 files changed
+108 -11

No files matched your search

@@ -468,6 +468,7 @@ pub(crate) struct RowOptions {
eyedropper: bool,
eyedropper_armed: bool,
full_width: bool,
beside_next: bool,
not_remembered: bool,
/// The lines a multi-line text row shows.
lines: Option<usize>,
@@ -841,6 +842,18 @@ impl Param {
self
}
/// Pairs this field with the next one in its section, wrapping when they cannot fit together.
/// If only one is visible, it keeps the content width it takes in a pair.
#[must_use]
pub fn beside_next(mut self) -> Self {
self.options.get_or_insert_default().beside_next = true;
self
}
pub(super) fn pairs_with_next(&self) -> bool {
self.option().beside_next
}
/// Sets the unit shown after the number.
#[must_use]
pub fn unit(mut self, unit: UnitKind) -> Self {
@@ -13,7 +13,7 @@ use super::words::ParamWords;
use crate::buttons::{Button, ToggleButton};
use crate::choice::{Checkbox, Radio, RadioGroup, Swatch};
use crate::contract::{
Bind, BodyCx, CanvasGesture, Entry, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, clicked,
Bind, BodyCx, CanvasGesture, Entry, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, Sizing, clicked,
};
use crate::display::Text;
use crate::form::{Field, FieldGroup, SliderField, SliderFieldLayout};
@@ -272,12 +272,16 @@ struct Line<'r> {
key: &'r str,
name: Label,
enabled: bool,
compact: bool,
}
impl Line<'_> {
/// Shows `body` in a field named by the row: its label beside the control, its tooltip over both.
fn show(&self, ui: &mut Ui, mut body: impl FnMut(&mut Ui)) {
let mut field = Field::new(self.name.clone()).id_salt(self.key).enabled(self.enabled);
if self.compact {
field = field.sizing(Sizing::Content);
}
if let Some(tip) = self.param.tooltip_text() {
field = field.tooltip(tip.clone());
}
@@ -376,29 +380,45 @@ impl<'p> Pass<'p> {
fn rows(&mut self, ui: &mut Ui, params: &[Param]) {
let is_section = |param: &Param| matches!(param.kind, ParamKind::Section);
let first = params.iter().position(is_section).unwrap_or(params.len());
for param in &params[..first] {
self.row(ui, param);
}
self.section_rows(ui, &params[..first]);
let mut rest = &params[first..];
while let Some((head, tail)) = rest.split_first() {
let end = tail.iter().position(is_section).unwrap_or(tail.len());
let (body, after) = tail.split_at(end);
if head.visible(&self.work) {
Section::fixed(plain(&head.label)).id_salt(head.key.as_str()).show(ui, |ui, _| {
for param in body {
self.row(ui, param);
}
self.section_rows(ui, body);
});
}
rest = after;
}
}
fn row(&mut self, ui: &mut Ui, param: &Param) {
fn section_rows(&mut self, ui: &mut Ui, params: &[Param]) {
let mut params = params.iter();
while let Some(param) = params.next() {
if param.pairs_with_next()
&& let Some(next) = params.next()
{
// A row with no visible fields must not add a gap to the form.
if !param.visible(&self.work) && !next.visible(&self.work) {
continue;
}
Row::new().id_salt(param.key.as_str()).wrap(true).fill_width().show(ui, |ui, _| {
self.row(ui, param, true);
self.row(ui, next, true);
});
} else {
self.row(ui, param, false);
}
}
}
fn row(&mut self, ui: &mut Ui, param: &Param, compact: bool) {
if !param.visible(&self.work) {
return;
}
let line = Line { param, key: param.key.as_str(), name: plain(&param.label), enabled: param.enabled(&self.work) };
let line = Line { param, key: param.key.as_str(), name: plain(&param.label), enabled: param.enabled(&self.work), compact };
let held = self.work.get(line.key).cloned();
self.show_row(ui, &line);
param.drop_dependents(held.as_ref(), &mut self.work);
@@ -653,7 +673,8 @@ impl<'p> Pass<'p> {
.or_else(|| rows.iter().find(is_pattern))
.map(|row| row.key.clone());
// With no pattern to offer the row is disabled and writes nothing.
let line = Line { param: line.param, key: line.key, name: line.name.clone(), enabled: line.enabled && current.is_some() };
let line =
Line { param: line.param, key: line.key, name: line.name.clone(), enabled: line.enabled && current.is_some(), compact: line.compact };
line.show(ui, |ui| {
let picked = PresetPicker::pattern(line.name.clone(), Bind::given(current.clone()), rows, Bind::kept()).show(ui).picked;
if let Some(id) = picked.filter(|id| rows.iter().filter(is_pattern).any(|row| row.key == *id)) {
@@ -5,7 +5,7 @@ use craft_ui::contract::{Bind, Role};
use craft_ui::patterns::blend_keys::{BlendKeyEntry, BlendShortcut};
use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, NumberUnit, Param, ParamDialog, ParamKind, ParamRows, set_param_field};
use craft_ui::patterns::presets::PresetRow;
use craft_ui::runtime::env::{Label, UnitKind};
use craft_ui::runtime::env::{Label, Scale, UnitKind};
use craft_ui::testing::{Fixture, trial};
use craft_ui::value_drag::SliderScale;
use egui::{Key, Modifiers, Vec2};
@@ -242,6 +242,69 @@ fn a_blend_row_steps_and_picks_by_its_keys_inside_the_dialog() {
assert_eq!(held().as_deref(), Some("multiply"), "Down on the closed list skips the greyed mode");
}
#[trial]
fn paired_parameter_fields_15539_keep_their_controls_and_values() {
let values = Rc::new(RefCell::new(DialogFields::new()));
let shown = values.clone();
let mut fixture = Fixture::new(move |ui| {
let modes = vec![
(BlendKeyEntry::new("normal".to_owned(), BlendShortcut::Normal), "Normal".into()),
(BlendKeyEntry::new("multiply".to_owned(), BlendShortcut::Multiply), "Multiply".into()),
];
let params = vec![
Param::new("name", "Name", ParamKind::text("Layer 1")),
Param::new("mode", "Mode", ParamKind::blend(modes, Some("normal".into()))).beside_next(),
Param::new("opacity", "Opacity", ParamKind::number(Some(0.0), Some(100.0), 100.0, 1.0, 0)).enabled_when("mode", "multiply"),
Param::new("neutral", "Fill with neutral color", ParamKind::bool(false)).enabled_when_in("mode", ["multiply".to_owned()]),
];
let _ = ParamDialog::new("newLayer", "New Layer", params).values(Bind::bound(&mut *shown.borrow_mut())).show(ui);
});
fixture.scale(Scale::new(1.25)).room(Vec2::new(1280.0, 800.0)).settle();
let mode = fixture.by_role(Role::COMBO_BOX).into_iter().find(|target| fixture.tree().name_of(target.node) == Some("Mode")).expect("mode");
let opacity =
fixture.by_role(Role::SPIN_BUTTON).into_iter().find(|target| fixture.tree().name_of(target.node) == Some("Opacity")).expect("opacity");
assert!((mode.center().y - opacity.center().y).abs() < 1.0, "Mode and Opacity share a row");
assert!(mode.rect.right() <= opacity.rect.left(), "Mode and Opacity have separate hit areas");
assert!(fixture.tree().is_disabled_key(opacity.node), "Normal has a disabled paired Opacity field");
fixture.click_at(opacity.center()).settle();
fixture.type_text("42").settle();
assert_eq!(values.borrow().number("opacity"), Some(100.0), "a disabled neighbor ignores input");
fixture.click(&mode).settle();
let multiply = fixture.by_label("Multiply").expect("the mode option");
fixture.click(&multiply).settle();
let opacity = fixture.by_label("Opacity").expect("the paired field remains present");
assert!(!fixture.tree().is_disabled_key(opacity.node), "the original predicate enables Opacity");
assert_eq!(values.borrow().text("mode"), Some("multiply"));
fixture.click(&opacity).settle();
fixture.key(Key::A, Modifiers::COMMAND).type_text("42").key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(values.borrow().number("opacity"), Some(42.0));
assert_eq!(values.borrow().text("mode"), Some("multiply"));
}
#[trial]
fn paired_parameter_fields_15539_leave_no_gap_when_both_are_hidden() {
let before = Param::new("before", "Before", ParamKind::text("one"));
let after = Param::new("after", "After", ParamKind::text("two"));
let values = Rc::new(RefCell::new(DialogFields::new()));
let hidden = rows(
vec![
before.clone(),
Param::new("mode", "Mode", ParamKind::text("Normal")).visible_when("show", true).beside_next(),
Param::new("opacity", "Opacity", ParamKind::int(Some(0), Some(100), 100)).visible_when("show", true),
after.clone(),
],
&values,
);
let center_y = |fixture: &Fixture, name: &str| fixture.by_label(name).expect(name).center().y;
let gap = |fixture: &Fixture| center_y(fixture, "After") - center_y(fixture, "Before");
assert!(hidden.by_label("Mode").is_none() && hidden.by_label("Opacity").is_none());
let absent = rows(vec![before, after], &Rc::new(RefCell::new(DialogFields::new())));
assert_eq!(gap(&hidden), gap(&absent), "a hidden pair uses no space");
}
/// A pattern row shows the first pattern for a stored id the rows lack without writing it, a pick writes the pattern's
/// id, and a group header takes no pick.
#[trial]