From 7e00c61c7fffda354f44f67832d50d57964d4dcb Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:20:30 -0400 Subject: [PATCH] M12: the editor follows each focus script in turn, so an exit then an enter that both change the field leave it on the last value --- crates/ui-egui/src/editing.rs | 8 +++++--- crates/ui-egui/tests/it/xfa.rs | 12 +++++++++++- crates/xfa/src/fixtures.rs | 4 +++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/editing.rs b/crates/ui-egui/src/editing.rs index 34c9fac2f3..5266bebfbb 100644 --- a/crates/ui-egui/src/editing.rs +++ b/crates/ui-egui/src/editing.rs @@ -485,11 +485,13 @@ impl PdfCraftApp { } let left = std::mem::replace(&mut view.forms.focused, now.clone()); let id = view.id; - // The edited field's value before the scripts, to tell its untouched draft from typing. - let before: Option> = - now.as_ref().and_then(|(name, _)| self.session.get(id)?.form.iter().find(|f| &f.name == name).map(|f| f.value.clone())); + let edited = now.as_ref().map(|(name, _)| name.clone()); use pdfcraft_engine::js::FieldFocus; for ((name, widget), focus) in [left.map(|l| (l, FieldFocus::Exit)), now.map(|n| (n, FieldFocus::Enter))].into_iter().flatten() { + // The edited field's value before this script, to tell its untouched draft from typing: + // the exit of the field left may have changed it already, and the draft with it. + let before: Option> = + edited.as_ref().and_then(|e| self.session.get(id)?.form.iter().find(|f| &f.name == e).map(|f| f.value.clone())); match self.session.field_focus(id, &name, widget, focus) { Ok(changed) => { if changed diff --git a/crates/ui-egui/tests/it/xfa.rs b/crates/ui-egui/tests/it/xfa.rs index e98d3df14e..120fe2cc2e 100644 --- a/crates/ui-egui/tests/it/xfa.rs +++ b/crates/ui-egui/tests/it/xfa.rs @@ -190,7 +190,8 @@ fn xfa_enter_and_exit_scripts_run_whenever_the_focus_moves() { /// An enter script that changes the field it enters (clearing a "Type here") shows in the editor, /// so leaving the field keeps the script's value rather than writing the old one back; typing -/// after it is kept as usual. +/// after it is kept as usual. When the exit of the field left changed it first (`relay` puts the +/// "Type here" back), the editor follows both scripts and ends on the enter script's value. #[test] fn an_enter_script_that_changes_its_field_shows_in_the_editor() { use egui::{Key, Modifiers}; @@ -213,6 +214,15 @@ fn an_enter_script_that_changes_its_field_shows_in_the_editor() { h.key_press_modifiers(Modifiers::NONE, Key::Tab); h.run_steps(4); assert_eq!(value(&h), "Ann"); + // From `relay` back to `hint`: relay's exit puts "Type here" back over "Ann", then hint's enter + // clears it. The editor follows both, so it holds the last value, and Tab keeps it. + click_field(&mut h, "relay"); + click_field(&mut h, "hint"); + assert_eq!(value(&h), "", "relay's exit, then hint's enter"); + assert_eq!(h.state().views[0].forms.focus.as_ref().map(|f| f.text.clone()).as_deref(), Some(""), "the editor holds the enter script's value"); + h.key_press_modifiers(Modifiers::NONE, Key::Tab); + h.run_steps(4); + assert_eq!(value(&h), ""); } #[test] diff --git a/crates/xfa/src/fixtures.rs b/crates/xfa/src/fixtures.rs index 885ef9831c..4b116a9aa3 100644 --- a/crates/xfa/src/fixtures.rs +++ b/crates/xfa/src/fixtures.rs @@ -662,7 +662,8 @@ if (this.rawValue == "1") { /// A form whose text field `probe` counts its enters in `enters` and its exits in `exits`, whose /// text field `quiet` only shows a message when it is left, and whose radio button `size.large` /// counts its enters in `picks` and its exits in `drops` (each button of a radio group is a field -/// with scripts of its own), and whose text field `hint` clears its "Type here" when it gains focus. +/// with scripts of its own), whose text field `hint` clears its "Type here" when it gains focus, and +/// whose text field `relay` puts that "Type here" back in `hint` when it loses focus. pub fn focus_template() -> String { let js = "application/x-javascript"; let count = |name: &str| format!(r#""#); @@ -682,6 +683,7 @@ pub fn focus_template() -> String { {}{}{}{} Type here +