summaryrefslogtreecommitdiffstats
path: root/src/command/scroll.rs
blob: 5734c2591d1f5ea5bef1445907c27ace8065fdd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#[derive(Debug, Clone, Copy)]
pub enum ScrollCommand {
    Lines(i32),
    Pages(i32),
}

impl ScrollCommand {
    pub fn to_lines(self, page_height: usize) -> i32 {
        match self {
            Self::Lines(n) => n,
            Self::Pages(n) => n * page_height as i32,
        }
    }
    pub fn is_up(self) -> bool {
        match self {
            Self::Lines(n) => n < 0,
            Self::Pages(n) => n < 0,
        }
    }
    /// compute the new scroll value
    pub fn apply(
        self,
        scroll: usize,
        content_height: usize,
        page_height: usize,
    ) -> usize {
        (scroll as i32 + self.to_lines(page_height))
            .min(content_height as i32 - page_height as i32 + 1)
            .max(0) as usize
    }
    pub fn is_thumb(y: u16, scrollbar: Option<(u16, u16)>) -> bool {
        if let Some((sctop, scbottom)) = scrollbar {
            if sctop <= y && y <= scbottom {
                return true;
            }
        }
        false
    }
}