-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Jump to middle of the view when selection leaves the visible area #2915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ebdfd80
Bump gocui
stefanhaller 4a4afc4
Fix typo in comment
stefanhaller 79c11a0
If selected line is outside, move it to the middle of the view
stefanhaller 8f164f7
Stop cycling hunks when reaching the end
stefanhaller 341b972
Add ScrollOffMargin user config
stefanhaller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/jesseduffield/lazygit/pkg/gui/types" | ||
"github.com/jesseduffield/lazygit/pkg/utils" | ||
) | ||
|
||
// To be called after pressing up-arrow; checks whether the cursor entered the | ||
// top scroll-off margin, and so the view needs to be scrolled up one line | ||
func checkScrollUp(view types.IViewTrait, scrollOffMargin int, lineIdxBefore int, lineIdxAfter int) { | ||
viewPortStart, viewPortHeight := view.ViewPortYBounds() | ||
|
||
linesToScroll := calculateLinesToScrollUp( | ||
viewPortStart, viewPortHeight, scrollOffMargin, lineIdxBefore, lineIdxAfter) | ||
if linesToScroll != 0 { | ||
view.ScrollUp(linesToScroll) | ||
} | ||
} | ||
|
||
// To be called after pressing down-arrow; checks whether the cursor entered the | ||
// bottom scroll-off margin, and so the view needs to be scrolled down one line | ||
func checkScrollDown(view types.IViewTrait, scrollOffMargin int, lineIdxBefore int, lineIdxAfter int) { | ||
viewPortStart, viewPortHeight := view.ViewPortYBounds() | ||
|
||
linesToScroll := calculateLinesToScrollDown( | ||
viewPortStart, viewPortHeight, scrollOffMargin, lineIdxBefore, lineIdxAfter) | ||
if linesToScroll != 0 { | ||
view.ScrollDown(linesToScroll) | ||
} | ||
} | ||
|
||
func calculateLinesToScrollUp(viewPortStart int, viewPortHeight int, scrollOffMargin int, lineIdxBefore int, lineIdxAfter int) int { | ||
// Cap the margin to half the view height. This allows setting the config to | ||
// a very large value to keep the cursor always in the middle of the screen. | ||
// Use +.5 so that if the height is even, the top margin is one line higher | ||
// than the bottom margin. | ||
scrollOffMargin = utils.Min(scrollOffMargin, int((float64(viewPortHeight)+.5)/2)) | ||
|
||
// Scroll only if the "before" position was visible (this could be false if | ||
// the scroll wheel was used to scroll the selected line out of view) ... | ||
if lineIdxBefore >= viewPortStart && lineIdxBefore < viewPortStart+viewPortHeight { | ||
marginEnd := viewPortStart + scrollOffMargin | ||
// ... and the "after" position is within the top margin (or before it) | ||
if lineIdxAfter < marginEnd { | ||
return marginEnd - lineIdxAfter | ||
} | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func calculateLinesToScrollDown(viewPortStart int, viewPortHeight int, scrollOffMargin int, lineIdxBefore int, lineIdxAfter int) int { | ||
// Cap the margin to half the view height. This allows setting the config to | ||
// a very large value to keep the cursor always in the middle of the screen. | ||
// Use -.5 so that if the height is even, the bottom margin is one line lower | ||
// than the top margin. | ||
scrollOffMargin = utils.Min(scrollOffMargin, int((float64(viewPortHeight)-.5)/2)) | ||
|
||
// Scroll only if the "before" position was visible (this could be false if | ||
// the scroll wheel was used to scroll the selected line out of view) ... | ||
if lineIdxBefore >= viewPortStart && lineIdxBefore < viewPortStart+viewPortHeight { | ||
marginStart := viewPortStart + viewPortHeight - scrollOffMargin - 1 | ||
// ... and the "after" position is within the bottom margin (or after it) | ||
if lineIdxAfter > marginStart { | ||
return lineIdxAfter - marginStart | ||
} | ||
} | ||
|
||
return 0 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package controllers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_calculateLinesToScrollUp(t *testing.T) { | ||
scenarios := []struct { | ||
name string | ||
viewPortStart int | ||
viewPortHeight int | ||
scrollOffMargin int | ||
lineIdxBefore int | ||
lineIdxAfter int | ||
expectedLinesToScroll int | ||
}{ | ||
{ | ||
name: "before position is above viewport - don't scroll", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 9, | ||
lineIdxAfter: 8, | ||
expectedLinesToScroll: 0, | ||
}, | ||
{ | ||
name: "before position is below viewport - don't scroll", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 20, | ||
lineIdxAfter: 19, | ||
expectedLinesToScroll: 0, | ||
}, | ||
{ | ||
name: "before and after positions are outside scroll-off margin - don't scroll", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 14, | ||
lineIdxAfter: 13, | ||
expectedLinesToScroll: 0, | ||
}, | ||
{ | ||
name: "before outside, after inside scroll-off margin - scroll by 1", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 13, | ||
lineIdxAfter: 12, | ||
expectedLinesToScroll: 1, | ||
}, | ||
{ | ||
name: "before inside scroll-off margin - scroll by more than 1", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 11, | ||
lineIdxAfter: 10, | ||
expectedLinesToScroll: 3, | ||
}, | ||
{ | ||
name: "very large scroll-off margin - keep view centered (even viewport height)", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 999, | ||
lineIdxBefore: 15, | ||
lineIdxAfter: 14, | ||
expectedLinesToScroll: 1, | ||
}, | ||
{ | ||
name: "very large scroll-off margin - keep view centered (odd viewport height)", | ||
viewPortStart: 10, | ||
viewPortHeight: 9, | ||
scrollOffMargin: 999, | ||
lineIdxBefore: 14, | ||
lineIdxAfter: 13, | ||
expectedLinesToScroll: 1, | ||
}, | ||
} | ||
for _, scenario := range scenarios { | ||
t.Run(scenario.name, func(t *testing.T) { | ||
linesToScroll := calculateLinesToScrollUp(scenario.viewPortStart, scenario.viewPortHeight, scenario.scrollOffMargin, scenario.lineIdxBefore, scenario.lineIdxAfter) | ||
assert.Equal(t, scenario.expectedLinesToScroll, linesToScroll) | ||
}) | ||
} | ||
} | ||
|
||
func Test_calculateLinesToScrollDown(t *testing.T) { | ||
scenarios := []struct { | ||
name string | ||
viewPortStart int | ||
viewPortHeight int | ||
scrollOffMargin int | ||
lineIdxBefore int | ||
lineIdxAfter int | ||
expectedLinesToScroll int | ||
}{ | ||
{ | ||
name: "before position is above viewport - don't scroll", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 9, | ||
lineIdxAfter: 10, | ||
expectedLinesToScroll: 0, | ||
}, | ||
{ | ||
name: "before position is below viewport - don't scroll", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 20, | ||
lineIdxAfter: 21, | ||
expectedLinesToScroll: 0, | ||
}, | ||
{ | ||
name: "before and after positions are outside scroll-off margin - don't scroll", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 15, | ||
lineIdxAfter: 16, | ||
expectedLinesToScroll: 0, | ||
}, | ||
{ | ||
name: "before outside, after inside scroll-off margin - scroll by 1", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 16, | ||
lineIdxAfter: 17, | ||
expectedLinesToScroll: 1, | ||
}, | ||
{ | ||
name: "before inside scroll-off margin - scroll by more than 1", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 3, | ||
lineIdxBefore: 18, | ||
lineIdxAfter: 19, | ||
expectedLinesToScroll: 3, | ||
}, | ||
{ | ||
name: "very large scroll-off margin - keep view centered (even viewport height)", | ||
viewPortStart: 10, | ||
viewPortHeight: 10, | ||
scrollOffMargin: 999, | ||
lineIdxBefore: 15, | ||
lineIdxAfter: 16, | ||
expectedLinesToScroll: 1, | ||
}, | ||
{ | ||
name: "very large scroll-off margin - keep view centered (odd viewport height)", | ||
viewPortStart: 10, | ||
viewPortHeight: 9, | ||
scrollOffMargin: 999, | ||
lineIdxBefore: 14, | ||
lineIdxAfter: 15, | ||
expectedLinesToScroll: 1, | ||
}, | ||
} | ||
for _, scenario := range scenarios { | ||
t.Run(scenario.name, func(t *testing.T) { | ||
linesToScroll := calculateLinesToScrollDown(scenario.viewPortStart, scenario.viewPortHeight, scenario.scrollOffMargin, scenario.lineIdxBefore, scenario.lineIdxAfter) | ||
assert.Equal(t, scenario.expectedLinesToScroll, linesToScroll) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the other branch we will have to call
ModelToViewIndex
onbefore
andafter
to make it work correctly for lists that have section headers.