diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-02-25 17:53:33 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-03-03 21:11:05 +0100 |
commit | 515a8b56f6e9b4e6efaf6a6a29c851dadf4b4a56 (patch) | |
tree | 773eb7af099ea985087d76db41c40d36f81cc153 /widgets/dirtree.go | |
parent | bd65ce1010a78eec38ba9c70d9fc23f85cd087a1 (diff) | |
download | aerc-515a8b56f6e9b4e6efaf6a6a29c851dadf4b4a56.tar.gz |
scrollable: extract scrolling behavior for reuse
Extract the vertical scrolling ability into its own Scrollable struct
that can be embedded and reused across any ui element that relies on
scrolling.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'widgets/dirtree.go')
-rw-r--r-- | widgets/dirtree.go | 45 |
1 files changed, 7 insertions, 38 deletions
diff --git a/widgets/dirtree.go b/widgets/dirtree.go index 52195d8a..cf4575f7 100644 --- a/widgets/dirtree.go +++ b/widgets/dirtree.go @@ -40,7 +40,7 @@ func (dt *DirectoryTree) UpdateList(done func([]string)) { dt.buildTree() dt.listIdx = findString(dt.dirs, dt.selecting) dt.Select(dt.selecting) - dt.scroll = 0 + dt.Scrollable = Scrollable{} }) } @@ -60,7 +60,8 @@ func (dt *DirectoryTree) Draw(ctx *ui.Context) { return } - dt.ensureScroll(ctx.Height()) + dt.UpdateScroller(ctx.Height(), n) + dt.EnsureScroll(dt.countVisible(dt.list[:dt.listIdx])) needScrollbar := true percentVisible := float64(ctx.Height()) / float64(n) @@ -78,10 +79,10 @@ func (dt *DirectoryTree) Draw(ctx *ui.Context) { rowNr := 0 for i, node := range dt.list { - if i < dt.scroll || !isVisible(node) { + if i < dt.Scroll() || !isVisible(node) { continue } - row := rowNr - dt.scroll + row := rowNr - dt.Scroll() if row >= ctx.Height() { break } @@ -105,41 +106,9 @@ func (dt *DirectoryTree) Draw(ctx *ui.Context) { ctx.Printf(0, row, style, dirString) } - if needScrollbar { + if dt.NeedScrollbar() { scrollBarCtx := ctx.Subcontext(ctx.Width()-1, 0, 1, ctx.Height()) - dt.drawScrollbar(scrollBarCtx, percentVisible) - } -} - -func (dt *DirectoryTree) ensureScroll(h int) { - selectingIdx := dt.countVisible(dt.list[:dt.listIdx]) - if selectingIdx < 0 { - // dir not found, meaning we are currently adding / removing a dir. - // we can simply ignore this until we get redrawn with the new - // dirlist.dir content - return - } - - maxScroll := dt.countVisible(dt.list) - h - if maxScroll < 0 { - maxScroll = 0 - } - - if selectingIdx >= dt.scroll && selectingIdx < dt.scroll+h { - if dt.scroll > maxScroll { - dt.scroll = maxScroll - } - return - } - - if selectingIdx >= dt.scroll+h { - dt.scroll = selectingIdx - h + 1 - } else if selectingIdx < dt.scroll { - dt.scroll = selectingIdx - } - - if dt.scroll > maxScroll { - dt.scroll = maxScroll + dt.drawScrollbar(scrollBarCtx) } } |