Skip to content

Commit 3ec47ce

Browse files
authored
Fix scrolling issues in staged/unstaged section (fixes #144) (#162)
1 parent 923bed9 commit 3ec47ce

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/components/filetree.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
use anyhow::Result;
1717
use asyncgit::{hash, StatusItem, StatusItemType};
1818
use crossterm::event::Event;
19+
use std::cell::Cell;
1920
use std::{borrow::Cow, convert::From, path::Path};
2021
use tui::{backend::Backend, layout::Rect, widgets::Text, Frame};
2122

@@ -28,6 +29,7 @@ pub struct FileTreeComponent {
2829
show_selection: bool,
2930
queue: Option<Queue>,
3031
theme: SharedTheme,
32+
scroll_top: Cell<usize>,
3133
}
3234

3335
impl FileTreeComponent {
@@ -46,6 +48,7 @@ impl FileTreeComponent {
4648
show_selection: focus,
4749
queue,
4850
theme,
51+
scroll_top: Cell::new(0),
4952
}
5053
}
5154

@@ -247,12 +250,25 @@ impl DrawableComponent for FileTreeComponent {
247250
},
248251
);
249252

253+
let select = self
254+
.tree
255+
.selection
256+
.map(|idx| idx - selection_offset)
257+
.unwrap_or_default();
258+
let tree_height = r.height.saturating_sub(2) as usize;
259+
260+
self.scroll_top.set(ui::calc_scroll_top(
261+
self.scroll_top.get(),
262+
tree_height,
263+
select,
264+
));
265+
250266
ui::draw_list(
251267
f,
252268
r,
253269
self.title.as_str(),
254-
items,
255-
self.tree.selection.map(|idx| idx - selection_offset),
270+
items.skip(self.scroll_top.get()),
271+
Some(select),
256272
self.focused,
257273
&self.theme,
258274
);

src/ui/scrolllist.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,8 @@ where
4949
L: Iterator<Item = Text<'b>>,
5050
{
5151
fn render(self, area: Rect, buf: &mut Buffer) {
52-
let list_area = match self.block {
53-
Some(b) => b.inner(area),
54-
None => area,
55-
};
56-
57-
let list_height = list_area.height as usize;
58-
59-
let offset = if self.scroll >= list_height {
60-
self.scroll - list_height + 1
61-
} else {
62-
0
63-
};
64-
6552
// Render items
66-
List::new(self.items.skip(offset as usize))
53+
List::new(self.items)
6754
.block(self.block.unwrap_or_default())
6855
.style(self.style)
6956
.render(area, buf);

0 commit comments

Comments
 (0)