diff options
author | Jeffas <dev@jeffas.io> | 2020-06-09 20:13:13 +0100 |
---|---|---|
committer | Reto Brunner <reto@labrat.space> | 2020-06-09 21:52:10 +0200 |
commit | 15b72df1dabb6675c20cff043648e97a209d2132 (patch) | |
tree | 184b15a40d8aa242259f4ee46caebc51527f5e26 /widgets/msglist.go | |
parent | c6f4d7badd4cb36067f0e76198630a3d0f9e7ce9 (diff) | |
download | aerc-15b72df1dabb6675c20cff043648e97a209d2132.tar.gz |
Rework msglist scrolling
This changes the scrolling to be done on the draw, when the height is updated, ensuring that the selected item is kept on screen during resizing. Also, this ensures that messages will fill the screen when resizing the window, for instance, shrinking and then growing drags down more messages if possible. This is a transplant of the dirlist scrolling logic.
Diffstat (limited to 'widgets/msglist.go')
-rw-r--r-- | widgets/msglist.go | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/widgets/msglist.go b/widgets/msglist.go index 5aedb44..38b6369 100644 --- a/widgets/msglist.go +++ b/widgets/msglist.go @@ -63,6 +63,8 @@ func (ml *MessageList) Draw(ctx *ui.Context) { } } + ml.ensureScroll() + var ( needsHeaders []uint32 row int = 0 @@ -179,12 +181,12 @@ func (ml *MessageList) MouseEvent(localX int, localY int, event tcell.Event) { if ml.store != nil { ml.store.Next() } - ml.Scroll() + ml.Invalidate() case tcell.WheelUp: if ml.store != nil { ml.store.Prev() } - ml.Scroll() + ml.Invalidate() } } } @@ -225,7 +227,6 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) { ml.nmsgs = len(uids) } - ml.Scroll() ml.Invalidate() } @@ -266,25 +267,40 @@ func (ml *MessageList) Selected() *models.MessageInfo { func (ml *MessageList) Select(index int) { store := ml.Store() store.Select(index) - ml.Scroll() + ml.Invalidate() } -func (ml *MessageList) Scroll() { +func (ml *MessageList) ensureScroll() { store := ml.Store() - if store == nil || len(store.Uids()) == 0 { return } - if ml.Height() != 0 { - // I'm too lazy to do the math right now - for store.SelectedIndex()-ml.scroll >= ml.Height() { - ml.scroll += 1 - } - for store.SelectedIndex()-ml.scroll < 0 { - ml.scroll -= 1 + + h := ml.Height() + + maxScroll := len(store.Uids()) - h + if maxScroll < 0 { + maxScroll = 0 + } + + selectedIndex := store.SelectedIndex() + + if selectedIndex >= ml.scroll && selectedIndex < ml.scroll+h { + if ml.scroll > maxScroll { + ml.scroll = maxScroll } + return + } + + if selectedIndex >= ml.scroll+h { + ml.scroll = selectedIndex - h + 1 + } else if selectedIndex < ml.scroll { + ml.scroll = selectedIndex + } + + if ml.scroll > maxScroll { + ml.scroll = maxScroll } - ml.Invalidate() } func (ml *MessageList) drawEmptyMessage(ctx *ui.Context) { |