Skip to content

Commit 095eb13

Browse files
authored
Fix scroll position when entering staging view by clicking in the main view (jesseduffield#3992)
This is both a bug fix and a behavior change: - The bug fix is that when entering the staging view for a selected file by clicking in the main view, and the main view was scrolled down, then it would scroll to the top and show an ugly range selection from the line that was clicked to the line that is now under the mouse after scrolling up. - The behavior change is that when leaving the staging view by pressing escape, it now retains its scroll position rather than scrolling back up to the top. In addition, this fixes a minor flickering issue when leaving the staging view. Note that maintaining the scroll position when going into and out of the staging view is not always perfect for two reasons: - the focused staging view does not wrap long lines, but the unfocused one does - a pager like delta might change the number of lines e.g. of the diff hunks For the second problem there's little we can do, but the first one will be improved once we wrap lines in the focused staging view (see jesseduffield#1384 and jesseduffield#3558), which I'm currently working on. - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [ ] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] If a new UserConfig entry was added, make sure it can be hot-reloaded (see [here](https://github.com/jesseduffield/lazygit/blob/master/docs/dev/Codebase_Guide.md#using-userconfig)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
2 parents 4883c86 + f08b3e9 commit 095eb13

File tree

8 files changed

+22
-26
lines changed

8 files changed

+22
-26
lines changed

pkg/gui/context/patch_explorer_context.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewPatchExplorerContext(
5353
func(selectedLineIdx int) error {
5454
ctx.GetMutex().Lock()
5555
defer ctx.GetMutex().Unlock()
56-
ctx.NavigateTo(ctx.c.Context().IsCurrent(ctx), selectedLineIdx)
56+
ctx.NavigateTo(selectedLineIdx)
5757
return nil
5858
}),
5959
)
@@ -79,15 +79,15 @@ func (self *PatchExplorerContext) GetIncludedLineIndices() []int {
7979
return self.getIncludedLineIndices()
8080
}
8181

82-
func (self *PatchExplorerContext) RenderAndFocus(isFocused bool) {
83-
self.setContent(isFocused)
82+
func (self *PatchExplorerContext) RenderAndFocus() {
83+
self.setContent()
8484

8585
self.FocusSelection()
8686
self.c.Render()
8787
}
8888

89-
func (self *PatchExplorerContext) Render(isFocused bool) {
90-
self.setContent(isFocused)
89+
func (self *PatchExplorerContext) Render() {
90+
self.setContent()
9191

9292
self.c.Render()
9393
}
@@ -97,8 +97,8 @@ func (self *PatchExplorerContext) Focus() {
9797
self.c.Render()
9898
}
9999

100-
func (self *PatchExplorerContext) setContent(isFocused bool) {
101-
self.GetView().SetContent(self.GetContentToRender(isFocused))
100+
func (self *PatchExplorerContext) setContent() {
101+
self.GetView().SetContent(self.GetContentToRender())
102102
}
103103

104104
func (self *PatchExplorerContext) FocusSelection() {
@@ -119,19 +119,19 @@ func (self *PatchExplorerContext) FocusSelection() {
119119
view.SetCursorY(endIdx - newOriginY)
120120
}
121121

122-
func (self *PatchExplorerContext) GetContentToRender(isFocused bool) string {
122+
func (self *PatchExplorerContext) GetContentToRender() string {
123123
if self.GetState() == nil {
124124
return ""
125125
}
126126

127-
return self.GetState().RenderForLineIndices(isFocused, self.GetIncludedLineIndices())
127+
return self.GetState().RenderForLineIndices(self.GetIncludedLineIndices())
128128
}
129129

130-
func (self *PatchExplorerContext) NavigateTo(isFocused bool, selectedLineIdx int) {
130+
func (self *PatchExplorerContext) NavigateTo(selectedLineIdx int) {
131131
self.GetState().SetLineSelectMode()
132132
self.GetState().SelectLine(selectedLineIdx)
133133

134-
self.RenderAndFocus(isFocused)
134+
self.RenderAndFocus()
135135
}
136136

137137
func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex {

pkg/gui/controllers/helpers/patch_building_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpt
9898
return
9999
}
100100

101-
mainContent := context.GetContentToRender(true)
101+
mainContent := context.GetContentToRender()
102102

103103
self.c.Contexts().CustomPatchBuilder.FocusSelection()
104104

pkg/gui/controllers/helpers/staging_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (self *StagingHelper) RefreshStagingPanel(focusOpts types.OnFocusOpts) {
7373
mainState := mainContext.GetState()
7474
secondaryState := secondaryContext.GetState()
7575

76-
mainContent := mainContext.GetContentToRender(!secondaryFocused)
77-
secondaryContent := secondaryContext.GetContentToRender(secondaryFocused)
76+
mainContent := mainContext.GetContentToRender()
77+
secondaryContent := secondaryContext.GetContentToRender()
7878

7979
mainContext.GetMutex().Unlock()
8080
secondaryContext.GetMutex().Unlock()

pkg/gui/controllers/patch_explorer_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (self *PatchExplorerController) withRenderAndFocus(f func() error) func() e
302302
return err
303303
}
304304

305-
self.context.RenderAndFocus(self.isFocused())
305+
self.context.RenderAndFocus()
306306
return nil
307307
})
308308
}

pkg/gui/controllers/staging_controller.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ func (self *StagingController) GetOnFocusLost() func(types.OnFocusLostOpts) {
132132
if opts.NewContextKey != self.otherContext.GetKey() {
133133
self.c.Views().Staging.Wrap = true
134134
self.c.Views().StagingSecondary.Wrap = true
135-
self.c.Contexts().Staging.Render(false)
136-
self.c.Contexts().StagingSecondary.Render(false)
137135
}
138136
}
139137
}

pkg/gui/patch_exploring/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (s *State) AdjustSelectedLineIdx(change int) {
249249
s.SelectLine(s.selectedLineIdx + change)
250250
}
251251

252-
func (s *State) RenderForLineIndices(isFocused bool, includedLineIndices []int) string {
252+
func (s *State) RenderForLineIndices(includedLineIndices []int) string {
253253
includedLineIndicesSet := set.NewFromSlice(includedLineIndices)
254254
return s.patch.FormatView(patch.FormatViewOpts{
255255
IncLineIndices: includedLineIndicesSet,

pkg/gui/tasks_adapter.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ func (gui *Gui) newStringTaskWithoutScroll(view *gocui.View, str string) error {
5353
return nil
5454
}
5555

56-
// Using empty key so that on subsequent calls we won't reset the view's origin.
57-
// Note this means that we will be scrolling back to the top if we're switching from a different key
58-
if err := manager.NewTask(f, ""); err != nil {
56+
if err := manager.NewTask(f, manager.GetTaskKey()); err != nil {
5957
return err
6058
}
6159

@@ -71,7 +69,7 @@ func (gui *Gui) newStringTaskWithScroll(view *gocui.View, str string, originX in
7169
return nil
7270
}
7371

74-
if err := manager.NewTask(f, ""); err != nil {
72+
if err := manager.NewTask(f, manager.GetTaskKey()); err != nil {
7573
return err
7674
}
7775

pkg/gui/types/context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ type IPatchExplorerContext interface {
177177
GetState() *patch_exploring.State
178178
SetState(*patch_exploring.State)
179179
GetIncludedLineIndices() []int
180-
RenderAndFocus(isFocused bool)
181-
Render(isFocused bool)
180+
RenderAndFocus()
181+
Render()
182182
Focus()
183-
GetContentToRender(isFocused bool) string
184-
NavigateTo(isFocused bool, selectedLineIdx int)
183+
GetContentToRender() string
184+
NavigateTo(selectedLineIdx int)
185185
GetMutex() *deadlock.Mutex
186186
IsPatchExplorerContext() // used for type switch
187187
}

0 commit comments

Comments
 (0)