diff --git a/src/components/revision_files.rs b/src/components/revision_files.rs index 5f077ac2..48ab7ace 100644 --- a/src/components/revision_files.rs +++ b/src/components/revision_files.rs @@ -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, current_file: SyntaxTextComponent, tree: FileTree, - scroll_top: Cell, + scroll: VerticalScroll, revision: Option, 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(&self, f: &mut Frame, 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); } } } diff --git a/src/components/utils/scroll_vertical.rs b/src/components/utils/scroll_vertical.rs index 73105513..c67c9b6d 100644 --- a/src/components/utils/scroll_vertical.rs +++ b/src/components/utils/scroll_vertical.rs @@ -21,6 +21,10 @@ impl VerticalScroll { self.top.get() } + pub fn reset(&self) { + self.top.set(0); + } + pub fn update( &self, selection: usize,