diff --git a/crates/ui-egui/src/canvas/navigation.rs b/crates/ui-egui/src/canvas/navigation.rs index 9eee2dae1b..437e77675c 100644 --- a/crates/ui-egui/src/canvas/navigation.rs +++ b/crates/ui-egui/src/canvas/navigation.rs @@ -7,7 +7,7 @@ use super::{Pos2, WordApp}; /// coordinates) while any of it is inside the window (`top` .. `top + height`, `top` the layout's y at the window's /// top edge), else the page at the window's top: the first whose shown band ends below it (with Hide White Space the /// rects overlap in the hidden margins, and the next page's text already shows there), the last page past the end. -/// A page whose shown part at the top is no more than the gap to the page below it (one row down) does not count: Go To leaves the +/// A page whose shown part at the top is no more than the gap to the page below it (the next row down) does not count: Go To leaves the /// pages' margin above a page, which is more than that gap, so a sliver of the page above still shows (#7149). pub(super) fn viewed_page(caret: Option<(usize, f64, f64)>, top: f64, layout: &PagedLayout, height: f64) -> usize { match caret { @@ -15,7 +15,10 @@ pub(super) fn viewed_page(caret: Option<(usize, f64, f64)>, top: f64, layout: &P _ => (0..layout.rects.len()) .position(|i| { let end = layout.rects[i].y0 + layout.bands.get(i).map_or(0.0, |band| band.1); - let gap = layout.rects.get(i + layout.columns.max(1)).map_or(0.0, |next| (next.y0 - end).max(0.0)); + // The gap to the first later page whose shown part starts at or below this one's end: the page below, + // whatever the column (an incomplete last row, a Two Up cover) and with Hide White Space (a seam). + let below = |j: &usize| layout.rects.get(*j).is_some_and(|r| r.y0 + layout.bands.get(*j).map_or(0.0, |b| b.0) >= end); + let gap = ((i + 1)..layout.rects.len()).find(below).and_then(|j| layout.rects.get(j)).map_or(0.0, |next| (next.y0 - end).max(0.0)); end > top + gap }) .unwrap_or(layout.rects.len().saturating_sub(1)), diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 4eb50a05c0..0b47bc0759 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -2328,3 +2328,26 @@ fn a_sliver_of_a_row_above_is_not_the_page_in_view_with_columns() { assert_eq!(viewed_page(None, 96.0, &layout, 100.0), 2, "4 points of the row above is a sliver"); assert_eq!(viewed_page(None, 80.0, &layout, 100.0), 0, "20 points of the first row is the page in view"); } + +/// A last row that is not full has no page below the one above it: the gap is still the one to the next row. +#[test] +fn a_sliver_of_a_row_above_is_not_the_page_in_view_with_an_incomplete_last_row() { + let rects: Vec = + [(0.0, 0.0), (90.0, 0.0), (0.0, 110.0)].iter().map(|&(x, y)| craft::behavior::kurbo::Rect::new(x, y, x + 80.0, y + 100.0)).collect(); + let layout = craft::pages::layout::PagedLayout { rects, size: Default::default(), columns: 2, bands: vec![(0.0, 100.0); 3] }; + assert_eq!(viewed_page(None, 96.0, &layout, 100.0), 2, "4 points of the row above is a sliver"); + assert_eq!(viewed_page(None, 80.0, &layout, 100.0), 0, "20 points of the first row is the page in view"); +} + +/// Two Up with a cover: page 0 is a row of its own, so there is no page `columns` after it. +#[test] +fn a_sliver_of_the_cover_is_not_the_page_in_view() { + let rects: Vec = [(90.0, 0.0), (0.0, 110.0), (90.0, 110.0), (0.0, 220.0)] + .iter() + .map(|&(x, y)| craft::behavior::kurbo::Rect::new(x, y, x + 80.0, y + 100.0)) + .collect(); + let layout = craft::pages::layout::PagedLayout { rects, size: Default::default(), columns: 2, bands: vec![(0.0, 100.0); 4] }; + assert_eq!(viewed_page(None, 96.0, &layout, 100.0), 1, "4 points of the cover is a sliver"); + assert_eq!(viewed_page(None, 80.0, &layout, 100.0), 0, "20 points of the cover is the page in view"); + assert_eq!(viewed_page(None, 206.0, &layout, 100.0), 3, "4 points of the spread is a sliver"); +}