PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(ui): the Hand tool moves the page with the pointer (#924) (#3017)
Guillem Hermida233767d181c2
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 8fd54db90..a5ebf3e38 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -582,6 +582,8 @@ pub struct DocView {pub(crate) last_scroll: Vec2,/// Side-by-side compare: scroll to here this frame, following the other half.pub(crate) sync_scroll: Option<Vec2>,+ /// The page widget's id, by which the Hand tool finds its drag before the pages are laid out.+ page_widget: Option<egui::Id>,pan_hold_until: f64,/// Pages whose raster proved slow (see `SLOW_MILLIS`): only these get the zoom/pan/// settle holds. Fast pages render straight through, with no added latency.@@ -777,6 +779,7 @@ impl DocView {sharp_hold_until: 0.0,last_scroll: Vec2::ZERO,sync_scroll: None,+ page_widget: None,pan_hold_until: 0.0,slow_pages: HashSet::new(),comments: Default::default(),@@ -2578,6 +2581,25 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {if !std::mem::replace(&mut view.shown, true) {scroll = scroll.scroll_offset(Vec2::ZERO);}+ // The Hand tool pans here, before the pages are laid out, so the page moves with the pointer+ // in the frame the pointer moves (#924). Scrolling by the drag from inside the area moved the+ // page only after that frame was drawn, a frame behind. The frame egui tells the drag from a+ // click, the page catches up with all the pointer has moved since the press. A jump asked for+ // below still wins.+ if app.quick_tool == QuickTool::Hand+ && !middle_gesture+ && let Some(id) = view.page_widget.filter(|id| ui.ctx().is_being_dragged(*id))+ {+ let moved = if ui.ctx().drag_started_id() == Some(id) {+ ui.input(|i| i.pointer.total_drag_delta()).unwrap_or(Vec2::ZERO)+ } else {+ ui.input(|i| i.pointer.delta())+ };+ // Within the content, as egui keeps it: past an end the page would show there for a+ // frame and spring back. `max` is never NaN or below zero, so the clamp can't panic.+ let max = vec2((content_w - avail.width()).max(0.0), (content_h - avail.height()).max(0.0));+ scroll = scroll.scroll_offset((view.last_scroll - moved).clamp(Vec2::ZERO, max));+ }if let Some(v) = view.sync_scroll.take() {scroll = scroll.scroll_offset(v);}@@ -2723,14 +2745,14 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());}let (resp_rect, resp) = ui.allocate_exact_size(vec2(content_w, content_h), Sense::click_and_drag());+ view.page_widget = Some(resp.id);// A drag lasts only while egui reports it, so the mark can't outlive one.if !resp.dragged_by(egui::PointerButton::Primary) {view.selection_drag = false;}- // The Hand tool pans: the content widget takes every drag, so scroll by its delta.+ // The Hand tool pans by this widget's drag, before the pages are laid out (above).if hand {if resp.dragged() {- ui.scroll_with_delta(resp.drag_delta());ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);} else if resp.hovered() {ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);diff --git a/crates/ui-egui/tests/view.rs b/crates/ui-egui/tests/view.rsindex 3f4420d3b..5ea5452fa 100644--- a/crates/ui-egui/tests/view.rs+++ b/crates/ui-egui/tests/view.rs@@ -1046,6 +1046,47 @@ fn the_hand_tool_pans_by_dragging() {assert!(h.state().views[0].selected_text().is_none(), "no text selection with the hand");}+#[test]+fn the_hand_tool_moves_the_page_with_the_pointer() {+ // With the Hand tool the page stays under the pointer, point for point, in the frame the+ // pointer moves (#924, #1992). The drag used to reach the scroll offset only after the frame+ // was drawn (and, before the page view stopped animating its scrolls, eased in over several+ // frames), so the page trailed behind the pointer; the points moved before egui tells a drag+ // from a click were lost on top.+ let mut h = harness_stepping(1.0 / 60.0, &[("layout", "continuous"), ("zoom", "600"), ("quick", "hand")]);+ let start = h.state().views[0].viewport_rect().center();+ h.hover_at(start);+ h.run_steps(2);+ let page = |h: &Harness<'static, PdfCraftApp>| rect(h, 0).expect("page 1 on screen").min;+ let grabbed = page(&h);+ let click_dist = h.ctx.options(|o| o.input_options.max_click_dist);+ let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE };+ h.event(button(start, true));+ h.step();+ // A slow start, two points a frame, then quick strokes both ways. Once the pointer is further+ // than a click may wander, the page has moved exactly as far as it, in every frame.+ let mut pointer = start;+ let strokes = [egui::vec2(-2.0, -2.0); 5].into_iter().chain([egui::vec2(-40.0, -150.0), egui::vec2(25.0, 60.0)]);+ for stroke in strokes {+ pointer += stroke;+ h.event(egui::Event::PointerMoved(pointer));+ h.step();+ if (pointer - start).length() > click_dist {+ assert_eq!(page(&h) - grabbed, pointer - start, "the page keeps up with the pointer in the same frame");+ }+ }+ // Held still, it stays: nothing is left to ease in.+ h.run_steps(30);+ assert_eq!(page(&h) - grabbed, pointer - start, "nothing moves the page once the pointer stops");+ // Let go mid-stroke, it stops with the pointer: the Hand tool doesn't fling the page on.+ pointer += egui::vec2(0.0, -30.0);+ h.event(egui::Event::PointerMoved(pointer));+ h.step();+ h.event(button(pointer, false));+ h.run_steps(30);+ assert_eq!(page(&h) - grabbed, pointer - start, "the page stays where it was let go");+}+#[test]fn required_fields_get_a_red_border_when_highlighting() {let _gpu = gpu();diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.tomlindex 71b5725f9..9b0266053 100644--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -1070,8 +1070,8 @@ milestone = "M3"status = "shipped"commands = []tools = []-evidence = ["crates/ui-egui/tests/view.rs::the_hand_tool_pans_by_dragging", "crates/ui-egui/tests/viewer.rs::v_h_and_space_pick_the_quick_tools"]-notes = "drag to pan; Grab/Grabbing cursors; H picks it, V goes back to Select, Space held pans temporarily"+evidence = ["crates/ui-egui/tests/view.rs::the_hand_tool_pans_by_dragging", "crates/ui-egui/tests/view.rs::the_hand_tool_moves_the_page_with_the_pointer", "crates/ui-egui/tests/viewer.rs::v_h_and_space_pick_the_quick_tools"]+notes = "drag to pan: the page follows the pointer point for point in the frame it moves, and stops where it is let go (no fling); Grab/Grabbing cursors; H picks it, V goes back to Select, Space held pans temporarily. Checked with kittest pointer events, not yet on a desktop"[[feature]]id = "view.rotate-view"