Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge PR #2595: M3: a page jumped to in single-page view no longer hops into place

aiongg0c0995b62e60
diff --cc crates/ui-egui/tests/view.rs
index 372dd9479,b492d09b0..95761ab04
--- a/crates/ui-egui/tests/view.rs
+++ b/crates/ui-egui/tests/view.rs
@@@ -185,38 -185,21 +185,53 @@@ fn single_page_layout_shows_one_page_at
assert!(rect(&h, 0).is_none() && rect(&h, 1).is_some(), "only page 2");
}
+#[test]
+fn single_page_view_prefetches_the_next_page() {
+ // Only the shown page is drawn, but the next one renders ahead of the turn: otherwise
+ // turning forward shows blank paper until it arrives.
+ let mut h = harness(&[("layout", "single"), ("zoom", "50")]);
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ while h.state().render_pending() {
+ assert!(std::time::Instant::now() < deadline, "renders settle");
+ h.run_steps(2);
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert!(rect(&h, 1).is_none(), "page 2 is not shown");
+ assert!(h.state().views[0].raster_memory().page_count >= 2, "page 2 is rendered ahead of the turn");
+}
+
+#[test]
+fn single_page_wheel_turn_draws_the_new_page_at_once() {
+ // The frame that turns the page draws the new one. Its jump used to be measured from the
+ // old page's place, which scrolled both out of view: a blank frame on every turn forward.
+ let mut h = harness_stepping(1.0 / 60.0, &[("layout", "single")]);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Num0); // fit page
+ h.run_steps(4);
+ let vp = h.state().views[0].viewport_rect();
+ h.hover_at(vp.center());
+ h.run_steps(2);
+ h.event(wheel(MouseWheelUnit::Line, -1.0, TouchPhase::Move));
+ h.step();
+ assert_eq!(h.state().views[0].current, 1, "one notch turns one page");
+ let r = rect(&h, 1).expect("page 2 drawn in the frame that turns to it");
+ assert!(r.min.y >= vp.min.y && r.max.y <= vp.max.y, "page 2 fits on screen: {r:?} in {vp:?}");
+}
+
+ #[test]
+ fn single_page_jump_draws_a_fitting_page_where_it_stays() {
+ // A page that fits has nothing to scroll, but the jump to it asked for the gap above it:
+ // drawn that much too high for a frame, it then hopped into place.
+ let mut h = harness_stepping(1.0 / 60.0, &[("layout", "single")]);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Num0); // fit page
+ h.run_steps(4);
+ h.state_mut().set_option("page", "2").unwrap();
+ h.step();
+ let first = rect(&h, 1).expect("page 2 drawn in the frame after the jump");
+ h.run_steps(4);
+ let settled = rect(&h, 1).expect("page 2");
+ assert!((first.min.y - settled.min.y).abs() < 0.5, "page 2 drawn where it settles: {first:?} then {settled:?}");
+ }
+
#[test]
fn single_page_wheel_pans_instead_when_zoomed_in() {
// The zoomed-in guard: with room to pan, the wheel pans and must not turn pages.