PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Text selection: dragging to the top or bottom edge of the view scrolls it (part of #1670) (#1675)
Marco Arnulfo740117620db8
diff --git a/crates/ui-egui/src/autoscroll.rs b/crates/ui-egui/src/autoscroll.rsindex 139bdaed8..7eea563ee 100644--- a/crates/ui-egui/src/autoscroll.rs+++ b/crates/ui-egui/src/autoscroll.rs@@ -1,6 +1,7 @@//! Middle-button scrolling for the document viewport: Linux latches auto-scroll, other//! platforms pan while the button is held. Either way the page tools never see the button,-//! because egui's drag responses accept every pointer button.+//! because egui's drag responses accept every pointer button. Also the edge scrolling of a+//! text-selection drag.use egui::{Context, CursorIcon, Event, Key, PointerButton, Pos2, Stroke, Vec2, vec2};@@ -187,6 +188,27 @@ impl AutoScroll {}}+/// Within this distance of the viewport's top or bottom edge, a text-selection drag scrolls.+const EDGE: f32 = 30.0;+/// Points per second for each point the pointer is into the edge band or past the viewport.+const EDGE_SPEED: f32 = 20.0;++/// How far a selection drag scrolls this frame with the pointer at `y`, near or past the top or+/// bottom of `viewport` (#1670); faster the farther it goes. egui's delta moves the content.+pub(crate) fn edge_delta(y: f32, viewport: egui::Rect, dt: f32) -> f32 {+ if !y.is_finite() || !dt.is_finite() || viewport.height() < 4.0 * EDGE {+ return 0.0;+ }+ let distance = if y > viewport.bottom() - EDGE {+ y - (viewport.bottom() - EDGE)+ } else if y < viewport.top() + EDGE {+ y - (viewport.top() + EDGE)+ } else {+ return 0.0;+ };+ -distance.clamp(-MAX_DISPLACEMENT, MAX_DISPLACEMENT) * EDGE_SPEED * dt.clamp(0.0, 0.05)+}+fn scroll_delta(displacement: f32, dt: f32) -> f32 {if !displacement.is_finite() || !dt.is_finite() {return 0.0;@@ -281,6 +303,20 @@ mod tests {assert!(!scroll.escape(&ctx));}+ #[test]+ fn edge_delta_scrolls_toward_the_edge_the_pointer_is_at() {+ let viewport = egui::Rect::from_min_max(egui::pos2(0.0, 100.0), egui::pos2(400.0, 500.0));+ assert_eq!(edge_delta(300.0, viewport, 0.016), 0.0, "nothing away from the edges");+ assert!(edge_delta(495.0, viewport, 0.016) < 0.0, "the bottom edge moves the content up");+ assert!(edge_delta(105.0, viewport, 0.016) > 0.0, "the top edge moves it down");+ assert!(edge_delta(600.0, viewport, 0.016) < edge_delta(495.0, viewport, 0.016), "faster past the viewport");+ for invalid in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {+ assert_eq!(edge_delta(invalid, viewport, 0.016), 0.0);+ assert_eq!(edge_delta(495.0, viewport, invalid), 0.0);+ }+ assert!(edge_delta(f32::MAX, viewport, 0.016).is_finite());+ }+#[test]fn chromium_curve_is_gentle_near_the_anchor_and_accelerates_farther_away() {// Reference speeds in screen points/second from Chromium's distance exponent (2.2),diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 45f5fda84..959337e2f 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3055,6 +3055,21 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());}}+ // Held at the top or bottom edge, or dragged past it, a text selection scrolls the view+ // so it can reach text off screen (#1670); the selection follows the pointer on its page.+ if view.selection_drag+ && navigation_enabled+ && unobstructed+ && !single_paging+ && let Some(p) = ui.input(|i| i.pointer.latest_pos())+ {+ let dy = crate::autoscroll::edge_delta(p.y, avail, ui.input(|i| i.stable_dt));+ if dy != 0.0 {+ ui.scroll_with_delta_animation(vec2(0.0, dy), egui::style::ScrollAnimation::none());+ // Keep scrolling while the pointer rests there and sends no events.+ ui.ctx().request_repaint();+ }+ }// The page navigated to stays current while no page shows more of itself, so the next// page step starts from it rather than from a page further down the screen (#188).// It also stays current while it is wholly on screen: a short page (a cheque) gone todiff --git a/crates/ui-egui/tests/ui_group/text_select.rs b/crates/ui-egui/tests/ui_group/text_select.rsindex a98b2f8ed..b77d7f05a 100644--- a/crates/ui-egui/tests/ui_group/text_select.rs+++ b/crates/ui-egui/tests/ui_group/text_select.rs@@ -267,3 +267,50 @@ fn a_selection_drag_keeps_scrolling_over_the_gap_and_the_next_page() {h.run_steps(2);assert_eq!(h.state().views[0].selected_text(), Some(held), "releasing keeps the selection");}++/// One 300×600 page with a line of Helvetica text at its top and another at its bottom.+fn tall_page() -> Vec<u8> {+ let content = "BT /F1 14 Tf 20 570 Td (Top line) Tj 0 -550 Td (Bottom line) Tj ET";+ format!(+ "%PDF-1.7+1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj+2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj+3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 600] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >> endobj+4 0 obj << /Length {} >> stream+{content}+endstream endobj+5 0 obj << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> endobj+trailer << /Root 1 0 R >>+%%EOF",+ content.len()+ )+ .into_bytes()+}++/// Issue #1670: holding a selection drag at the bottom edge of the view scrolls it, and the+/// selection follows the pointer down to text that was off screen.+#[test]+fn a_selection_drag_held_at_the_bottom_edge_scrolls_the_view() {+ let (mut h, _) = harness_with(tall_page(), |app| {+ app.set_option("layout", "continuous").unwrap();+ app.set_option("zoom", "200").unwrap();+ });+ let view = &h.state().views[0];+ let viewport = view.viewport_rect();+ assert!(view.page_screen_rect(0).expect("page on screen").bottom() > viewport.bottom() + 100.0, "the page must not fit");+ // "Top" (glyphs 0..=2).+ let (from, to) = (view.glyph_screen_pos(0, 0).expect("glyph on screen"), view.glyph_screen_pos(0, 2).expect("glyph on screen"));+ press_and_move(&mut h, from, to);+ assert_eq!(h.state().views[0].selected_text().as_deref(), Some("Top"));+ let edge = egui::pos2(to.x, viewport.bottom() - 2.0);+ let before = page_top(&h, 0);+ h.hover_at(edge);+ h.run_steps(120);+ assert!(page_top(&h, 0) < before - 100.0, "the view scrolls while the pointer rests at its edge ({before} -> {})", page_top(&h, 0));+ let held = h.state().views[0].selected_text().expect("still selected");+ // The pointer stays above the "t" of "Top", so the selection ends at the "t" of "Bottom".+ assert_eq!(held, "Top line\nBot", "the selection reaches the bottom line");+ h.event(Event::PointerButton { pos: edge, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });+ h.run_steps(2);+ assert_eq!(h.state().views[0].selected_text(), Some(held), "releasing keeps the selection");+}