From e088e526794f8bbb44f8276ddf2b7eee62987d2a Mon Sep 17 00:00:00 2001 From: Terra854 Date: Fri, 4 Dec 2026 14:36:33 +0800 Subject: [PATCH] fix(forms): retain keyboard focus across successive text fields (#8994) Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/ui-egui/src/canvas.rs | 6 ++- crates/ui-egui/tests/it/forms.rs | 78 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index bd1a061816..b56530c4f1 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1474,8 +1474,12 @@ fn page_body(ui: &mut egui::Ui, body: &PageBody<'_>, callbacks: &mut PageCallbac } // The focused field's editor, and the Fill & Sign text being typed, over their pages. if let Some(editor) = crate::forms_ui::editor::show(ui, body, session, cx.doc, cx.form, cx.info, cx.today, cx.geometry(body)) - && *field_arrived + && session.forms.focus.is_some() + && (*field_arrived || focus::is_focused(ui.ctx(), parts_out.response.id) || ui.memory(|memory| memory.focused().is_none())) { + // show also reports an editor it just closed; do not request that closed editor's focus. + // A field selected while the previous editor commits can miss its arrival report. Complete + // that handoff while the page (or no control) owns focus, without taking it from another control. // The keys of a field that takes typing are its editor's: it takes the focus over from the field's part. focus::request(ui.ctx(), FocusTarget::Id(editor)); } diff --git a/crates/ui-egui/tests/it/forms.rs b/crates/ui-egui/tests/it/forms.rs index 6c55d33d9e..0bbfaa35b4 100644 --- a/crates/ui-egui/tests/it/forms.rs +++ b/crates/ui-egui/tests/it/forms.rs @@ -337,6 +337,84 @@ fn typing_into_a_text_field_and_tabbing_on() { assert!(h.state().views[0].session.forms.focus.is_none()); } +/// An original synthetic AcroForm with two consecutive, unrestricted text fields. +fn text_fields_harness() -> Harness<'static, PdfCraftApp> { + harness_with_bytes( + "text-fields.pdf", + pdf_from_objects(&[ + "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R 5 0 R] /DA (/Helv 12 Tf 0 g) /DR << /Font << /Helv 6 0 R >> >> >> >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 300 400] >>", + "<< /Type /Page /Parent 2 0 R /Annots [4 0 R 5 0 R] >>", + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (first) /Rect [20 250 180 275] /P 3 0 R /F 4 >>", + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (last) /Rect [20 200 180 225] /P 3 0 R /F 4 >>", + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + ]), + ) +} + +/// #8953: committing one text editor must leave the next field able to receive text. +#[test] +fn consecutive_text_fields_keep_keyboard_focus_and_saved_values() { + for tab in [false, true] { + let mut h = text_fields_harness(); + click_field(&mut h, "first", 0); + h.event(egui::Event::Text("alpha".into())); + h.run_steps(2); + if tab { + h.key_press(egui::Key::Tab); + h.run_steps(4); + } else { + click_field(&mut h, "last", 0); + } + assert_eq!(focused(&h), Some(("last".into(), 0)), "Tab: {tab}"); + assert!( + h.query_by(|node| node.is_focused() && node.label().as_deref() == Some("last")).is_some(), + "the second editor has keyboard focus; Tab: {tab}" + ); + h.event(egui::Event::Text("beta".into())); + h.run_steps(2); + h.key_press(egui::Key::Enter); + h.run_steps(4); + assert_eq!(value(&h, "first"), ["alpha"], "Tab: {tab}"); + assert_eq!(value(&h, "last"), ["beta"], "Tab: {tab}"); + let app = h.state(); + let bytes = app.session.save_bytes(app.views[0].id).unwrap(); + let mut reopened = pdfcraft_engine::Session::new(); + let id = reopened.open("saved.pdf", None, bytes, None).unwrap(); + for (name, expected) in [("first", "alpha"), ("last", "beta")] { + assert_eq!(reopened.get(id).unwrap().form.iter().find(|field| field.name == name).unwrap().value, [expected], "Tab: {tab}"); + } + } +} + +#[test] +fn an_open_form_editor_preserves_intentional_focus_in_another_control() { + let mut h = check_box_harness(); + click_field(&mut h, "first", 0); + h.event(egui::Event::Text("alpha".into())); + h.run_steps(2); + h.get_by_role_and_label(egui::accesskit::Role::SpinButton, "Zoom").focus(); + h.run_steps(4); + assert!(h.get_by_role_and_label(egui::accesskit::Role::SpinButton, "Zoom").is_focused()); +} + +#[test] +fn enter_and_escape_close_the_form_editor_without_reopening_it() { + for key in [egui::Key::Enter, egui::Key::Escape] { + let mut h = check_box_harness(); + click_field(&mut h, "first", 0); + h.event(egui::Event::Text("alpha".into())); + h.run_steps(2); + h.key_press(key); + h.run_steps(4); + assert!(focused(&h).is_none(), "{key:?}"); + h.event(egui::Event::Text("ignored".into())); + h.run_steps(4); + assert!(focused(&h).is_none(), "{key:?}"); + assert_eq!(value(&h, "first"), if key == egui::Key::Enter { vec!["alpha".to_string()] } else { vec![] }); + } +} + /// One step of a row of `field_keys_and_clicks_set_field_values`. #[derive(Clone, Copy)] enum Act {