From be5e97ea495f791b9e540a5f7cfb03ef897353bc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 23 Nov 2026 08:27:17 -0500 Subject: [PATCH] fw sync: F-R1: Param::select_on_focus for text rows (Rename opens on the name selected); trial a_text_row_that_selects_on_focus_is_replaced_by_typing --- .../src/patterns/form_dialogs/param.rs | 15 +++++++++++++ .../src/patterns/form_dialogs/rows.rs | 3 +++ .../tests/it/form_dialogs/params.rs | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/crates/ui-patterns/src/patterns/form_dialogs/param.rs b/crates/ui-patterns/src/patterns/form_dialogs/param.rs index 5840f228a8..8930c6ef48 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/param.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/param.rs @@ -491,6 +491,8 @@ pub(crate) struct RowOptions { max_chars: Option, /// Whether a text row shows its text in the monospace face ([`Param::mono`]). mono: bool, + /// Whether a text row selects its text when it takes the focus ([`Param::select_on_focus`]). + select_on_focus: bool, /// The width a text row's field asks for, in characters ([`Param::field_chars`]). field_chars: Option, units: Option, @@ -885,6 +887,14 @@ impl Param { self } + /// A [`ParamKind::Text`] row selects its whole text when it takes the focus, so typing replaces it (a Rename + /// dialog opening on the name); by default the caret goes to the end. Other kinds are unchanged. + #[must_use] + pub fn select_on_focus(mut self) -> Self { + self.options.get_or_insert_default().select_on_focus = true; + self + } + /// The width of a [`ParamKind::Text`] row's field, in characters (`TextField::width_chars`): the field asks for /// `chars` times the width of a `0` in its face instead of the field length, so one row of a form can be wider or /// narrower than the others (a watermark's text against a short name). A row that lacks room still narrows it. @@ -1170,6 +1180,11 @@ impl RowOptions { self.mono } + /// Whether a text row selects its text when it takes the focus. + pub(crate) fn select_on_focus(&self) -> bool { + self.select_on_focus + } + /// The width a text row's field asks for, in characters; `None` for the field length. pub(crate) fn field_chars(&self) -> Option { self.field_chars diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index 59d326ebfa..c3c74e7438 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -1067,6 +1067,9 @@ impl<'p> Pass<'p> { if options.mono() { field = field.mono(); } + if options.select_on_focus() { + field = field.select_on_focus(); + } if let Some(chars) = options.field_chars() { field = field.width_chars(chars); } diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index 6481eede35..c59574423d 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -630,3 +630,25 @@ fn a_row_beside_the_previous_one_shares_its_line() { assert!(stacked.is_some_and(|gap| gap > 10.0), "unmarked rows stack: {stacked:?}"); assert!(beside.is_some_and(|gap| gap.abs() < 3.0), "a beside row shares the line: {beside:?}"); } + +/// A text row with `select_on_focus` selects its whole text when it takes the focus, so typing replaces it (PdfCraft's +/// Rename opens on the name selected); a row without it keeps the text and types at the caret. +#[trial] +fn a_text_row_that_selects_on_focus_is_replaced_by_typing() { + for (select, expected) in [(true, "Draft"), (false, "")] { + let param = Param::new("name", "Name", ParamKind::text("Report")); + let params = vec![if select { param.select_on_focus() } else { param }]; + let values = Rc::new(RefCell::new(DialogFields::new())); + values.borrow_mut().set("name", FieldValue::Text("Report".into())); + let mut fixture = rows(params, &values); + let field = fixture.by_label("Name").expect("the name row has a field"); + fixture.click(&field).settle(); + fixture.type_text("Draft").settle(); + let typed = values.borrow().text("name").map(str::to_owned); + if select { + assert_eq!(typed.as_deref(), Some(expected), "typing replaced the selected name"); + } else { + assert!(typed.as_deref().is_some_and(|text| text.contains("Report") && text.contains("Draft")), "typing kept the name: {typed:?}"); + } + } +}