ui-contract: a hidden field label, and one drawn inside its control, makes no Label node of its own: it names its control by its text alone (FieldLabel::shown)

This commit is contained in:
bflatastic
2026-11-23 02:01:35 +07:00
parent 73701aca9d
commit a37e402774
4 changed files with 55 additions and 16 deletions
@@ -298,7 +298,7 @@ fn show(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, decided: Decided, trailing:
let label_id = sub(input.scope, "label");
let Decided { placed, leaf, .. } = decided;
let Some(DecidedLeaf { leaf, mut kids, installed, reset }) = leaf else {
scope::begin(&ctx, input.scope, input.owner, scope::Kind::Label(field_label(env, input, label_id, Rect::NOTHING)));
scope::begin(&ctx, input.scope, input.owner, scope::Kind::Label(field_label(env, input, label_id, Rect::NOTHING).shown(false)));
scope::place_controls(&ctx, input.scope, placed.control.top());
return placed;
};
+31 -12
View File
@@ -49,6 +49,9 @@ pub struct FieldLabel {
pub link: bool,
/// The container shows its control's validity message as its status line (`FooterRegion::status`).
pub status: bool,
/// The label shows as a node of its own. A hidden label and one drawn inside its control have none: they name
/// their control by their text alone.
pub shown: bool,
}
impl FieldLabel {
@@ -65,9 +68,17 @@ impl FieldLabel {
activated: false,
link: false,
status: false,
shown: true,
}
}
/// Whether the label shows as a node of its own.
#[must_use]
pub fn shown(mut self, shown: bool) -> Self {
self.shown = shown;
self
}
/// Whether the label names or describes the controls.
#[must_use]
pub fn role(mut self, role: FieldLabelRole) -> Self {
@@ -494,13 +505,14 @@ pub(crate) struct Merged {
/// One change a label scope makes to a node, decided here and written by `runtime::egui::access`.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum LabelPatch {
/// The label's own node: made with the role `Label` and the label's text where no component wrote it (a hidden
/// label), and disabled when every control it labels is.
/// The label's own node, which its leaf wrote: made with the role `Label` and the label's text where the leaf wrote
/// none this pass, and disabled when every control it labels is.
Label { id: Id, text: String, disabled: bool },
/// `node` keeps its name and is described by `label`.
DescribedBy { node: Id, label: Id },
/// `node` is named `text`, the label's text, and keeps `label` as the node that labels it.
LabelledBy { node: Id, label: Id, text: String },
/// `node` is named `text`, the label's text, and keeps `label` as the node that labels it; `None` for a label with
/// no node of its own.
LabelledBy { node: Id, label: Option<Id>, text: String },
/// `container` is labelled by `label`: it holds several controls.
GroupLabelledBy { container: Id, label: Id },
/// `text` joins the description of `node`.
@@ -509,12 +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.
/// 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.
pub(crate) fn label_patches(container: Id, label: &FieldLabel, nodes: &[ScopeNode]) -> Vec<LabelPatch> {
if nodes.is_empty() {
return Vec::new();
}
let mut patches = vec![LabelPatch::Label { id: label.id, text: label.text.clone(), disabled: nodes.iter().all(|node| node.disabled) }];
let own = label.shown.then_some(label.id);
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
// part of the field's arrangement: the label names the group, as for several controls, and the control keeps the
// name its own content gives it (a link's file name, a command button's words).
@@ -522,12 +537,16 @@ pub(crate) fn label_patches(container: Id, label: &FieldLabel, nodes: &[ScopeNod
[only] if !only.arranged => Some(only),
_ => None,
};
match single {
Some(only) if label.role == FieldLabelRole::Describes || only.keeps_name => {
patches.push(LabelPatch::DescribedBy { node: only.id, label: label.id });
match (single, own) {
(Some(only), Some(own)) if label.role == FieldLabelRole::Describes || only.keeps_name => {
patches.push(LabelPatch::DescribedBy { node: only.id, label: own });
}
Some(only) => patches.push(LabelPatch::LabelledBy { node: only.id, label: label.id, text: label.text.clone() }),
None => patches.push(LabelPatch::GroupLabelledBy { container, label: label.id }),
(Some(only), None) if label.role == FieldLabelRole::Describes || only.keeps_name => {
patches.push(LabelPatch::Detail { node: only.id, text: label.text.clone() });
}
(Some(only), _) => patches.push(LabelPatch::LabelledBy { node: only.id, label: own, text: label.text.clone() }),
(None, Some(own)) => patches.push(LabelPatch::GroupLabelledBy { container, label: own }),
(None, None) => {}
}
if let Some(text) = &label.detail {
patches.push(LabelPatch::Detail { node: single.map_or(container, |only| only.id), text: text.clone() });
@@ -617,7 +636,7 @@ pub(crate) fn finish(ctx: &Context, container: Id, merged: Option<Merged>) {
}
for patch in &patches {
adapter::write_label_patch(ctx, patch);
if let LabelPatch::LabelledBy { node, label, .. } = patch
if let LabelPatch::LabelledBy { node, label: Some(label), .. } = patch
&& crate::runtime::inspect::recording(ctx)
{
crate::runtime::inspect::labelled(ctx, *node, *label);
@@ -9,8 +9,8 @@ pub(crate) fn write_label_patch(ctx: &Context, patch: &LabelPatch) {
match patch {
LabelPatch::Label { id, text, disabled } => {
ctx.accesskit_node_builder(*id, |node| {
// A shown label's leaf wrote its node, named, in this pass; a hidden label has no component, and its
// node is made here, named by the same rule.
// The label's leaf wrote its node, named, in this pass; one it did not write is made here, named by the
// same rule.
if node.role() == accesskit::Role::Unknown {
node.set_role(accesskit::Role::Label);
super::node::write_name(node, text);
@@ -30,7 +30,9 @@ pub(crate) fn write_label_patch(ctx: &Context, patch: &LabelPatch) {
LabelPatch::LabelledBy { node, label, text } => {
ctx.accesskit_node_builder(*node, |node| {
node.set_label(text.clone());
node.set_labelled_by(vec![label.accesskit_id()]);
if let Some(label) = label {
node.set_labelled_by(vec![label.accesskit_id()]);
}
});
}
LabelPatch::GroupLabelledBy { container, label } => {
+18
View File
@@ -48,3 +48,21 @@ fn a_hidden_label_with_content_shows_only_the_content() {
let radio = fixture.by_label(SIZE).expect("the label's text names the control");
assert!(radio.rect.max.x <= sample.rect.min.x, "the content follows its radio, as a label after it does");
}
/// A field whose label is hidden names its control and shows no label of its own: the control is the one node
/// named by the label's text, so a screen reader reads the name once. Guards against the hidden label still adding a
/// node beside the control it names.
#[trial]
fn a_hidden_field_label_names_its_control_and_adds_no_node() {
use craft_ui::choice::Checkbox;
use craft_ui::contract::Role;
const STEP: &str = "1. Draw a shape";
let mut fixture = Fixture::new(|ui| {
Field::new(STEP).side(SideLabel::new().kind(LabelKind::Hidden)).show(ui, |ui, _| Checkbox::new(Bind::given(false)).show(ui));
});
fixture.settle();
let tree = fixture.tree();
let named: Vec<_> = tree.keys().into_iter().filter(|key| tree.name_of(*key) == Some(STEP)).collect();
let check = fixture.by_role(Role::CHECK_BOX).first().expect("the check box").node;
assert_eq!(named, vec![check], "only the check box carries the name: {:?}", named.iter().map(|key| tree.role_of(*key)).collect::<Vec<_>>());
}