fix(ui): correct scrolling up behavior in lazy list

Ayman Bagabas created

Change summary

internal/ui/lazylist/list.go | 25 +++++--------------------
1 file changed, 5 insertions(+), 20 deletions(-)

Detailed changes

internal/ui/lazylist/list.go 🔗

@@ -259,25 +259,14 @@ func (l *List) ScrollBy(lines int) {
 		}
 	} else if lines < 0 {
 		// Scroll up
-		// Calculate from offset how many items needed to fill the viewport
-		// This is needed to know when to stop scrolling up
-		var totalLines int
-		var firstItemIdx int
-		for i := l.offsetIdx; i >= 0; i-- {
-			item := l.getItem(i)
-			totalLines += item.height
-			if l.gap > 0 && i < l.offsetIdx {
-				totalLines += l.gap
-			}
-			if totalLines >= l.height {
-				firstItemIdx = i
+		l.offsetLine += lines // lines is negative
+		for l.offsetLine < 0 {
+			if l.offsetIdx <= 0 {
+				// Reached top
+				l.ScrollToTop()
 				break
 			}
-		}
 
-		// Now scroll up by lines
-		l.offsetLine += lines // lines is negative
-		for l.offsetIdx > firstItemIdx && l.offsetLine < 0 {
 			// Move to previous item
 			l.offsetIdx--
 			prevItem := l.getItem(l.offsetIdx)
@@ -287,10 +276,6 @@ func (l *List) ScrollBy(lines int) {
 			}
 			l.offsetLine += totalHeight
 		}
-
-		if l.offsetLine < 0 {
-			l.offsetLine = 0
-		}
 	}
 }