Skip to content

Commit

Permalink
fix(viewport): performance scrolling (#312)
Browse files Browse the repository at this point in the history
* fix(viewport): performance scroll behavior for line up/down

* fix(viewport): performance scrolling in multi-line operations
  • Loading branch information
meowgorithm authored Jan 20, 2023
1 parent 3099851 commit 4a71cee
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ func (m *Model) ViewDown() []string {
return nil
}

m.SetYOffset(m.YOffset + m.Height)
return m.visibleLines()
return m.LineDown(m.Height)
}

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

m.SetYOffset(m.YOffset - m.Height)
return m.visibleLines()
return m.LineUp(m.Height)
}

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

m.SetYOffset(m.YOffset + m.Height/2)
return m.visibleLines()
return m.LineDown(m.Height / 2)
}

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

m.SetYOffset(m.YOffset - m.Height/2)
return m.visibleLines()
return m.LineUp(m.Height / 2)
}

// LineDown moves the view down by the given number of lines.
func (m *Model) LineDown(n int) (lines []string) {
if m.AtBottom() || n == 0 {
if m.AtBottom() || n == 0 || len(m.lines) == 0 {
return nil
}

// Make sure the number of lines by which we're going to scroll isn't
// greater than the number of lines we actually have left before we reach
// the bottom.
m.SetYOffset(m.YOffset + n)
return m.visibleLines()

// Gather lines to send off for performance scrolling.
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines))
top := clamp(m.YOffset+m.Height-n, 0, bottom)
return m.lines[top:bottom]
}

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

// Make sure the number of lines by which we're going to scroll isn't
// greater than the number of lines we are from the top.
m.SetYOffset(m.YOffset - n)
return m.visibleLines()

// Gather lines to send off for performance scrolling.
top := max(0, m.YOffset)
bottom := clamp(m.YOffset+n, 0, m.maxYOffset())
return m.lines[top:bottom]
}

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

0 comments on commit 4a71cee

Please sign in to comment.