From 670b496a824b91d6dd331d61355042b10420dcf9 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Fri, 30 Oct 2020 02:34:23 +0100 Subject: [PATCH] fix scrollbar (#350) --- src/components/commit_details/details.rs | 3 +-- src/components/diff.rs | 2 +- src/ui/scrollbar.rs | 23 +++++++++++++---------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index 6bcc66a01a..f11cd914b3 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -359,8 +359,7 @@ impl DrawableComponent for DetailsComponent { f, chunks[1], &self.theme, - self.get_number_of_lines(width as usize) - .saturating_sub(height as usize), + self.get_number_of_lines(width as usize), self.scroll_top.get(), ) } diff --git a/src/components/diff.rs b/src/components/diff.rs index d38c0eb35d..cf6e3a05f7 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -591,7 +591,7 @@ impl DrawableComponent for DiffComponent { r, &self.theme, self.lines_count(), - self.selection.get_end(), + self.scroll_top.get(), ); } diff --git a/src/ui/scrollbar.rs b/src/ui/scrollbar.rs index 673c3ceadf..5cd63bd472 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( f: &mut Frame, 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) }