Forms: a push button takes the focus on a click and as a Tab stop (#4589)

* M6: a field's mouse and focus actions run as the pointer and the focus move

Every trigger of ISO 32000-2 Table 197 runs its action: Mouse Enter and Exit as the pointer arrives over a widget and leaves it, Mouse Down on a press (before the field the focus leaves commits), On Blur and On Focus with #4359's focus scripts, and Mouse Up on the release once they have run, which a push button's click now waits for too. Order and event.name values measured black-box on a synthetic form. forms::widget_action reads any trigger's action; Session::run_field_script runs a script as that field event; form_trigger runs one headlessly.

* M6: only a click's field actions may print, open links or Save As

A field's Mouse Enter, Mouse Exit, Focus and Blur actions run without anyone asking: their scripts' requests to print, open a link or Save As, or show a side panel are dropped and noted on the console, and an action that runs a menu item, opens a link, submits or picks a picture doesn't run. Mouse Down and Up, a click, keep them.

* M6: a trigger's reset or show/hide keeps the typing a press committed

A press commits the field it leaves into the view's queued edit, and a Mouse Down reset or show/hide action replaced that queued edit, losing the typing. A trigger's reset or show/hide now applies at once, and the queued commit applies after it, as another viewer orders a press's Mouse Down before the field left commits. action_edit builds the edit for both a push button's click and a trigger.

* M6: a push button takes the focus on a click and as a Tab stop

Another viewer gives a push button the focus when it is clicked, and stops at it on Tab (probe 10: Tab from a text field reached the button; a click on it ran Mouse Down, the field left's Blur, its Focus, then Mouse Up). PdfCraft's push buttons took none, so their On Focus and On Blur never ran and Tab skipped them. A click now focuses the button as it does a check box, Tab and Shift+Tab include push buttons, and Space or Enter presses the focused button. The check box's keyboard holder is shared as hold_keys.

* M6: a test that XFA focus scripts may not print, open links or Save As

The XFA runner already drops these for any activity but a click; the test holds enter and exit to the same rule as an AcroForm field's Focus and Blur.

* M6: hovering a form that may not be filled in runs none of its actions

Mouse Enter and Exit ran where the document's security forbids filling in the form, as Mouse Down and the other actions don't: a script changing a value was refused on every pass, with a message each time. The hover is noted only where the form may be filled in.

* M6: a reset by a field's action shows in the editor of the field being edited, as a script's change does

* M6: a push button scrolled out of view lets go of the focus, as a check box does
This commit is contained in:
Eric Kaden authored and GitHub committed 2026-12-04 14:42:36 +01:00
1 parent 640ce697e8
commit 30dfab44ff
4 files changed
+66 -11

No files matched your search

+35 -8
View File
@@ -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),
+2 -1
View File
@@ -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");
}
+27
View File
@@ -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::<String>::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<u8> {
+2 -2
View File
@@ -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"