rosier 0.5.0

A fast picture and video viewer for large folders
//! Vertical scroll for books: the pages one under another in a column,
//! scrolled continuously, as comics made for phones are read. Where each
//! page sits; the decoding and drawing are in viewer.rs and ui_split.rs.

/// Space between pages, in layout pixels.
pub const GAP: f32 = 6.0;

/// Height over width of a page not decoded yet: a common comic page.
pub const GUESS: f32 = 1.42;

/// How many pages before the top one stay decoded, and how many after the
/// last one on screen are decoded ahead.
pub const BEHIND: usize = 1;
pub const AHEAD: usize = 2;

/// Width of the column in a `w`x`h` pane: the whole width, but no wider
/// than three quarters of the height, so a page is not taller than a few
/// screens.
pub fn column(w: f32, h: f32) -> f32 {
    w.min(h * 0.75).max(1.0)
}

/// Where the column is scrolled to: page `top` starts `offset` pixels above
/// the top of the pane (0 up to the page's height).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Pos {
    pub top: usize,
    pub offset: f32,
}

/// `pos` scrolled down by `dy` pixels (up when negative) in a book of `n`
/// pages whose heights `height` gives, in a pane `view` pixels tall. It
/// stops at the first page's top and where the last page's bottom meets
/// the pane's; the second value says whether it had to stop there.
pub fn scroll(pos: Pos, dy: f32, n: usize, view: f32, height: impl Fn(usize) -> f32) -> (Pos, bool) {
    if n == 0 {
        return (Pos { top: 0, offset: 0.0 }, true);
    }
    let mut top = pos.top.min(n - 1);
    let mut offset = pos.offset + dy;
    while offset < 0.0 && top > 0 {
        top -= 1;
        offset += height(top) + GAP;
    }
    if offset < 0.0 {
        return (Pos { top: 0, offset: 0.0 }, true);
    }
    while top + 1 < n && offset >= height(top) + GAP {
        offset -= height(top) + GAP;
        top += 1;
    }
    // The bottom of the last page stops at the bottom of the pane: move
    // back up from the end by one pane.
    let end = end(n, view, &height);
    let past = (top, offset) > (end.top, end.offset);
    if past { (end, dy > 0.0) } else { (Pos { top, offset }, false) }
}

/// The furthest the column scrolls: the last page's bottom at the pane's
/// bottom (or the last page's top, when it is shorter than the pane).
pub fn end(n: usize, view: f32, height: impl Fn(usize) -> f32) -> Pos {
    let mut top = n.saturating_sub(1);
    let mut above = (view - height(top)).max(0.0);
    let mut offset = (height(top) - view).max(0.0);
    while above > 0.0 && top > 0 {
        let h = height(top - 1) + GAP;
        if h >= above {
            offset = h - above;
            above = 0.0;
        } else {
            above -= h;
        }
        top -= 1;
    }
    if above > 0.0 { Pos { top: 0, offset: 0.0 } } else { Pos { top, offset } }
}

/// The pages a pane `view` pixels tall shows at `pos`, with where each
/// starts (from the pane's top) and how tall it is.
pub fn visible(pos: Pos, n: usize, view: f32, height: impl Fn(usize) -> f32) -> Vec<(usize, f32, f32)> {
    let mut out = Vec::new();
    let mut y = -pos.offset;
    let mut k = pos.top;
    while k < n && y < view {
        let h = height(k);
        out.push((k, y, h));
        y += h + GAP;
        k += 1;
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn h(k: usize) -> f32 {
        [100.0, 200.0, 50.0][k % 3]
    }

    #[test]
    fn scrolling_crosses_pages_and_stops_at_both_ends() {
        let at = |top, offset| Pos { top, offset };
        // Down past the first page into the second.
        assert_eq!(scroll(at(0, 0.0), 130.0, 6, 80.0, h), (at(1, 130.0 - 100.0 - GAP), false));
        // Back up across it.
        assert_eq!(scroll(at(1, 10.0), -20.0, 6, 80.0, h), (at(0, 100.0 + GAP - 10.0), false));
        // Above the first page it stops.
        assert_eq!(scroll(at(0, 5.0), -50.0, 6, 80.0, h), (at(0, 0.0), true));
        // Past the end it stops with the last page's bottom at the bottom.
        let (last, stopped) = scroll(at(0, 0.0), 10_000.0, 6, 80.0, h);
        assert!(stopped);
        let shown = visible(last, 6, 80.0, h);
        let (k, y, hh) = *shown.last().unwrap();
        assert_eq!(k, 5);
        assert!((y + hh - 80.0).abs() < 0.01, "{shown:?}");
        // Scrolling up from there is not stopped.
        assert!(!scroll(last, -1.0, 6, 80.0, h).1);
    }

    #[test]
    fn a_short_book_does_not_scroll() {
        // Two pages that fit in the pane together.
        let small = |_| 20.0;
        assert_eq!(end(2, 500.0, small), Pos { top: 0, offset: 0.0 });
        assert_eq!(scroll(Pos { top: 0, offset: 0.0 }, 30.0, 2, 500.0, small).0, Pos { top: 0, offset: 0.0 });
    }

    #[test]
    fn the_pages_on_screen_follow_each_other_with_a_gap() {
        let v = visible(Pos { top: 1, offset: 150.0 }, 6, 200.0, h);
        assert_eq!(v[0], (1, -150.0, 200.0));
        assert_eq!(v[1], (2, 50.0 + GAP, 50.0));
        assert_eq!(v[2].0, 3);
        assert!(v.iter().all(|(_, y, _)| *y < 200.0));
    }

    #[test]
    fn the_column_is_at_most_three_quarters_of_the_height() {
        assert_eq!(column(1920.0, 1000.0), 750.0);
        assert_eq!(column(400.0, 1000.0), 400.0);
    }
}