Skip to content

Commit 3a849bc

Browse files
author
Stephan Dilly
committed
use new scroll in files list too
1 parent b9c7637 commit 3a849bc

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

src/components/revision_files.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{
2-
CommandBlocking, CommandInfo, Component, DrawableComponent,
3-
EventState, SyntaxTextComponent,
2+
utils::scroll_vertical::VerticalScroll, CommandBlocking,
3+
CommandInfo, Component, DrawableComponent, EventState,
4+
SyntaxTextComponent,
45
};
56
use crate::{
67
keys::SharedKeyConfig,
@@ -16,9 +17,7 @@ use asyncgit::{
1617
use crossbeam_channel::Sender;
1718
use crossterm::event::Event;
1819
use filetreelist::{FileTree, FileTreeItem};
19-
use std::{
20-
cell::Cell, collections::BTreeSet, convert::From, path::Path,
21-
};
20+
use std::{collections::BTreeSet, convert::From, path::Path};
2221
use tui::{
2322
backend::Backend,
2423
layout::{Constraint, Direction, Layout, Rect},
@@ -43,7 +42,7 @@ pub struct RevisionFilesComponent {
4342
files: Vec<TreeFile>,
4443
current_file: SyntaxTextComponent,
4544
tree: FileTree,
46-
scroll_top: Cell<usize>,
45+
scroll: VerticalScroll,
4746
revision: Option<CommitId>,
4847
focus: Focus,
4948
key_config: SharedKeyConfig,
@@ -60,7 +59,7 @@ impl RevisionFilesComponent {
6059
Self {
6160
queue: queue.clone(),
6261
tree: FileTree::default(),
63-
scroll_top: Cell::new(0),
62+
scroll: VerticalScroll::new(),
6463
current_file: SyntaxTextComponent::new(
6564
sender,
6665
key_config.clone(),
@@ -168,25 +167,22 @@ impl RevisionFilesComponent {
168167
fn draw_tree<B: Backend>(&self, f: &mut Frame<B>, area: Rect) {
169168
let tree_height = usize::from(area.height.saturating_sub(2));
170169

171-
let selection = self.tree.visual_selection();
172-
let visual_count = selection.map_or_else(
170+
self.tree.visual_selection().map_or_else(
173171
|| {
174-
self.scroll_top.set(0);
175-
0
172+
self.scroll.reset();
176173
},
177174
|selection| {
178-
self.scroll_top.set(ui::calc_scroll_top(
179-
self.scroll_top.get(),
180-
tree_height,
175+
self.scroll.update(
181176
selection.index,
182-
));
183-
selection.count
177+
selection.count,
178+
tree_height,
179+
);
184180
},
185181
);
186182

187183
let items = self
188184
.tree
189-
.iterate(self.scroll_top.get(), tree_height)
185+
.iterate(self.scroll.get(), tree_height)
190186
.map(|(item, selected)| {
191187
Self::tree_item_to_span(item, &self.theme, selected)
192188
});
@@ -213,13 +209,7 @@ impl RevisionFilesComponent {
213209
);
214210

215211
if is_tree_focused {
216-
ui::draw_scrollbar(
217-
f,
218-
area,
219-
&self.theme,
220-
visual_count.saturating_sub(tree_height),
221-
self.scroll_top.get(),
222-
);
212+
self.scroll.draw(f, area, &self.theme);
223213
}
224214
}
225215
}

src/components/utils/scroll_vertical.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ impl VerticalScroll {
2121
self.top.get()
2222
}
2323

24+
pub fn reset(&self) {
25+
self.top.set(0);
26+
}
27+
2428
pub fn update(
2529
&self,
2630
selection: usize,

0 commit comments

Comments
 (0)