@@ -139,23 +139,41 @@ func (self *ListController) handlePageChange(delta int) error {
139139view := self .context .GetViewTrait ()
140140
141141before := list .GetSelectedLineIdx ()
142- afterViewIdx := self .context .ModelIndexToViewIndex (before ) + delta
143- newModelIndex := self .context .ViewIndexToModelIndex (afterViewIdx )
144- list .MoveSelectedLine (newModelIndex - before )
145142
146143viewPortStart , viewPortHeight := view .ViewPortYBounds ()
144+ beforeViewIdx := self .context .ModelIndexToViewIndex (before )
145+ afterViewIdx := beforeViewIdx + delta
146+ newModelIndex := self .context .ViewIndexToModelIndex (afterViewIdx )
147147
148148if delta < 0 {
149149// Previous page: keep selection at top of viewport
150- linesToScroll := afterViewIdx - viewPortStart
151- if linesToScroll < 0 {
152- view .ScrollUp (- linesToScroll )
150+ indexAtTopOfPage := self .context .ViewIndexToModelIndex (viewPortStart )
151+ if before != indexAtTopOfPage {
152+ // If the selection isn't already at the top of the page, move it there without scrolling
153+ list .MoveSelectedLine (indexAtTopOfPage - before )
154+ } else {
155+ // Otherwise, move the selection by one page and scroll
156+ list .MoveSelectedLine (newModelIndex - before )
157+
158+ linesToScroll := afterViewIdx - viewPortStart
159+ if linesToScroll < 0 {
160+ view .ScrollUp (- linesToScroll )
161+ }
153162}
154163} else {
155164// Next page: keep selection at bottom of viewport
156- linesToScroll := afterViewIdx - (viewPortStart + viewPortHeight - 1 )
157- if linesToScroll > 0 {
158- view .ScrollDown (linesToScroll )
165+ indexAtBottomOfPage := self .context .ViewIndexToModelIndex (viewPortStart + viewPortHeight - 1 )
166+ if before != indexAtBottomOfPage {
167+ // If the selection isn't already at the bottom of the page, move it there without scrolling
168+ list .MoveSelectedLine (indexAtBottomOfPage - before )
169+ } else {
170+ // Otherwise, move the selection by one page and scroll
171+ list .MoveSelectedLine (newModelIndex - before )
172+
173+ linesToScroll := afterViewIdx - (viewPortStart + viewPortHeight - 1 )
174+ if linesToScroll > 0 {
175+ view .ScrollDown (linesToScroll )
176+ }
159177}
160178}
161179
0 commit comments