diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 923f13f57..3d4244cb5 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -805,20 +805,23 @@ fn run_pass(ui: &mut Ui, cx: &mut BodyCx<'_>, held: &mut Held<'_, PageViewState> geo = build.of(&state); state.zoom = geo.scale / SCREEN_POINTS_PER_POINT; want.1 = match geo.layout.rects.get(state.current) { - // The view's top goes where what shows of the row above ends (the document's start for the first row), so - // the space the layout keeps above the page shows and nothing of the page before it. + // A point inside the page goes to the view's top. The page's own top puts the view's top where what shows + // of the row above ends (the document's start for the first row), so the space the layout keeps above the + // page shows and nothing of the page before it. Some(r) if state.continuous => { let (top, bottom) = geo.layout.bands[state.current]; let start = r.y0 + top; - let above = geo - .layout - .rects - .iter() - .zip(&geo.layout.bands) - .map(|(r, band)| r.y0 + band.1) - .filter(|&end| end <= start) - .fold(geo.doc.y0, f64::max); - above + fraction * (bottom - top) + if fraction > 0.0 { + start + fraction * (bottom - top) + } else { + geo.layout + .rects + .iter() + .zip(&geo.layout.bands) + .map(|(r, band)| r.y0 + band.1) + .filter(|&end| end <= start) + .fold(geo.doc.y0, f64::max) + } } _ => geo.doc.y0, }; diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index 46032dbde..425a56ccf 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -909,30 +909,32 @@ fn a_wheel_up_at_the_top_never_moves_the_page_with_rulers_on_or_off() { } } -/// Go To page `page` in a continuous view of 20 pages at 100 %, `rulers` on or off: the first page the view places and -/// the offset, with where the page before ends. -fn gone_to(page: usize, rulers: bool) -> (Option, f64, f64) { +/// Go To `fraction` of page `page` in a continuous view of 20 pages at 100 %, `rulers` on or off: the first page the +/// view places and the offset, with where the page before ends and where that fraction of the page lies. +fn gone_to(page: usize, fraction: f64, rulers: bool) -> (Option, f64, f64, f64) { let state = Rc::new(RefCell::new(state_with(|s| { s.fit = Fit::None; s.zoom = 1.0; s.continuous = true; }))); - let seen = Rc::new(RefCell::new((None, 0.0))); + let seen = Rc::new(RefCell::new((None, 0.0, 0.0))); let (kept, shown) = (Rc::clone(&state), Rc::clone(&seen)); let pages = DemoPages::new(20); let mut fixture = Fixture::new(move |ui| { let mut now = kept.borrow().clone(); let out = PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).rulers(rulers).show(ui, |_, _| ()); let before = page.checked_sub(1).map_or(0.0, |before| out.layout.rects[before].y0 + out.layout.bands[before].1); - *shown.borrow_mut() = (out.pages.first().map(|p| p.page), before); + let (top, bottom) = out.layout.bands[page]; + let at = out.layout.rects[page].y0 + top + fraction * (bottom - top); + *shown.borrow_mut() = (out.pages.first().map(|p| p.page), before, at); *kept.borrow_mut() = now; }); fixture.room(vec2(1280.0, 760.0)); fixture.settle(); - state.borrow_mut().go_to(page); + state.borrow_mut().go_to = Some((page, fraction)); fixture.idle(3); - let (first, before) = *seen.borrow(); - (first, state.borrow().offset.1, before) + let (first, before, at) = *seen.borrow(); + (first, state.borrow().offset.1, before, at) } /// Go To puts the view's top where the page before ends, so the gap above the page shows and nothing of the page @@ -940,8 +942,16 @@ fn gone_to(page: usize, rulers: bool) -> (Option, f64, f64) { #[trial] fn go_to_shows_the_page_at_the_top_and_nothing_of_the_page_before() { for rulers in [false, true] { - let (first, offset, before) = gone_to(5, rulers); + let (first, offset, before, _) = gone_to(5, 0.0, rulers); assert_eq!(first, Some(5), "rulers {rulers}: the first page in view is the one gone to"); assert!((offset - before).abs() < 0.01, "rulers {rulers}: the view's top {offset} is where the page before ends, {before}"); } } + +/// Go To a point inside a page puts that point at the view's top, not the gap above it. +#[trial] +fn go_to_a_point_inside_a_page_puts_it_at_the_top() { + let (first, offset, _, at) = gone_to(5, 0.5, false); + assert_eq!(first, Some(5), "the first page in view is the one gone to"); + assert!((offset - at).abs() < 0.01, "the view's top {offset} is the point gone to, {at}"); +}