Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(viewport): performance scrolling #312

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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