Compare commits

...
Author SHA1 Message Date
Martin BallandClaude Opus 5.5 d10e460913 viewed_page: #7149 item 7, find the page below by band top, not by column offset
The gap to measure is the one to the first later page whose shown band starts at or below this
page end. An incomplete last row in Columns and a Two Up cover (page 0 alone in row 0) have no
page columns entries later, which gave a gap of 0. Hide White Space still resolves to its seam.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 10:24:22 +00:00
Martin Ball 9fe1caba81 Merge origin/main into fix/7149-item7-viewed-page 2026-11-21 10:07:31 +00:00
Martin BallandClaude Opus 5.5 6fadccef6a viewed_page: #7149 item 7, measure the sliver gap to the page below in Columns and Two Up
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 21:32:16 +00:00
Martin BallandClaude Sonnet 5.5 ee06729687 viewed_page: #7149 item 7, a sliver of the page above is not the page in view
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 16:20:16 +00:00
2 changed files with 61 additions and 1 deletions
+12 -1
View File
@@ -7,10 +7,21 @@ 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 (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 {
Some((page, from, to)) if to >= top && from <= top + height => page,
_ => layout.rects.iter().zip(&layout.bands).position(|(r, band)| r.y0 + band.1 > top).unwrap_or(layout.rects.len().saturating_sub(1)),
_ => (0..layout.rects.len())
.position(|i| {
let end = layout.rects[i].y0 + layout.bands.get(i).map_or(0.0, |band| band.1);
// 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)),
}
}
+49
View File
@@ -2302,3 +2302,52 @@ fn a_linked_text_box_shows_and_follows_its_link_from_inside() {
let (start, _) = wordcraft_engine::cmd::insert::bookmark_range(&s.doc, "Spot").unwrap();
assert!(s.sel.is_collapsed() && s.sel.focus == start, "the caret went to the bookmark: {:?}", s.sel);
}
/// `viewed_page` with the sliver of the page above that Go To leaves at the top of the window: it is not the page in view.
#[test]
fn a_sliver_of_the_page_above_is_not_the_page_in_view() {
let rects: Vec<craft::behavior::kurbo::Rect> =
(0..4).map(|i| craft::behavior::kurbo::Rect::new(0.0, f64::from(i) * 110.0, 80.0, f64::from(i) * 110.0 + 100.0)).collect();
let layout = craft::pages::layout::PagedLayout { rects, size: Default::default(), columns: 1, bands: vec![(0.0, 100.0); 4] };
// The gap between pages is 10; page 2 ends at 320. A window top 4 above its end shows 4 of it: a sliver.
assert_eq!(viewed_page(None, 316.0, &layout, 100.0), 3, "4 points of page 2 is a sliver");
assert_eq!(viewed_page(None, 310.0, &layout, 100.0), 3, "as much as the gap is still a sliver");
assert_eq!(viewed_page(None, 300.0, &layout, 100.0), 2, "20 points of page 2 is the page in view");
}
/// In Columns and Two Up the pages are stored row by row: the gap to measure is the one to the page below, not to the
/// page beside.
#[test]
fn a_sliver_of_a_row_above_is_not_the_page_in_view_with_columns() {
let rects: Vec<craft::behavior::kurbo::Rect> = [(0.0, 0.0), (90.0, 0.0), (0.0, 110.0), (90.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); 4] };
// The first row ends at 100 and the next starts at 110: a window top at 96 shows 4 points of it, a sliver.
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<craft::behavior::kurbo::Rect> =
[(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<craft::behavior::kurbo::Rect> = [(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");
}