From 81eebf041f71e54e8bd864cc2c356dd518995648 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 18 Jan 2023 17:18:19 -0500 Subject: [PATCH 1/2] fix(viewport): performance scroll behavior for line up/down --- viewport/viewport.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/viewport/viewport.go b/viewport/viewport.go index f67f57da..2e5f1e91 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -183,7 +183,7 @@ func (m *Model) HalfViewUp() (lines []string) { // 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 } @@ -191,20 +191,28 @@ func (m *Model) LineDown(n int) (lines []string) { // 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. From 8a783ec2ac5f4326d00972572e567367970eb7c6 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 18 Jan 2023 21:56:29 -0500 Subject: [PATCH 2/2] fix(viewport): performance scrolling in multi-line operations --- viewport/viewport.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/viewport/viewport.go b/viewport/viewport.go index 2e5f1e91..b2dfa2cb 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -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". @@ -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. @@ -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. @@ -177,8 +174,7 @@ 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.