Skip to content

Commit 52ffbc4

Browse files
committed
Avoid scrolling the selection into view on refresh
It is possible to scroll the selection out of view using the mouse wheel; after doing this, it would sometimes scroll into view by itself again, for example when a background fetch occurred. In the files panel this would even happen every 10s with every regular files refresh. Fix this by adding a scrollIntoView parameter to HandleFocus, which is false by default, and is only set to true from controllers that change the selection.
1 parent 56b3453 commit 52ffbc4

File tree

11 files changed

+24
-18
lines changed

11 files changed

+24
-18
lines changed

pkg/gui/context/list_context_trait.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type ListContextTrait struct {
2525

2626
func (self *ListContextTrait) IsListContext() {}
2727

28-
func (self *ListContextTrait) FocusLine() {
29-
self.Context.FocusLine()
28+
func (self *ListContextTrait) FocusLine(scrollIntoView bool) {
29+
self.Context.FocusLine(scrollIntoView)
3030

3131
// Doing this at the end of the layout function because we need the view to be
3232
// resized before we focus the line, otherwise if we're in accordion mode
@@ -36,7 +36,7 @@ func (self *ListContextTrait) FocusLine() {
3636
oldOrigin, _ := self.GetViewTrait().ViewPortYBounds()
3737

3838
self.GetViewTrait().FocusPoint(
39-
self.ModelIndexToViewIndex(self.list.GetSelectedLineIdx()))
39+
self.ModelIndexToViewIndex(self.list.GetSelectedLineIdx()), scrollIntoView)
4040

4141
selectRangeIndex, isSelectingRange := self.list.GetRangeStartIdx()
4242
if isSelectingRange {
@@ -75,7 +75,7 @@ func formatListFooter(selectedLineIdx int, length int) string {
7575
}
7676

7777
func (self *ListContextTrait) HandleFocus(opts types.OnFocusOpts) {
78-
self.FocusLine()
78+
self.FocusLine(opts.ScrollSelectionIntoView)
7979

8080
self.GetViewTrait().SetHighlight(self.list.Len() > 0)
8181

pkg/gui/context/simple_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
5454
}
5555
}
5656

57-
func (self *SimpleContext) FocusLine() {
57+
func (self *SimpleContext) FocusLine(scrollIntoView bool) {
5858
}
5959

6060
func (self *SimpleContext) HandleRender() {

pkg/gui/context/view_trait.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func NewViewTrait(view *gocui.View) *ViewTrait {
1717
return &ViewTrait{view: view}
1818
}
1919

20-
func (self *ViewTrait) FocusPoint(yIdx int) {
21-
self.view.FocusPoint(self.view.OriginX(), yIdx)
20+
func (self *ViewTrait) FocusPoint(yIdx int, scrollIntoView bool) {
21+
self.view.FocusPoint(self.view.OriginX(), yIdx, scrollIntoView)
2222
}
2323

2424
func (self *ViewTrait) SetRangeSelectStart(yIdx int) {

pkg/gui/controllers/helpers/cherry_pick_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (self *CherryPickHelper) Paste() error {
101101
// below the selection.
102102
if commit := self.c.Contexts().LocalCommits.GetSelected(); commit != nil && !commit.IsTODO() {
103103
self.c.Contexts().LocalCommits.MoveSelection(len(cherryPickedCommits))
104-
self.c.Contexts().LocalCommits.FocusLine()
104+
self.c.Contexts().LocalCommits.FocusLine(true)
105105
}
106106

107107
// If we're in the cherry-picking state at this point, it must

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func (self *RefsHelper) SelectFirstBranchAndFirstCommit() {
3535
self.c.Contexts().Branches.SetSelection(0)
3636
self.c.Contexts().ReflogCommits.SetSelection(0)
3737
self.c.Contexts().LocalCommits.SetSelection(0)
38+
self.c.Contexts().Branches.GetView().SetOriginY(0)
39+
self.c.Contexts().ReflogCommits.GetView().SetOriginY(0)
40+
self.c.Contexts().LocalCommits.GetView().SetOriginY(0)
3841
}
3942

4043
func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions) error {

pkg/gui/controllers/list_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (self *ListController) handleLineChangeAux(f func(int), change int) error {
116116
}
117117

118118
if cursorMoved || rangeBefore != rangeAfter {
119-
self.context.HandleFocus(types.OnFocusOpts{})
119+
self.context.HandleFocus(types.OnFocusOpts{ScrollSelectionIntoView: true})
120120
}
121121

122122
return nil
@@ -173,6 +173,8 @@ func (self *ListController) handlePageChange(delta int) error {
173173
}
174174
}
175175

176+
// Since we are maintaining the scroll position ourselves above, there's no point in passing
177+
// ScrollSelectionIntoView=true here.
176178
self.context.HandleFocus(types.OnFocusOpts{})
177179

178180
return nil

pkg/gui/controllers/local_commits_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ func (self *LocalCommitsController) revert(commits []*models.Commit, start, end
869869
return err
870870
}
871871
self.context().MoveSelection(len(commits))
872-
self.context().HandleFocus(types.OnFocusOpts{})
872+
self.context().HandleFocus(types.OnFocusOpts{ScrollSelectionIntoView: true})
873873

874874
if mustStash {
875875
if err := self.c.Git().Stash.Pop(0); err != nil {

pkg/gui/controllers/stash_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (self *StashController) handleRenameStashEntry(stashEntry *models.StashEntr
205205
return err
206206
}
207207
self.context().SetSelection(0) // Select the renamed stash
208-
self.context().FocusLine()
208+
self.context().FocusLine(true)
209209
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
210210
return nil
211211
},

pkg/gui/types/context.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type Context interface {
109109

110110
HandleFocus(opts OnFocusOpts)
111111
HandleFocusLost(opts OnFocusLostOpts)
112-
FocusLine()
112+
FocusLine(scrollIntoView bool)
113113
HandleRender()
114114
HandleRenderToMain()
115115
}
@@ -201,7 +201,7 @@ type IPatchExplorerContext interface {
201201
}
202202

203203
type IViewTrait interface {
204-
FocusPoint(yIdx int)
204+
FocusPoint(yIdx int, scrollIntoView bool)
205205
SetRangeSelectStart(yIdx int)
206206
CancelRangeSelect()
207207
SetViewPortContent(content string)
@@ -221,8 +221,9 @@ type IViewTrait interface {
221221
}
222222

223223
type OnFocusOpts struct {
224-
ClickedWindowName string
225-
ClickedViewLineIdx int
224+
ClickedWindowName string
225+
ClickedViewLineIdx int
226+
ScrollSelectionIntoView bool
226227
}
227228

228229
type OnFocusLostOpts struct {

pkg/gui/view_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (gui *Gui) postRefreshUpdate(c types.Context) {
140140
// non-focused views to ensure that an inactive selection is painted
141141
// correctly, and that integration tests see the up to date selection
142142
// state.
143-
c.FocusLine()
143+
c.FocusLine(false)
144144

145145
currentCtx := gui.State.ContextMgr.Current()
146146
if currentCtx.GetKey() == context.NORMAL_MAIN_CONTEXT_KEY || currentCtx.GetKey() == context.NORMAL_SECONDARY_CONTEXT_KEY {

0 commit comments

Comments
 (0)