1
//! The window of a list on its rows: the first row on screen and the rows the view holds, kept
2
//! between frames by the draw so scrolling and paging stop where the last page fills the view.
3

            
4
use std::cell::Cell;
5

            
6
#[derive(Debug, Default, Clone, PartialEq, Eq)]
7
pub struct RowWindow {
8
    top: Cell<usize>,
9
    rows: Cell<usize>,
10
    len: Cell<usize>,
11
}
12

            
13
impl RowWindow {
14
    /// The first row of the last page of `len` rows in a view of `rows`.
15
    #[must_use]
16
4378
    pub fn last_top(len: usize, rows: usize) -> usize {
17
4378
        len.saturating_sub(rows)
18
4378
    }
19

            
20
    #[must_use]
21
553
    pub fn top(&self) -> usize {
22
553
        self.top.get()
23
553
    }
24

            
25
    /// The rows the view held when last drawn.
26
    #[must_use]
27
7
    pub fn rows(&self) -> usize {
28
7
        self.rows.get()
29
7
    }
30

            
31
1784
    pub fn set_top(&self, top: usize) {
32
1784
        self.top.set(top);
33
1784
    }
34

            
35
2950
    pub fn reset(&self) {
36
2950
        self.top.set(0);
37
2950
        self.rows.set(0);
38
2950
        self.len.set(0);
39
2950
    }
40

            
41
    /// Records a draw of `len` rows in a view of `rows` and returns the top, kept inside the
42
    /// last page.
43
    #[must_use]
44
2138
    pub fn fit(&self, len: usize, rows: usize) -> usize {
45
2138
        self.rows.set(rows);
46
2138
        self.len.set(len);
47
2138
        let top = self.top.get().min(Self::last_top(len, rows));
48
2138
        self.top.set(top);
49
2138
        top
50
2138
    }
51

            
52
86
    pub fn scroll_up(&self, n: usize) {
53
86
        self.top.set(self.top.get().saturating_sub(n));
54
86
    }
55

            
56
    /// Moves down `n` rows, stopping where the last drawn page fills the view.
57
547
    pub fn scroll_down(&self, n: usize) {
58
547
        let last = Self::last_top(self.len.get(), self.rows.get());
59
547
        self.top.set(self.top.get().saturating_add(n).min(last));
60
547
    }
61

            
62
7
    pub fn to_start(&self) {
63
7
        self.top.set(0);
64
7
    }
65

            
66
14
    pub fn to_end(&self) {
67
14
        self.top
68
14
            .set(Self::last_top(self.len.get(), self.rows.get()));
69
14
    }
70

            
71
    /// A view's height of rows, at least one.
72
    #[must_use]
73
770
    pub fn page(&self) -> usize {
74
770
        self.rows.get().max(1)
75
770
    }
76

            
77
28
    pub fn page_up(&self) {
78
28
        self.scroll_up(self.page());
79
28
    }
80

            
81
456
    pub fn page_down(&self) {
82
456
        self.scroll_down(self.page());
83
456
    }
84

            
85
    /// `selected` moved a view's height down or up among `len` rows.
86
    #[must_use]
87
148
    pub fn paged(&self, selected: usize, len: usize, down: bool) -> usize {
88
148
        match down {
89
99
            true => selected
90
99
                .saturating_add(self.page())
91
99
                .min(len.saturating_sub(1)),
92
49
            false => selected.saturating_sub(self.page()),
93
        }
94
148
    }
95
}
96

            
97
#[cfg(test)]
98
mod tests;