ui-contract: a field label without text names no control, so a custom row with an empty label leaves its control the name its own label gives it

This commit is contained in:
bflatastic
2026-11-23 12:33:18 +07:00
parent f712b3306b
commit 2bad4aebd5
2 changed files with 25 additions and 3 deletions
@@ -521,13 +521,15 @@ pub(crate) enum LabelPatch {
/// What `label` does to the `nodes` registered in the scope of `container`: one control is labelled by it
/// (described by it when the control keeps its own name or the label describes), several make the container's node
/// labelled by it, none leaves everything alone. A label with no node of its own names its one control by its text,
/// or joins the control's description, and leaves the container its own name.
/// labelled by it, none, or a label without text, leaves everything alone. A label with no node of its own names its
/// one control by its text, or joins the control's description, and leaves the container its own name.
pub(crate) fn label_patches(container: Id, label: &FieldLabel, nodes: &[ScopeNode]) -> Vec<LabelPatch> {
if nodes.is_empty() {
return Vec::new();
}
let own = label.shown.then_some(label.id);
// A label without text has nothing to name or describe: the controls keep their own names.
let named = !label.text.is_empty();
let own = label.shown.then_some(label.id).filter(|_| named);
let mut patches: Vec<LabelPatch> =
own.map(|id| LabelPatch::Label { id, text: label.text.clone(), disabled: nodes.iter().all(|node| node.disabled) }).into_iter().collect();
// The label is about one control only when that control sits directly in the scope. One in an unlabelled row is
@@ -538,6 +540,7 @@ pub(crate) fn label_patches(container: Id, label: &FieldLabel, nodes: &[ScopeNod
_ => None,
};
match (single, own) {
_ if !named => {}
(Some(only), Some(own)) if label.role == FieldLabelRole::Describes || only.keeps_name => {
patches.push(LabelPatch::DescribedBy { node: only.id, label: own });
}
+19
View File
@@ -797,3 +797,22 @@ fn a_toggle_button_reports_pressed_on_and_off() {
assert_eq!(tree.toggled_of(NodeKey::of(plain)), None, "a plain button has no pressed state");
}
}
/// An icon-only toggle button in a field with no label text (a dialog's custom row with an empty label), beside an egui
/// check box in an `egui::Grid`, keeps the name its own label gives it.
#[trial]
fn an_icon_only_toggle_in_a_field_without_label_text_keeps_its_name() {
let mut fixture = Fixture::new(|ui| {
craft_ui::form::Field::new("").show(ui, |ui, _| {
egui::Grid::new("inks").num_columns(2).show(ui, |ui| {
let mut print = true;
ui.add(egui::Checkbox::without_text(&mut print));
let _ = ToggleButton::new("Convert Cyan to process", Bind::given(false)).icon(IconId::new("eye")).icon_only().show(ui);
ui.end_row();
});
});
});
fixture.settle();
let toggle = fixture.by_label("Convert Cyan to process").expect("the toggle keeps its name");
assert!(fixture.by_role(Role::BUTTON).iter().any(|button| button.node == toggle.node), "the named node is the toggle button");
}