Lines
100 %
Functions
Branches
//! The window of a list on its rows: the first row on screen and the rows the view holds, kept
//! between frames by the draw so scrolling and paging stop where the last page fills the view.
use std::cell::Cell;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RowWindow {
top: Cell<usize>,
rows: Cell<usize>,
len: Cell<usize>,
}
impl RowWindow {
/// The first row of the last page of `len` rows in a view of `rows`.
#[must_use]
pub fn last_top(len: usize, rows: usize) -> usize {
len.saturating_sub(rows)
pub fn top(&self) -> usize {
self.top.get()
/// The rows the view held when last drawn.
pub fn rows(&self) -> usize {
self.rows.get()
pub fn set_top(&self, top: usize) {
self.top.set(top);
pub fn reset(&self) {
self.top.set(0);
self.rows.set(0);
self.len.set(0);
/// Records a draw of `len` rows in a view of `rows` and returns the top, kept inside the
/// last page.
pub fn fit(&self, len: usize, rows: usize) -> usize {
self.rows.set(rows);
self.len.set(len);
let top = self.top.get().min(Self::last_top(len, rows));
top
pub fn scroll_up(&self, n: usize) {
self.top.set(self.top.get().saturating_sub(n));
/// Moves down `n` rows, stopping where the last drawn page fills the view.
pub fn scroll_down(&self, n: usize) {
let last = Self::last_top(self.len.get(), self.rows.get());
self.top.set(self.top.get().saturating_add(n).min(last));
pub fn to_start(&self) {
pub fn to_end(&self) {
self.top
.set(Self::last_top(self.len.get(), self.rows.get()));
/// A view's height of rows, at least one.
pub fn page(&self) -> usize {
self.rows.get().max(1)
pub fn page_up(&self) {
self.scroll_up(self.page());
pub fn page_down(&self) {
self.scroll_down(self.page());
/// `selected` moved a view's height down or up among `len` rows.
pub fn paged(&self, selected: usize, len: usize, down: bool) -> usize {
match down {
true => selected
.saturating_add(self.page())
.min(len.saturating_sub(1)),
false => selected.saturating_sub(self.page()),
#[cfg(test)]
mod tests;