use new scroll in files list too

This commit is contained in:
Stephan Dilly 2021-06-05 22:59:00 +02:00
parent b9c763756a
commit 3a849bc900
2 changed files with 18 additions and 24 deletions

View file

@ -1,6 +1,7 @@
use super::{
CommandBlocking, CommandInfo, Component, DrawableComponent,
EventState, SyntaxTextComponent,
utils::scroll_vertical::VerticalScroll, CommandBlocking,
CommandInfo, Component, DrawableComponent, EventState,
SyntaxTextComponent,
};
use crate::{
keys::SharedKeyConfig,
@ -16,9 +17,7 @@ use asyncgit::{
use crossbeam_channel::Sender;
use crossterm::event::Event;
use filetreelist::{FileTree, FileTreeItem};
use std::{
cell::Cell, collections::BTreeSet, convert::From, path::Path,
};
use std::{collections::BTreeSet, convert::From, path::Path};
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
@ -43,7 +42,7 @@ pub struct RevisionFilesComponent {
files: Vec<TreeFile>,
current_file: SyntaxTextComponent,
tree: FileTree,
scroll_top: Cell<usize>,
scroll: VerticalScroll,
revision: Option<CommitId>,
focus: Focus,
key_config: SharedKeyConfig,
@ -60,7 +59,7 @@ impl RevisionFilesComponent {
Self {
queue: queue.clone(),
tree: FileTree::default(),
scroll_top: Cell::new(0),
scroll: VerticalScroll::new(),
current_file: SyntaxTextComponent::new(
sender,
key_config.clone(),
@ -168,25 +167,22 @@ impl RevisionFilesComponent {
fn draw_tree<B: Backend>(&self, f: &mut Frame<B>, area: Rect) {
let tree_height = usize::from(area.height.saturating_sub(2));
let selection = self.tree.visual_selection();
let visual_count = selection.map_or_else(
self.tree.visual_selection().map_or_else(
|| {
self.scroll_top.set(0);
0
self.scroll.reset();
},
|selection| {
self.scroll_top.set(ui::calc_scroll_top(
self.scroll_top.get(),
tree_height,
self.scroll.update(
selection.index,
));
selection.count
selection.count,
tree_height,
);
},
);
let items = self
.tree
.iterate(self.scroll_top.get(), tree_height)
.iterate(self.scroll.get(), tree_height)
.map(|(item, selected)| {
Self::tree_item_to_span(item, &self.theme, selected)
});
@ -213,13 +209,7 @@ impl RevisionFilesComponent {
);
if is_tree_focused {
ui::draw_scrollbar(
f,
area,
&self.theme,
visual_count.saturating_sub(tree_height),
self.scroll_top.get(),
);
self.scroll.draw(f, area, &self.theme);
}
}
}

View file

@ -21,6 +21,10 @@ impl VerticalScroll {
self.top.get()
}
pub fn reset(&self) {
self.top.set(0);
}
pub fn update(
&self,
selection: usize,