From c0ff5805cc0da7164f535b4b65e40edb0c471b28 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 30 Aug 2024 09:18:54 +0800 Subject: [PATCH] Refactor --- yazi-fs/src/folder.rs | 44 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/yazi-fs/src/folder.rs b/yazi-fs/src/folder.rs index fff9ee77..be6727aa 100644 --- a/yazi-fs/src/folder.rs +++ b/yazi-fs/src/folder.rs @@ -63,7 +63,7 @@ impl Folder { pub fn arrow(&mut self, step: impl Into) -> bool { let step = step.into() as Step; - let b = if self.files.is_empty() { + let mut b = if self.files.is_empty() { (self.cursor, self.offset, self.tracing) = (0, 0, false); false } else if step.is_positive() { @@ -72,9 +72,10 @@ impl Folder { self.prev(step) }; - self.sync_page(false); - self.adjust_offset(); self.tracing |= b; + b |= self.squeeze_offset(); + + self.sync_page(false); b } @@ -104,21 +105,6 @@ impl Folder { } } - pub fn adjust_offset(&mut self) { - let len = self.files.len(); - let limit = LAYOUT.load().current.height as usize; - let scrolloff = (limit / 2).min(MANAGER.scrolloff as usize); - let max_offset = (self.offset + limit).min(len).saturating_sub(scrolloff); - - let new_offset = if self.cursor >= max_offset { - self.cursor.saturating_sub(limit) + scrolloff - } else { - self.offset - }; - - self.offset = new_offset.min(len.saturating_sub(limit)); - } - fn next(&mut self, step: Step) -> bool { let old = (self.cursor, self.offset); let len = self.files.len(); @@ -127,10 +113,10 @@ impl Folder { let scrolloff = (limit / 2).min(MANAGER.scrolloff as usize); self.cursor = step.add(self.cursor, limit).min(len.saturating_sub(1)); - self.offset = if self.cursor >= (self.offset + limit).min(len).saturating_sub(scrolloff) { - len.saturating_sub(limit).min(self.offset + self.cursor - old.0) - } else { + self.offset = if self.cursor < (self.offset + limit).min(len).saturating_sub(scrolloff) { self.offset.min(len.saturating_sub(1)) + } else { + len.saturating_sub(limit).min(self.offset + self.cursor - old.0) }; old != (self.cursor, self.offset) @@ -152,6 +138,22 @@ impl Folder { old != (self.cursor, self.offset) } + + fn squeeze_offset(&mut self) -> bool { + let old = self.offset; + let len = self.files.len(); + + let limit = LAYOUT.load().current.height as usize; + let scrolloff = (limit / 2).min(MANAGER.scrolloff as usize); + + self.offset = if self.cursor < (self.offset + limit).min(len).saturating_sub(scrolloff) { + len.saturating_sub(limit).min(self.offset) + } else { + len.saturating_sub(limit).min(self.cursor.saturating_sub(limit) + scrolloff) + }; + + old != self.offset + } } impl Folder {