Skip to content

Commit 4a71cee

Browse files
authored
fix(viewport): performance scrolling (#312)
* fix(viewport): performance scroll behavior for line up/down * fix(viewport): performance scrolling in multi-line operations
1 parent 3099851 commit 4a71cee

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

viewport/viewport.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ func (m *Model) ViewDown() []string {
147147
return nil
148148
}
149149

150-
m.SetYOffset(m.YOffset + m.Height)
151-
return m.visibleLines()
150+
return m.LineDown(m.Height)
152151
}
153152

154153
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
@@ -157,8 +156,7 @@ func (m *Model) ViewUp() []string {
157156
return nil
158157
}
159158

160-
m.SetYOffset(m.YOffset - m.Height)
161-
return m.visibleLines()
159+
return m.LineUp(m.Height)
162160
}
163161

164162
// HalfViewDown moves the view down by half the height of the viewport.
@@ -167,8 +165,7 @@ func (m *Model) HalfViewDown() (lines []string) {
167165
return nil
168166
}
169167

170-
m.SetYOffset(m.YOffset + m.Height/2)
171-
return m.visibleLines()
168+
return m.LineDown(m.Height / 2)
172169
}
173170

174171
// HalfViewUp moves the view up by half the height of the viewport.
@@ -177,34 +174,41 @@ func (m *Model) HalfViewUp() (lines []string) {
177174
return nil
178175
}
179176

180-
m.SetYOffset(m.YOffset - m.Height/2)
181-
return m.visibleLines()
177+
return m.LineUp(m.Height / 2)
182178
}
183179

184180
// LineDown moves the view down by the given number of lines.
185181
func (m *Model) LineDown(n int) (lines []string) {
186-
if m.AtBottom() || n == 0 {
182+
if m.AtBottom() || n == 0 || len(m.lines) == 0 {
187183
return nil
188184
}
189185

190186
// Make sure the number of lines by which we're going to scroll isn't
191187
// greater than the number of lines we actually have left before we reach
192188
// the bottom.
193189
m.SetYOffset(m.YOffset + n)
194-
return m.visibleLines()
190+
191+
// Gather lines to send off for performance scrolling.
192+
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines))
193+
top := clamp(m.YOffset+m.Height-n, 0, bottom)
194+
return m.lines[top:bottom]
195195
}
196196

197197
// LineUp moves the view down by the given number of lines. Returns the new
198198
// lines to show.
199199
func (m *Model) LineUp(n int) (lines []string) {
200-
if m.AtTop() || n == 0 {
200+
if m.AtTop() || n == 0 || len(m.lines) == 0 {
201201
return nil
202202
}
203203

204204
// Make sure the number of lines by which we're going to scroll isn't
205205
// greater than the number of lines we are from the top.
206206
m.SetYOffset(m.YOffset - n)
207-
return m.visibleLines()
207+
208+
// Gather lines to send off for performance scrolling.
209+
top := max(0, m.YOffset)
210+
bottom := clamp(m.YOffset+n, 0, m.maxYOffset())
211+
return m.lines[top:bottom]
208212
}
209213

210214
// TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.

0 commit comments

Comments
 (0)