From 8a7f01b8210b94f99a137a531643de4ddf8140e3 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 02:01:38 +0700 Subject: [PATCH] ui-widgets: a Select whose value names no entry reads as the placeholder its face shows --- crates/ui-widgets/src/pickers/select.rs | 7 ++++++- .../tests/it/pickers/select_steps.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/ui-widgets/src/pickers/select.rs b/crates/ui-widgets/src/pickers/select.rs index f42d7a98b..456a376cd 100644 --- a/crates/ui-widgets/src/pickers/select.rs +++ b/crates/ui-widgets/src/pickers/select.rs @@ -292,13 +292,18 @@ impl 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)); } diff --git a/crates/ui-widgets/tests/it/pickers/select_steps.rs b/crates/ui-widgets/tests/it/pickers/select_steps.rs index f7929e4ef..3639ba031 100644 --- a/crates/ui-widgets/tests/it/pickers/select_steps.rs +++ b/crates/ui-widgets/tests/it/pickers/select_steps.rs @@ -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 = 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"); + } +}