fix scrollbar (#350)

This commit is contained in:
Stephan Dilly 2020-10-30 02:34:23 +01:00
parent 7e6e310ab8
commit bd00b3e4b8
3 changed files with 15 additions and 13 deletions

View File

@ -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(),
)
}

View File

@ -591,7 +591,7 @@ impl DrawableComponent for DiffComponent {
r,
&self.theme,
self.lines_count(),
self.selection.get_end(),
self.scroll_top.get(),
);
}

View File

@ -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)
}