fix(ui-patterns): a preferences text row takes a placeholder (#4028) (#4091)

Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin BallandClaude Sonnet 5.5 authored and GitHub committed 2026-11-28 20:18:48 +00:00
1 parent 7c058a1d2d
commit bd4f24ac1e
3 files changed
+48 -5

No files matched your search

@@ -126,6 +126,8 @@ pub struct RowSpec<'a> {
/// default a drop-down, and on a card ([`PreferencesLayout::Cards`](super::PreferencesLayout::Cards)) a short
/// choice of up to five options is segmented. Ignored by other kinds.
pub pills: Option<bool>,
/// A [`RowKind::Text`] row: the translated text shown while the field is empty. Ignored by other kinds.
pub placeholder: Option<&'a str>,
/// The control and the value it shows.
pub kind: RowKind<'a>,
}
@@ -133,7 +135,7 @@ pub struct RowSpec<'a> {
impl<'a> RowSpec<'a> {
/// A visible, enabled row with no keywords or help.
pub fn new(key: &'a str, label: &'a str, kind: RowKind<'a>) -> Self {
Self { key, label, keywords: "", help: None, visible: true, enabled: true, step: None, pills: None, kind }
Self { key, label, keywords: "", help: None, visible: true, enabled: true, step: None, pills: None, placeholder: None, kind }
}
/// Sets extra search text for the row.
@@ -171,6 +173,13 @@ impl<'a> RowSpec<'a> {
self
}
/// Sets the text a [`RowKind::Text`] row shows while empty.
#[must_use]
pub fn placeholder(mut self, placeholder: &'a str) -> Self {
self.placeholder = Some(placeholder);
self
}
/// Sets the step of a [`RowKind::Float`] row.
#[must_use]
pub fn step(mut self, step: f64) -> Self {
@@ -39,7 +39,7 @@ pub(super) enum Kind {
Int { value: i64, range: RangeInclusive<i64>, unit: Option<UnitKind>, unit_text: Option<Label> },
Float { value: f64, range: RangeInclusive<f64>, decimals: u8, step: Option<f64>, unit: Option<UnitKind>, unit_text: Option<Label> },
Length { value: f64, range: RangeInclusive<f64>, lengths: LengthUnits },
Text(String),
Text { value: String, placeholder: Option<Label> },
Custom,
CustomRows,
}
@@ -72,7 +72,7 @@ impl ShownRow {
unit_text: unit_text.map(|text| Label::from(text.to_owned())),
},
RowKind::Length { value, range, lengths } => Kind::Length { value: *value, range: range.clone(), lengths: *lengths },
RowKind::Text(text) => Kind::Text((*text).to_owned()),
RowKind::Text(text) => Kind::Text { value: (*text).to_owned(), placeholder: row.placeholder.map(|text| Label::from(text.to_owned())) },
RowKind::Custom => Kind::Custom,
RowKind::CustomRows => Kind::CustomRows,
};
@@ -229,10 +229,14 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(
});
record(next.map(PrefValue::Float));
}
Kind::Text(value) => {
Kind::Text { value, placeholder } => {
let mut next = None;
control(ui, row, cards, |ui| {
next = TextField::new(label.clone(), Bind::given(value.clone())).show(ui).events.landed().cloned();
let mut field = TextField::new(label.clone(), Bind::given(value.clone()));
if let Some(placeholder) = placeholder {
field = field.placeholder(placeholder.clone());
}
next = field.show(ui).events.landed().cloned();
});
record(next.map(PrefValue::Text));
}
@@ -320,3 +320,33 @@ fn the_search_shows_how_many_settings_match() {
fixture.click(&search).type_text("snap").settle();
assert!(fixture.texts().iter().any(|t| t.text == "1 match"), "{:?}", fixture.texts().iter().map(|t| &t.text).collect::<Vec<_>>());
}
/// A text row with a placeholder shows it while the field is empty; one without shows none.
#[trial]
fn a_text_rows_placeholder_shows_while_empty() {
struct Initials(bool);
impl PreferencesModel for Initials {
fn pages(&self) -> Vec<PageSpec<'_>> {
let row = RowSpec::new("general.initials", "Initials", RowKind::Text(""));
vec![PageSpec::new("general", "General", vec![if self.0 { row.placeholder("AB") } else { row }])]
}
fn set(&mut self, _: &str, _: PrefValue) {}
fn custom_row(&mut self, _: &mut Ui, _: &str) -> bool {
false
}
fn custom_page(&mut self, _: &mut Ui, _: &str) -> bool {
false
}
fn reset_page(&mut self, _: &str) {}
}
let shown = |placeholder| {
let mut state = PreferencesState::default();
let mut fixture = Fixture::new(move |ui| {
PreferencesPage::new(Bind::bound(&mut state)).show(ui, &mut Initials(placeholder));
});
fixture.settle();
craft_ui::testing::text::painted(&fixture.frame_shapes()).iter().any(|text| text == "AB")
};
assert!(shown(true), "the placeholder is painted");
assert!(!shown(false), "no placeholder, none painted");
}