fix(ui-contract): a dialog that opens on a text field selects its text

A focus scope shown for the first time moves focus to its initial stop
(a dialog's `Initial::FirstField`), but a plain text field then put the
caret after its text, as its codec asks for any focus gain, so typing
added to the value instead of replacing it (New Layer's name read
"Layer 17" after typing "7").

The initial focus now records its own origin, `FocusOrigin::Open`. A text
part that gains focus that way selects its whole text whatever its codec's
caret. Tab and clicks keep their caret rules, and `focus_arrived_by_key`
still counts the open as an arrival by key, as before.

photocraft#15540, part of storytold/photocraft#15476.
This commit is contained in:
ljkx
2026-11-18 17:15:31 +01:00
parent 7a7f5302fa
commit 7be4487c58
6 changed files with 83 additions and 3 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ impl Focus for CxCore<'_> {
fn focus_arrived_by_key(&self) -> Option<PartRef> {
let facts = if focus::writes() == self.focus.at { self.focus } else { focus::facts(&self.ctx, self.id) };
self.focus_arrived().filter(|_| facts.arrived_origin == Some(focus::FocusOrigin::Request))
self.focus_arrived().filter(|_| matches!(facts.arrived_origin, Some(focus::FocusOrigin::Request | focus::FocusOrigin::Open)))
}
fn is_focused(&self) -> bool {
+6 -1
View File
@@ -465,7 +465,12 @@ pub(crate) fn run<C: Component>(
let back_from_menu = menu_held && state.held_selection.is_none();
if out.focused && !left_for_part && !was_focused && !back_from_menu {
let len = decl.shown.chars().count();
match decl.caret {
// A dialog that opens on this field selects its whole text whatever the codec's caret, so typing replaces it
// (photocraft#15540).
let opened = crate::runtime::focus::focused_rec(&ctx)
.and_then(|rec| crate::runtime::focus::origin(&ctx, &rec))
.is_some_and(|origin| origin == crate::runtime::focus::FocusOrigin::Open);
match if opened { Caret::All } else { decl.caret } {
Caret::All => adapter::select(&ctx, widget, 0, len),
_ => adapter::select(&ctx, widget, len, len),
}
+3
View File
@@ -636,6 +636,9 @@ pub(crate) enum FocusOrigin {
Press,
/// The keyboard (Tab, arrows, Page keys) or a focus request.
Request,
/// A scope shown for the first time moved focus to its initial stop (a dialog's first field): a request too, so
/// it counts as an arrival by key, and a text part selects its whole text so typing replaces it.
Open,
/// The focused stop went away and handed focus to its neighbour: neither the keyboard nor a request chose it, so
/// it is no arrival by key (a deleted guide selects no other guide).
Removal,
+1 -1
View File
@@ -641,7 +641,7 @@ pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec<Act> {
match pick.cloned() {
Some(rec) => {
state.entered = true;
state.focus_stop(rec)
state.focus_stop_from(rec, FocusOrigin::Open)
}
None => Vec::new(),
}
+44
View File
@@ -321,3 +321,47 @@ fn each_open_of_a_dialog_has_its_own_serial() {
assert_eq!(again, id, "the reopened dialog has the same id");
assert_ne!(host.open_serial(id), Some(first), "the reopened dialog has a serial of its own");
}
/// A dialog with a text field and a number field before any other control, holding `name` and `radius`.
fn first_field_dialog(name: Rc<RefCell<String>>, radius: Rc<Cell<Option<f64>>>, number_first: bool) -> Fixture {
use craft_ui::contract::Bind;
use craft_ui::text::{NumberField, TextField};
Fixture::new(move |ui| {
let mut open = true;
Dialog::new("New Layer")
.opened(Bind::bound(&mut open))
.buttons([FooterButton::new("OK", DialogRole::Accept), FooterButton::new("Cancel", DialogRole::Cancel)])
.show(ui, |ui, _| {
let mut text = name.borrow().clone();
let mut value = radius.get();
if number_first {
NumberField::new("Radius", Bind::bound(&mut value)).show(ui);
TextField::new("Name", Bind::bound(&mut text)).show(ui);
} else {
TextField::new("Name", Bind::bound(&mut text)).show(ui);
NumberField::new("Radius", Bind::bound(&mut value)).show(ui);
}
*name.borrow_mut() = text;
radius.set(value);
});
})
}
/// photocraft#15540: a dialog that opens with its first field focused selects that field's whole text, so typing
/// right away replaces the value, for a text field (New Layer's name) and a number field (Gaussian Blur's Radius).
#[trial]
fn typing_in_a_freshly_opened_dialog_replaces_its_first_field() {
let mut typed_into = Vec::new();
for number_first in [false, true] {
let name = Rc::new(RefCell::new("Layer 1".to_owned()));
let radius = Rc::new(Cell::new(Some(1.0)));
let mut fixture = first_field_dialog(name.clone(), radius.clone(), number_first);
fixture.settle();
assert!(fixture.focused().is_some(), "the first field has the focus (number first: {number_first})");
fixture.type_text("7");
fixture.idle(2);
typed_into.push(fixture.focused().and_then(|id| fixture.tree().value(id).map(str::to_owned)));
}
// Typing replaced the first field's text: the text field first, then the number field first.
assert_eq!(typed_into, [Some("7".to_owned()), Some("7".to_owned())]);
}
@@ -297,3 +297,31 @@ fn an_optional_number_is_held_to_its_bounds_and_remembered_while_off() {
toggle(&mut fixture);
assert_eq!(values.borrow().number("quality"), Some(7.0), "the box on again brings back the value typed before");
}
/// photocraft#15540: a parameter dialog opens with its first field's whole text selected, so typing right away replaces
/// the value: a text row (New Layer's Name) and a slider row's number (Gaussian Blur's Radius).
#[trial]
fn typing_in_a_freshly_opened_parameter_dialog_replaces_its_first_field() {
let values = Rc::new(RefCell::new(DialogFields::new()));
let shown = values.clone();
let mut fixture = Fixture::new(move |ui| {
let params = vec![
Param::new("name", "Name", ParamKind::text("Layer 1")),
Param::new("radius", "Radius", ParamKind::range(0.1, 1000.0, 1.0, SliderScale::Log)),
];
let _ = ParamDialog::new("newLayer", "New Layer", params).values(Bind::bound(&mut *shown.borrow_mut())).show(ui);
});
fixture.settle();
fixture.type_text("Sky").settle();
assert_eq!(values.borrow().text("name"), Some("Sky"), "typing replaced the name");
let values = Rc::new(RefCell::new(DialogFields::new()));
let shown = values.clone();
let mut fixture = Fixture::new(move |ui| {
let params = vec![Param::new("radius", "Radius", ParamKind::range(0.1, 1000.0, 1.0, SliderScale::Log))];
let _ = ParamDialog::new("gaussianBlur", "Gaussian Blur", params).values(Bind::bound(&mut *shown.borrow_mut())).show(ui);
});
fixture.settle();
fixture.type_text("7").key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(values.borrow().number("radius"), Some(7.0), "typing replaced the radius");
}