diff options
author | Stephan Dilly <dilly.stephan@gmail.com> | 2020-10-30 02:34:23 +0100 |
---|---|---|
committer | Stephan Dilly <dilly.stephan@gmail.com> | 2020-10-31 12:32:20 +0100 |
commit | bd00b3e4b8980968b9fbedbe1308079df9ffeda3 (patch) | |
tree | 65e897107339fa6e8b888585f428ecb96270d9b3 /src/ui | |
parent | 7e6e310ab842fa73c0682e3a8ce379f2bf52ed1b (diff) |
fix scrollbar (#350)
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/scrollbar.rs | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/ui/scrollbar.rs b/src/ui/scrollbar.rs index 673c3cea..5cd63bd4 100644 --- a/src/ui/scrollbar.rs +++ b/src/ui/scrollbar.rs @@ -12,16 +12,16 @@ use tui::{ /// struct Scrollbar { - max: u16, + lines: u16, pos: u16, style_bar: Style, style_pos: Style, } impl Scrollbar { - fn new(max: usize, pos: usize) -> Self { + fn new(lines: usize, pos: usize) -> Self { Self { - max: u16::try_from(max).unwrap_or_default(), + lines: u16::try_from(lines).unwrap_or_default(), pos: u16::try_from(pos).unwrap_or_default(), style_pos: Style::default(), style_bar: Style::default(), @@ -41,11 +41,11 @@ impl Widget for Scrollbar { vertical: 1, }); - if area.height < 4 { + if area.height == 0 { return; } - if area.height > self.max { + if area.height >= self.lines { return; } @@ -53,12 +53,15 @@ impl Widget for Scrollbar { buf.set_string(right, y, THICK_VERTICAL, self.style_bar); } - let progress = f32::from(self.pos) / f32::from(self.max); - let pos = f32::from(area.height.saturating_sub(1)) * progress; + let max_pos = self.lines.saturating_sub(area.height); + let progress = f32::from(self.pos) / f32::from(max_pos); + let progress = if progress > 1.0 { 1.0 } else { progress }; + let pos = f32::from(area.height) * progress; + //TODO: any better way for this? #[allow(clippy::cast_sign_loss)] #[allow(clippy::cast_possible_truncation)] - let pos = pos as u16; + let pos = (pos as u16).saturating_sub(1); buf.set_string(right, area.top() + pos, FULL, self.style_pos); } @@ -68,10 +71,10 @@ pub fn draw_scrollbar<B: Backend>( f: &mut Frame<B>, r: Rect, theme: &SharedTheme, - max: usize, + lines: usize, pos: usize, ) { - let mut widget = Scrollbar::new(max, pos); + let mut widget = Scrollbar::new(lines, pos); widget.style_pos = theme.scroll_bar_pos(); f.render_widget(widget, r) } |