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

This commit is contained in:
Dylan M. Taylor committed 2026-11-23 08:27:17 -05:00
1 parent 874efb4576
commit be5e97ea49
3 files changed
+40

No files matched your search

@@ -491,6 +491,8 @@ pub(crate) struct RowOptions {
max_chars: Option<usize>,
/// 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<u16>,
units: Option<UnitChoices>,
@@ -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<u16> {
self.field_chars
@@ -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);
}
@@ -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:?}");
}
}
}