ui-widgets: a Select whose value names no entry reads as the placeholder its face shows

This commit is contained in:
bflatastic
2026-11-23 02:01:38 +07:00
parent a37e402774
commit 8a7f01b821
2 changed files with 24 additions and 1 deletions
+6 -1
View File
@@ -292,13 +292,18 @@ impl<T: Value + 'static> Component for Select<'_, T> {
}
/// The picked entry's place among the entries that can be picked, shown as its label, so AccessKit steps and sets it.
/// A value that names no entry reads as the placeholder the face shows for it.
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
let stops = step_stops(self.entries);
let range = 0.0..=stops.len().saturating_sub(1) as f64;
let held = self.position(cx.value());
let placeholder = self.placeholder.as_ref().filter(|_| held.is_none() && cx.value().is_some());
let numeric = match held.and_then(|held| stops.iter().position(|&stop| stop == held)) {
Some(at) => Numeric::new(at as f64, range).text(cx.env.resolve(self.entries[stops[at]].label()).as_str()),
None => Numeric::empty(range),
None => match placeholder {
Some(text) => Numeric::empty(range).text(cx.env.resolve(text).as_str()),
None => Numeric::empty(range),
},
};
access.numeric(numeric.step(1.0));
}
@@ -49,3 +49,21 @@ fn the_closed_face_shows_an_entrys_closed_label_and_the_accessible_value_keeps_i
let node = fixture.by_role(Role::COMBO_BOX).first().expect("the field").node;
assert_eq!(fixture.tree().value_of(node), Some("1 Dots"), "the accessible value is the full label");
}
/// A value that names no entry shows the placeholder on the face, and the field reads it as its value, as the face
/// shows it; a value that names an entry reads that entry. Guards against the shown placeholder leaving the field
/// with no value to read.
#[trial]
fn a_value_on_no_entry_reads_as_the_placeholder_the_face_shows() {
for (held, read) in [(9u8, "Custom"), (1u8, "One")] {
let mut fixture = Fixture::new(move |ui| {
let entries = [Entry::new(1u8, "One"), Entry::new(2u8, "Two")];
Select::new("Preset", Bind::given(held), &entries).placeholder("Custom").show(ui);
});
fixture.settle();
let texts: Vec<String> = fixture.texts().iter().map(|run| run.text.clone()).collect();
assert!(texts.iter().any(|text| text == read), "the face shows {read}: {texts:?}");
let node = fixture.by_role(Role::COMBO_BOX).first().expect("the field").node;
assert_eq!(fixture.tree().value_of(node), Some(read), "the field reads what its face shows");
}
}