@@ -170,27 +170,37 @@ func (l *List) ScrollBy(lines int) {
170170 }
171171 } else if lines < 0 {
172172 // Scroll up
173− l.offsetLine += lines
174− for l.offsetLine < 0 {
175− if l.offsetIdx == 0 {
176− // Reached the top of the list
177− l.offsetLine = 0
173+ // Calculate from offset how many items needed to fill the viewport
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
180+ if l.gap > 0 && i < l.offsetIdx {
181+ totalLines += l.gap
182+ }
183+ if totalLines >= l.height {
184+ firstItemIdx = i
178185 break
179186 }
187+ }
180188
189+ // Now scroll up by lines
190+ l.offsetLine += lines // lines is negative
191+ for l.offsetIdx > firstItemIdx && l.offsetLine < 0 {
181192 // Move to previous item
182193 l.offsetIdx--
183− item := l.getItem(l.offsetIdx)
184− totalHeight := item.height
194+ prevItem := l.getItem(l.offsetIdx)
195+ totalHeight := prevItem.height
185196 if l.gap > 0 {
186197 totalHeight += l.gap
187198 }
188199 l.offsetLine += totalHeight
189200 }
190201
191− item := l.getItem(l.offsetIdx)
192− if l.offsetLine >= item.height {
193− l.offsetLine = item.height - 1
202+ if l.offsetLine < 0 {
203+ l.offsetLine = 0
194204 }
195205 }
196206}