on main help sign in
fix(ui): scroll down off by one
1460d75Ayman Bagabas
internal/ui/lazylist/list.go+32−3
@@ -138,7 +138,7 @@ func (l *List) ScrollBy(lines int) {
138 if l.gap > 0 && i < len(l.items)-1 {
138 if l.gap > 0 && i < len(l.items)-1 {
139 totalLines += l.gap
139 totalLines += l.gap
140 }
140 }
141− if totalLines >= l.height {
141+ if totalLines > l.height-1 {
142 lastItemIdx = i
142 lastItemIdx = i
143 break
143 break
144 }
144 }
@@ -165,7 +165,7 @@ func (l *List) ScrollBy(lines int) {
165 }
165 }
166
166
167 if l.offsetLine >= item.height {
167 if l.offsetLine >= item.height {
168− l.offsetLine = item.height - 1
168+ l.offsetLine = item.height
169 }
169 }
170 } else if lines < 0 {
170 } else if lines < 0 {
171 // Scroll up
171 // Scroll up
@@ -376,7 +376,36 @@ func (l *List) ScrollToBottom() {
376
376
377// ScrollToSelected scrolls the list to the selected item.
377// ScrollToSelected scrolls the list to the selected item.
378func (l *List) ScrollToSelected() {
378func (l *List) ScrollToSelected() {
379− // TODO: Implement me
379+ if l.selectedIdx < 0 || l.selectedIdx >= len(l.items) {
380+ return
381+ }
382+
383+ startIdx, endIdx := l.findVisibleItems()
384+ if l.selectedIdx < startIdx {
385+ // Selected item is above the visible range
386+ l.offsetIdx = l.selectedIdx
387+ l.offsetLine = 0
388+ } else if l.selectedIdx > endIdx {
389+ // Selected item is below the visible range
390+ // Scroll so that the selected item is at the bottom
391+ var totalHeight int
392+ for i := l.selectedIdx; i >= 0; i-- {
393+ item := l.getItem(i)
394+ totalHeight += item.height
395+ if l.gap > 0 && i < l.selectedIdx {
396+ totalHeight += l.gap
397+ }
398+ if totalHeight >= l.height {
399+ l.offsetIdx = i
400+ l.offsetLine = totalHeight - l.height
401+ break
402+ }
403+ }
404+ if totalHeight < l.height {
405+ // All items fit in the viewport
406+ l.ScrollToTop()
407+ }
408+ }
380}
409}
381
410
382// SelectedItemInView returns whether the selected item is currently in view.
411// SelectedItemInView returns whether the selected item is currently in view.