From 44160ef8448239e397c7bddbbd78bc9cd38327a3 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 30 Mar 2023 18:17:50 +0200 Subject: Only render visible portion of the screen for commits view --- pkg/gui/context/list_context_trait.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'pkg/gui/context/list_context_trait.go') diff --git a/pkg/gui/context/list_context_trait.go b/pkg/gui/context/list_context_trait.go index 78d524bb2..aca33cbd0 100644 --- a/pkg/gui/context/list_context_trait.go +++ b/pkg/gui/context/list_context_trait.go @@ -18,6 +18,9 @@ type ListContextTrait struct { // we should find out exactly which lines are now part of the path and refresh those. // We should also keep track of the previous path and refresh those lines too. refreshViewportOnChange bool + // If this is true, we only render the visible lines of the list. Useful for lists that can + // get very long, because it can save a lot of memory + renderOnlyVisibleLines bool } func (self *ListContextTrait) IsListContext() {} @@ -28,6 +31,8 @@ func (self *ListContextTrait) FocusLine() { // the view could be squashed and won't how to adjust the cursor/origin. // Also, refreshing the viewport needs to happen after the view has been resized. self.c.AfterLayout(func() error { + oldOrigin, _ := self.GetViewTrait().ViewPortYBounds() + self.GetViewTrait().FocusPoint( self.ModelIndexToViewIndex(self.list.GetSelectedLineIdx())) @@ -41,6 +46,11 @@ func (self *ListContextTrait) FocusLine() { if self.refreshViewportOnChange { self.refreshViewport() + } else if self.renderOnlyVisibleLines { + newOrigin, _ := self.GetViewTrait().ViewPortYBounds() + if oldOrigin != newOrigin { + return self.HandleRender() + } } return nil }) @@ -83,8 +93,21 @@ func (self *ListContextTrait) HandleFocusLost(opts types.OnFocusLostOpts) error // OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view func (self *ListContextTrait) HandleRender() error { self.list.ClampSelection() - content := self.renderLines(-1, -1) - self.GetViewTrait().SetContent(content) + if self.renderOnlyVisibleLines { + // Rendering only the visible area can save a lot of cell memory for + // those views that support it. + totalLength := self.list.Len() + if self.getNonModelItems != nil { + totalLength += len(self.getNonModelItems()) + } + self.GetViewTrait().SetContentLineCount(totalLength) + startIdx, length := self.GetViewTrait().ViewPortYBounds() + content := self.renderLines(startIdx, startIdx+length) + self.GetViewTrait().SetViewPortContentAndClearEverythingElse(content) + } else { + content := self.renderLines(-1, -1) + self.GetViewTrait().SetContent(content) + } self.c.Render() self.setFooter() -- cgit v1.2.3