PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(ui): a click on a link or a form button counts only released over it
alehel2929ba854e5a
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 79c95d049..03bfa167a 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1737,12 +1737,14 @@ fn page_overlays(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_paint_field_highlights(painter, &xf, info, form.as_slice(), i, view.highlight_fields);// Links: the hand and the link's target, which count only for the tools that follow or// edit links (`Layer::reached_by`). Tips draw above the palette, so none shows while it is- // open, even beside it.+ // open, even beside it. A click follows the link its press was on, released over it: one that+ // slips off the link first follows nothing, as a button's does.if unobstructed&& resp.clicked()&& tool == QuickTool::Select&& !input.consumed- && let Some(crate::page_router::Owner::Link { index }) = view.router.owner(i)+ && let Some(owner @ crate::page_router::Owner::Link { index }) = view.router.owner(i)+ && pointer.and_then(|p| link_hit(info, &xf, i, p)).is_some_and(|h| h.owner == owner)&& let Some(l) = info.links.get(index){frame.out.clicked_link = Some(l.target.clone());diff --git a/crates/ui-egui/src/tools/forms_ui.rs b/crates/ui-egui/src/tools/forms_ui.rsindex 077ad8550..7232b1169 100644--- a/crates/ui-egui/src/tools/forms_ui.rs+++ b/crates/ui-egui/src/tools/forms_ui.rs@@ -208,7 +208,10 @@ pub(crate) fn page_input(cx: PageToolContext<'_>, form: &[FormField], allowed: bview.forms.focus = Some(Focus { press: Some(at), ..open_focus(f, wi) });view.forms.pressed = true;}- if !resp.clicked() {+ // A click counts released over the widget its press was on, as a button's does: one that slips+ // off it first does nothing (a check box's overlay closes on it too).+ if !resp.clicked() || !ptr.pos.and_then(|p| hit(xf, info, page, form, allowed, p)).is_some_and(|h| h.owner == Owner::Field { field, widget: wi })+ {return true;}if !allowed {diff --git a/crates/ui-egui/tests/it/pointer.rs b/crates/ui-egui/tests/it/pointer.rsindex 0d058cb45..889caf4c1 100644--- a/crates/ui-egui/tests/it/pointer.rs+++ b/crates/ui-egui/tests/it/pointer.rs@@ -132,6 +132,16 @@ fn highlighted_checkbox_pdf() -> Vec<u8> {])}+/// A 300×400 page with a push button at x 100..200, y 200..220 whose action opens a web address.+fn button_pdf() -> Vec<u8> {+ pdf(&[+ "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R] /DA (/Helv 0 Tf 0 g) >> >>",+ PAGES,+ "<< /Type /Page /Parent 2 0 R /Annots [4 0 R] >>",+ "<< /Type /Annot /Subtype /Widget /FT /Btn /Ff 65536 /T (web) /Rect [100 200 200 220] /P 3 0 R /A << /S /URI /URI (https://example.org/) >> >>",+ ])+}+/// The app with `pdf` open at `zoom` percent and its first frames run. The left panel stays/// open only where a test places the page relative to the window's chrome.fn open(pdf: Vec<u8>, zoom: &'static str, left_open: bool) -> Harness<'static, PdfCraftApp> {@@ -725,13 +735,46 @@ fn field_value(h: &Harness<'_, PdfCraftApp>, name: &str) -> Vec<String> {fn a_press_on_one_check_box_released_over_another_ticks_neither() {let mut h = open(two_checkboxes_pdf(), "150", false);let (press, release) = (at(&h, 119.0, 210.0), at(&h, 121.0, 210.0));+ slipping_click(&mut h, press, release);+ assert_eq!((field_value(&h, "agree"), field_value(&h, "other")), (Vec::<String>::new(), Vec::new()), "neither box is ticked");+}++/// A click whose pointer slips from `press` to `release` before the button comes up (a click+/// still, to egui, while they are close), in frames of their own as [`mouse_click`] sends them.+fn slipping_click(h: &mut Harness<'_, PdfCraftApp>, press: egui::Pos2, release: egui::Pos2) {h.hover_at(press);h.run_steps(1);h.drag_at(press);h.run_steps(1);h.drop_at(release);h.run_steps(4);- assert_eq!((field_value(&h, "agree"), field_value(&h, "other")), (Vec::<String>::new(), Vec::new()), "neither box is ticked");+}++/// A click follows the link its press was on only when released over it: pressed just inside the+/// link's edge and released just past it, it follows nothing, as a button pressed and released off+/// itself does nothing. The press used to be enough.+#[test]+fn a_press_on_a_link_released_off_it_follows_nothing() {+ let mut h = open(link_pdf(), "150", false);+ // The link ends at x 250.+ let press = at(&h, 249.0, 325.0);+ slipping_click(&mut h, press, press + egui::vec2(5.0, 0.0));+ assert_eq!(h.state().pending_link, None, "nothing asks to open the link");+ mouse_click(&mut h, press);+ assert!(h.state().pending_link.is_some(), "a click there asks to open it");+}++/// The same for a push button: pressed just inside its edge and released just past it, it runs no+/// action. The press used to be enough.+#[test]+fn a_press_on_a_button_released_off_it_runs_nothing() {+ let mut h = open(button_pdf(), "150", false);+ // The button ends at x 200.+ let press = at(&h, 199.0, 210.0);+ slipping_click(&mut h, press, press + egui::vec2(5.0, 0.0));+ assert_eq!(h.state().pending_link, None, "the button's action does not run");+ mouse_click(&mut h, press);+ assert!(h.state().pending_link.is_some(), "a click there runs it");}/// Text markup lies on the text, under a form field: a click on a check box under a highlight