From 87805509c56a9552efefdb9ee94ee3118f5f172a Mon Sep 17 00:00:00 2001 From: Ahmed ElSamhaa Date: Mon, 8 Apr 2024 00:34:57 +0300 Subject: [PATCH] Refactors the calc_skipped_rows function to make it more readable --- src/directory_buffer.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/directory_buffer.rs b/src/directory_buffer.rs index 5d73ec98..e1e3c567 100644 --- a/src/directory_buffer.rs +++ b/src/directory_buffer.rs @@ -31,18 +31,22 @@ impl ScrollState { let current_focus = self.current_focus; let last_focus = self.last_focus; let first_visible_row = self.skipped_rows; + + // Calculate the cushion rows at the start and end of the view port let start_cushion_row = first_visible_row + ScrollState::PREVIEW_CUSHION; let end_cushion_row = (first_visible_row + height) .saturating_sub(ScrollState::PREVIEW_CUSHION + 1); - if !vimlike_scrolling { + let new_skipped_rows = if !vimlike_scrolling { height * (self.current_focus / height.max(1)) } else if last_focus == None { // Just entered the directory 0 } else if current_focus == 0 { + // Focus on first node 0 } else if current_focus == total.saturating_sub(1) { + // Focus on last node total.saturating_sub(height) } else if current_focus > last_focus.unwrap() { // Scrolling down @@ -63,7 +67,9 @@ impl ScrollState { } else { current_focus.saturating_sub(ScrollState::PREVIEW_CUSHION) } - } + }; + + new_skipped_rows } }