diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-16 15:27:56 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-16 17:29:17 +1000 |
commit | e68093fe9974296fe5afb22ec8e0adb1eb2f4316 (patch) | |
tree | 040dbfdb4dc438271d9f3febae410d25698f1a38 /vendor/github.com/jesseduffield/gocui/view.go | |
parent | b838b74801102b27782cc20c7396279630e77fba (diff) |
add scrollbars
Diffstat (limited to 'vendor/github.com/jesseduffield/gocui/view.go')
-rw-r--r-- | vendor/github.com/jesseduffield/gocui/view.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/github.com/jesseduffield/gocui/view.go b/vendor/github.com/jesseduffield/gocui/view.go index d2c2b75f1..8051cce72 100644 --- a/vendor/github.com/jesseduffield/gocui/view.go +++ b/vendor/github.com/jesseduffield/gocui/view.go @@ -158,6 +158,9 @@ type View struct { // something like '1 of 20' for a list view Footer string + + // if true, the user can scroll all the way past the last item until it appears at the top of the view + CanScrollPastBottom bool } // call this in the event of a view resize, or if you want to render new content @@ -1287,3 +1290,69 @@ func (v *View) OverwriteLines(y int, content string) { lines := strings.Replace(content, "\n", "\x1b[K\n", -1) v.writeString(lines) } + +func (v *View) ScrollUp(amount int) { + newOy := v.oy - amount + if newOy < 0 { + newOy = 0 + } + v.oy = newOy +} + +// ensures we don't scroll past the end of the view's content +func (v *View) ScrollDown(amount int) { + adjustedAmount := v.adjustDownwardScrollAmount(amount) + if adjustedAmount > 0 { + v.oy += adjustedAmount + } +} + +func (v *View) ScrollLeft(amount int) { + newOx := v.ox - amount + if newOx < 0 { + newOx = 0 + } + v.ox = newOx +} + +// not applying any limits to this +func (v *View) ScrollRight(amount int) { + v.ox += amount +} + +func (v *View) adjustDownwardScrollAmount(scrollHeight int) int { + _, oy := v.Origin() + y := oy + if !v.CanScrollPastBottom { + _, sy := v.Size() + y += sy + } + scrollableLines := v.ViewLinesHeight() - y + if scrollableLines < 0 { + return 0 + } + + margin := v.scrollMargin() + if scrollableLines-margin < scrollHeight { + scrollHeight = scrollableLines - margin + } + if oy+scrollHeight < 0 { + return 0 + } else { + return scrollHeight + } +} + +// scrollMargin is about how many lines must still appear if you scroll +// all the way down. We'll subtract this from the total amount of scrollable lines +func (v *View) scrollMargin() int { + if v.CanScrollPastBottom { + // Setting to 2 because of the newline at the end of the file that we're likely showing. + // If we want to scroll past bottom outside the context of reading a file's contents, + // we should make this into a field on the view to be configured by the client. + // For now we're hardcoding it. + return 2 + } else { + return 0 + } +} |