fix(forms): retain keyboard focus across successive text fields (#8994)

Co-authored-by: FredApps <[email protected]>
This commit is contained in:
Terra854andFredApps authored and GitHub committed 2026-12-04 07:36:33 +01:00
1 parent 6f3f26fb1b
commit e088e52679
2 files changed
+83 -1

No files matched your search

+5 -1
View File
@@ -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));
}
+78
View File
@@ -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 {