on main help sign in
fix(ui): fix scrolling up last item
f79c9ceAyman Bagabas
internal/ui/lazylist/list.go+20−10
@@ -170,27 +170,37 @@ func (l *List) ScrollBy(lines int) {
170 }
170 }
171 } else if lines < 0 {
171 } else if lines < 0 {
172 // Scroll up
172 // Scroll up
173− l.offsetLine += lines
173+ // Calculate from offset how many items needed to fill the viewport
174− for l.offsetLine < 0 {
174+ // This is needed to know when to stop scrolling up
175+ var totalLines int
176+ var firstItemIdx int
177+ for i := l.offsetIdx; i >= 0; i-- {
178+ item := l.getItem(i)
179+ totalLines += item.height
175− if l.offsetIdx == 0 {
180+ if l.gap > 0 && i < l.offsetIdx {
176− // Reached the top of the list
181+ totalLines += l.gap
177− l.offsetLine = 0
182+ }
183+ if totalLines >= l.height {
184+ firstItemIdx = i
178 break
185 break
179 }
186 }
187+ }
180
188
189+ // Now scroll up by lines
190+ l.offsetLine += lines // lines is negative
191+ for l.offsetIdx > firstItemIdx && l.offsetLine < 0 {
181 // Move to previous item
192 // Move to previous item
182 l.offsetIdx--
193 l.offsetIdx--
183− item := l.getItem(l.offsetIdx)
194+ prevItem := l.getItem(l.offsetIdx)
184− totalHeight := item.height
195+ totalHeight := prevItem.height
185 if l.gap > 0 {
196 if l.gap > 0 {
186 totalHeight += l.gap
197 totalHeight += l.gap
187 }
198 }
188 l.offsetLine += totalHeight
199 l.offsetLine += totalHeight
189 }
200 }
190
201
191− item := l.getItem(l.offsetIdx)
192− if l.offsetLine >= item.height {
202+ if l.offsetLine < 0 {
193− l.offsetLine = item.height - 1
203+ l.offsetLine = 0
194 }
204 }
195 }
205 }
196}
206}