diff --git a/crates/ui-egui/src/tools/forms_ui.rs b/crates/ui-egui/src/tools/forms_ui.rs index 29a645b878..a04c78ad16 100644 --- a/crates/ui-egui/src/tools/forms_ui.rs +++ b/crates/ui-egui/src/tools/forms_ui.rs @@ -162,12 +162,18 @@ fn fillable(f: &FormField) -> bool { !f.read_only() && !matches!(f.kind, FormFieldKind::PushButton | FormFieldKind::Signature) } -/// The fillable widgets in the order Tab and Shift+Tab visit them. +/// Whether the field takes the focus, on a click and as a Tab stop: one that can be filled in, or a +/// push button, as another viewer gives it the focus and a Tab stop too. +fn takes_focus(f: &FormField) -> bool { + fillable(f) || (f.kind == FormFieldKind::PushButton && !f.read_only()) +} + +/// The widgets that take the focus, in the order Tab and Shift+Tab visit them. pub fn tab_order(form: &[FormField], info: &DocInfo) -> Vec<(usize, usize)> { let mut order: Vec<(usize, usize, usize)> = form .iter() .enumerate() - .filter(|(_, field)| fillable(field)) + .filter(|(_, field)| takes_focus(field)) .flat_map(|(fi, field)| { field .widgets @@ -477,6 +483,16 @@ pub(crate) fn field_press( } } +/// Presses the push button `f` (its widget `w`): queues its action, or says it has none. +fn press_button(session: &mut PageSession, f: &FormField, w: &FormWidget) { + match &f.button { + Some(a) => session.forms.button = Some((f.name.clone(), pdfcraft_engine::js::field_object_id(f.obj), a.clone())), + // Its press already ran its mouse-down action. + None if w.mouse_down => {} + None => session.forms.notice = Some(FormNotice::NoAction(f.name.clone())), + } +} + /// What a click at `at` on widget `wi` of `f` (field index `field`, widget `w`) does, with `mods` held. fn clicked( session: &mut PageSession, @@ -494,12 +510,16 @@ fn clicked( } match f.kind { _ if f.read_only() || w.read_only => session.forms.notice = Some(FormNotice::ReadOnly(f.name.clone())), - FormFieldKind::PushButton => match &f.button { - Some(a) => session.forms.button = Some((f.name.clone(), pdfcraft_engine::js::field_object_id(f.obj), a.clone())), - // Its press already ran its mouse-down action. - None if w.mouse_down => {} - None => session.forms.notice = Some(FormNotice::NoAction(f.name.clone())), - }, + FormFieldKind::PushButton => { + // The button takes the focus, as a check box does: what it leaves commits and blurs first, + // and Tab goes on from it. + if session.forms.focus.as_ref().is_none_or(|x| !x.is_on(f, wi)) { + commit(session, form); + session.forms.focus = Some(open_focus(f, wi)); + } + session.forms.refocus = true; + press_button(session, f, w); + } FormFieldKind::Text if let Some(script) = &w.mouse_up_script => { // Close the editor so the script can replace its value without a stale draft. commit(session, form); @@ -624,6 +644,13 @@ pub(crate) fn field_key(session: &mut PageSession, info: &DocInfo, form: &[FormF None } Intent::Tab => tab_to_next_field(session, info, form, f, widget, true, geo), + // Space or Enter presses the focused push button, as a click's release does. + Intent::Activate | Intent::Space if !read_only && f.kind == FormFieldKind::PushButton => { + if let Some(w) = f.widgets.get(widget) { + press_button(session, f, w); + } + None + } Intent::Activate | Intent::Space if !read_only => match f.kind { FormFieldKind::Radio => select_edit(f, widget), FormFieldKind::CheckBox => toggle_edit(f, widget), diff --git a/crates/ui-egui/tests/it/buttons.rs b/crates/ui-egui/tests/it/buttons.rs index 993128dfcd..8de6f28615 100644 --- a/crates/ui-egui/tests/it/buttons.rs +++ b/crates/ui-egui/tests/it/buttons.rs @@ -154,7 +154,8 @@ fn hide_actions_hide_and_show_fields() { assert!(name_hidden(&h), "the Hide action hid the field"); // A hidden field takes no clicks: clicking where it was doesn't start editing it. click_field(&mut h, "name"); - assert!(h.state().views[0].session.forms.focus.is_none(), "a hidden field can't be focused"); + // (The push button clicked before it still has the focus, as a check box would.) + assert!(h.state().views[0].session.forms.focus.as_ref().is_none_or(|f| f.name != "name"), "a hidden field can't be focused"); click_field(&mut h, "show"); assert!(!name_hidden(&h), "the Hide action with /H false showed it again"); } diff --git a/crates/ui-egui/tests/it/forms.rs b/crates/ui-egui/tests/it/forms.rs index 0bbfaa35b4..862b601f20 100644 --- a/crates/ui-egui/tests/it/forms.rs +++ b/crates/ui-egui/tests/it/forms.rs @@ -2070,6 +2070,33 @@ fn the_field_editor_sizes_auto_size_text_as_the_page_does() { const GUIDE_INNER: &str = "%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n\ 3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] >> endobj\ntrailer << /Root 1 0 R >>\n%%EOF"; +/// A push button takes the focus on a click and is a Tab stop, as another viewer treats it, so its +/// On Focus and On Blur run; Space presses the focused button. +#[test] +fn a_push_button_takes_the_focus_on_a_click_and_as_a_tab_stop() { + let bytes = pdf_from_objects(&[ + "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R 5 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 (a) /Rect [20 330 120 356] /P 3 0 R /F 4 >>", + "<< /Type /Annot /Subtype /Widget /FT /Btn /Ff 65536 /T (go) /Rect [20 280 120 306] /P 3 0 R /F 4 /A << /S /JavaScript /JS (this.getField\\(\"a\"\\).value = \"pressed\";) >> >>", + ]); + let mut h = harness_with_bytes("button-focus.pdf", bytes); + click_field(&mut h, "a", 0); + assert_eq!(focused(&h), Some(("a".into(), 0))); + h.key_press(egui::Key::Tab); + h.run_steps(4); + assert_eq!(focused(&h), Some(("go".into(), 0)), "Tab reaches the push button"); + assert_eq!(value(&h, "a"), Vec::::new(), "and does not press it"); + h.key_press(egui::Key::Space); + h.run_steps(4); + assert_eq!(value(&h, "a"), ["pressed"], "Space presses the focused button"); + click_field(&mut h, "a", 0); + assert_eq!(focused(&h), Some(("a".into(), 0))); + click_field(&mut h, "go", 0); + assert_eq!(focused(&h), Some(("go".into(), 0)), "a click focuses the push button"); +} + /// A page with a push button `go`, and three attached files: a one-page PDF filed under the key /// `Guide.pdf` (its own file name `inner.pdf`), `notes.txt`, and `fake.pdf`, which holds text. fn guide_fixture() -> Vec { diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml index 66982974df..753fdc4154 100644 --- a/parity/acrobat-features.toml +++ b/parity/acrobat-features.toml @@ -4697,8 +4697,8 @@ milestone = "M6" status = "shipped" commands = ["form.fields"] tools = [] -evidence = ["crates/ui-egui/tests/it/forms.rs::tabbing_into_a_filled_field_selects_it_so_typing_replaces"] -notes = "Tab / Shift+Tab move between text and choice fields in field order; entering with Tab selects the value" +evidence = ["crates/ui-egui/tests/it/forms.rs::tabbing_into_a_filled_field_selects_it_so_typing_replaces", "crates/ui-egui/tests/it/forms.rs::a_push_button_takes_the_focus_on_a_click_and_as_a_tab_stop"] +notes = "Tab / Shift+Tab move between the fields that take the focus (text, choice and push button fields) in field order; entering with Tab selects the value; a click focuses a push button, and Space or Enter presses it" [[feature]] id = "form.appearance-regeneration"