From c1286661090731729e76e48d5e47988ad0175a20 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 25 Mar 2018 12:35:50 +0300 Subject: Refactor things and add proper wheel scroll support --- ui/widget/message-view.go | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'ui/widget') diff --git a/ui/widget/message-view.go b/ui/widget/message-view.go index a3a7942..50de405 100644 --- a/ui/widget/message-view.go +++ b/ui/widget/message-view.go @@ -211,46 +211,41 @@ func (view *MessageView) recalculateBuffers() { const PaddingAtTop = 5 -func (view *MessageView) MoveUp(page bool) { +func (view *MessageView) AddScrollOffset(diff int) { _, _, _, height := view.GetInnerRect() totalHeight := len(view.textBuffer) - if view.ScrollOffset >= totalHeight-height { + if diff >= 0 && view.ScrollOffset >= totalHeight-height { // If the user is at the top and presses page up again, add a bit of blank space. - if page { + if view.ScrollOffset+diff >= totalHeight-height+PaddingAtTop { view.ScrollOffset = totalHeight - height + PaddingAtTop - } else if view.ScrollOffset < totalHeight-height+PaddingAtTop { - view.ScrollOffset++ + } else { + view.ScrollOffset += diff } return } - if page { - view.ScrollOffset += height / 2 - } else { - view.ScrollOffset++ - } + view.ScrollOffset += diff if view.ScrollOffset > totalHeight-height { view.ScrollOffset = totalHeight - height + } else if view.ScrollOffset < 0 { + view.ScrollOffset = 0 } } -func (view *MessageView) IsAtTop() bool { +func (view *MessageView) Height() int { _, _, _, height := view.GetInnerRect() - totalHeight := len(view.textBuffer) - return view.ScrollOffset >= totalHeight-height+PaddingAtTop + return height +} + +func (view *MessageView) TotalHeight() int { + return len(view.textBuffer) } -func (view *MessageView) MoveDown(page bool) { +func (view *MessageView) IsAtTop() bool { _, _, _, height := view.GetInnerRect() - if page { - view.ScrollOffset -= height / 2 - } else { - view.ScrollOffset-- - } - if view.ScrollOffset < 0 { - view.ScrollOffset = 0 - } + totalHeight := len(view.textBuffer) + return view.ScrollOffset >= totalHeight-height+PaddingAtTop } func (view *MessageView) writeLine(screen tcell.Screen, line string, x, y int, color tcell.Color) { -- cgit v1.2.3