From 7be4487c58f233a0188206389ce3e331523d104f Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:50:50 +0100 Subject: [PATCH 1/2] 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. --- crates/ui-contract/src/contract/cx/focus.rs | 2 +- crates/ui-contract/src/contract/text.rs | 7 ++- crates/ui-contract/src/runtime/focus.rs | 3 ++ crates/ui-contract/src/runtime/focus/nav.rs | 2 +- crates/ui-panels/tests/it/dialogs.rs | 44 +++++++++++++++++++ .../tests/it/form_dialogs/params.rs | 28 ++++++++++++ 6 files changed, 83 insertions(+), 3 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/focus.rs b/crates/ui-contract/src/contract/cx/focus.rs index 887657105..418de3a47 100644 --- a/crates/ui-contract/src/contract/cx/focus.rs +++ b/crates/ui-contract/src/contract/cx/focus.rs @@ -70,7 +70,7 @@ impl Focus for CxCore<'_> { fn focus_arrived_by_key(&self) -> Option { 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 { diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index 5ba196b66..5e118532a 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -465,7 +465,12 @@ pub(crate) fn run( 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), } diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 2d90de6e4..e4615e280 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -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, diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index 1b4368e7d..f18541dba 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -641,7 +641,7 @@ pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec { match pick.cloned() { Some(rec) => { state.entered = true; - state.focus_stop(rec) + state.focus_stop_from(rec, FocusOrigin::Open) } None => Vec::new(), } diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index 52f8d83aa..f346aa19e 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -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>, radius: Rc>>, 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())]); +} diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index a7d573d93..bbeb493da 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -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"); +} From b5b1300783d61a58be37f28e1b723ccfb5327d4d Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 19 Nov 2026 00:55:45 +0100 Subject: [PATCH 2/2] Lazy::show_or: a view that leaves its room takes none next has been red since #2273 on two counts, both in the dock's settling host view (3bcc5bec4): - design-lint: contract/lazy.rs named `egui::LayerId` for the run's layer. The run now holds the runtime's `Layer`, the type `Place` and `replay::close` already use. - hovering_a_tab_runs_only_its_group: the dock ran its host view in a `scope_builder`, which takes room and item spacing in the Lazy's child even when the host leaves the group to the dock. A replayed group then sat 4 points lower in flow than a group that ran, so the child's extent changed whenever a hovered group ran, layout memory asked for a discard pass, and that pass ran every group (5 runs where 1 is expected). `show_or` now runs `add` in a child `Ui` that takes room only when it returns `Some`, so `otherwise` takes the same place whether it runs or replays. --- crates/ui-contract/src/contract/lazy.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index bc6ffb266..c65a6cf75 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -3,6 +3,7 @@ use crate::layout::{flow_region, take_in_flow}; use crate::runtime::egui::alloc; +use crate::runtime::egui::areas::Layer; use crate::runtime::env::Env; use crate::runtime::focus; use crate::runtime::layout_memory; @@ -100,12 +101,23 @@ impl Lazy { /// again in the next pass rather than replay it. A dock group's host view is one: where the host leaves the group /// to the dock, the dock's group shows in its place and settles by its own rules. `None` when the child did not /// run: it replayed a run of `add`, or was out of view. + /// + /// `add` runs in a `Ui` that takes room in the child only when it returns `Some`: an `add` that leaves its room + /// leaves the child as it found it, so `otherwise` takes the same place whether it runs or replays, and the child's + /// extent does not change between them (a change would cost the frame a discard pass). pub fn show_or(self, ui: &mut Ui, add: impl FnOnce(&mut Ui) -> Option, otherwise: impl FnOnce(&mut Ui) -> U) -> Option> { - self.run(ui, |ui, run| match add(ui) { - Some(shown) => Ok(shown), - None => { - run.unkept(ui.ctx()); - Err(otherwise(ui)) + self.run(ui, |ui, run| { + let mut first = alloc::child(ui, UiBuilder::new().max_rect(flow_region(ui)).layout(*ui.layout())); + match add(&mut first) { + Some(shown) => { + alloc::advance_past(ui, first.min_rect()); + Ok(shown) + } + None => { + drop(first); + run.unkept(ui.ctx()); + Err(otherwise(ui)) + } } }) } @@ -206,7 +218,7 @@ impl Lazy { struct Run { recording: bool, kept: bool, - layer: egui::LayerId, + layer: Layer, } impl Run {