ui: fix scrollbar calculations

embed
Manos Pitsidianakis 2019-05-12 15:07:38 +03:00
parent 6e7ab0421b
commit 4c88422d71
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 31 additions and 23 deletions

View File

@ -372,9 +372,6 @@ impl ThreadView {
let page_no = (self.new_cursor_pos).wrapping_div(rows);
let top_idx = page_no * rows;
if (rows >= height!(self.content.area())) {
upper_left = pos_dec(upper_left!(area), (1, 0));
}
/* This closure (written for code clarity, should be inlined by the compiler) returns the
* **line** of an entry in the ThreadView grid. */
let get_entry_area = |idx: usize, entries: &[ThreadEntry]| {
@ -393,19 +390,21 @@ impl ThreadView {
if page_no != prev_page_no {
clear_area(grid, area);
}
let visibles = self
let visibles: Vec<&usize> = self
.visible_entries
.iter()
.flat_map(|v| v.iter())
.skip(top_idx)
.take(rows);
let mut visible_entry_counter = 0;
.flat_map(|ref v| v.iter())
.collect();
if (rows >= visibles.len()) {
upper_left = pos_dec(upper_left!(area), (1, 0));
}
for v in visibles {
let mut visible_entry_counter = 0;
for v in visibles.iter().skip(top_idx).take(rows) {
if visible_entry_counter >= rows {
break;
}
let idx = v;
let idx = *v;
copy_area(
grid,
&self.content,
@ -422,11 +421,6 @@ impl ThreadView {
}
/* If cursor position has changed, remove the highlight from the previous position and
* apply it in the new one. */
let visibles: Vec<&usize> = self
.visible_entries
.iter()
.flat_map(|ref v| v.iter())
.collect();
self.cursor_pos = self.new_cursor_pos;
if self.cursor_pos + 1 > visibles.len() {
self.cursor_pos = visibles.len().saturating_sub(1);
@ -455,7 +449,7 @@ impl ThreadView {
);
self.highlight_line(grid, dest_area, src_area, idx);
if (rows < height!(self.content.area())) {
if (rows < visibles.len()) {
ScrollBar::draw(
grid,
(
@ -479,6 +473,9 @@ impl ThreadView {
.iter()
.flat_map(|ref v| v.iter())
.collect();
if (rows >= visibles.len()) {
upper_left = pos_dec(upper_left!(area), (1, 0));
}
for &idx in &[old_cursor_pos, self.cursor_pos] {
let entry_idx = *visibles[idx];
let src_area = { get_entry_area(entry_idx, &self.entries) };
@ -504,7 +501,7 @@ impl ThreadView {
);
self.highlight_line(grid, dest_area, src_area, entry_idx);
if (rows < height!(self.content.area())) {
if (rows < visibles.len()) {
ScrollBar::draw(
grid,
(

View File

@ -707,22 +707,33 @@ impl ScrollBar {
if height < 3 {
return;
}
let visible_ratio: f32 = (std::cmp::min(visible_rows, length) as f32) / (length as f32);
let scrollbar_height = std::cmp::max((visible_ratio * (length as f32)) as usize, 1);
let scrollbar_offset = (visible_ratio * (pos as f32)) as usize + 1;
let height = height - 2;
clear_area(grid, area);
let visible_ratio: f32 = (std::cmp::min(visible_rows, length) as f32) / (length as f32);
let scrollbar_height = std::cmp::max((visible_ratio * (height as f32)) as usize, 1);
let scrollbar_offset = {
let mut temp = (((pos as f32) / (length as f32)) * (height as f32)) as usize;
if temp + scrollbar_height >= height {
height - scrollbar_height
} else {
temp
}
};
let (upper_left, bottom_right) = area;
grid[upper_left].set_ch('▴');
for y in get_y(upper_left) + 1..(get_y(upper_left) + scrollbar_offset) {
let upper_left = (upper_left.0, upper_left.1 + 1);
for y in get_y(upper_left)..(get_y(upper_left) + scrollbar_offset) {
grid[set_y(upper_left, y)].set_ch(' ');
}
for y in (get_y(upper_left) + scrollbar_offset)
..(get_y(upper_left) + scrollbar_offset + scrollbar_height)
..=(get_y(upper_left) + scrollbar_offset + scrollbar_height)
{
grid[set_y(upper_left, y)].set_ch('█');
}
for y in (get_y(upper_left) + scrollbar_offset + scrollbar_height)..get_y(bottom_right) - 1
for y in (get_y(upper_left) + scrollbar_offset + scrollbar_height + 1)..get_y(bottom_right)
{
grid[set_y(upper_left, y)].set_ch(' ');
}