diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-03-25 12:35:50 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-03-25 12:35:50 +0300 |
commit | c1286661090731729e76e48d5e47988ad0175a20 (patch) | |
tree | e43ce30f3d3020ec1e2bec13ea7fc954b89e78d4 | |
parent | e414e20215db3a80bb1cf511390406324e742ebf (diff) |
Refactor things and add proper wheel scroll support
-rw-r--r-- | matrix/matrix.go | 8 | ||||
-rw-r--r-- | ui/view-main.go | 51 | ||||
-rw-r--r-- | ui/widget/message-view.go | 39 |
3 files changed, 59 insertions, 39 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go index 8199449..7ba6ebb 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -260,7 +260,8 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) { if evt.Unsigned.PrevContent != nil { prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string) } - if membership == prevMembership { + const Hour = 1 * 60 * 60 * 1000 + if membership == prevMembership || evt.Unsigned.Age > Hour { return } switch membership { @@ -273,11 +274,6 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) { // HandleMembership is the event handler for the m.room.membership state event. func (c *Container) HandleMembership(evt *gomatrix.Event) { - const Hour = 1 * 60 * 60 * 1000 - if evt.Unsigned.Age > Hour { - return - } - if evt.StateKey != nil && *evt.StateKey == c.config.Session.UserID { c.processOwnMembershipChange(evt) } diff --git a/ui/view-main.go b/ui/view-main.go index e5c6792..a5580b3 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -195,16 +195,26 @@ func (view *MainView) KeyEventHandler(roomView *widget.RoomView, key *tcell.Even } else { return key } - } else if k == tcell.KeyPgUp || k == tcell.KeyPgDn || k == tcell.KeyUp || k == tcell.KeyDown { + } else if k == tcell.KeyPgUp || k == tcell.KeyPgDn || k == tcell.KeyUp || k == tcell.KeyDown || k == tcell.KeyEnd || k == tcell.KeyHome { msgView := roomView.MessageView() - if k == tcell.KeyPgUp || k == tcell.KeyUp { - if msgView.IsAtTop() { - go view.LoadHistory(roomView.Room.ID, false) - } else { - msgView.MoveUp(k == tcell.KeyPgUp) - } - } else { - msgView.MoveDown(k == tcell.KeyPgDn) + + if msgView.IsAtTop() && (k == tcell.KeyPgUp || k == tcell.KeyUp) { + go view.LoadHistory(roomView.Room.ID, false) + } + + switch k { + case tcell.KeyPgUp: + msgView.AddScrollOffset(msgView.Height() / 2) + case tcell.KeyPgDn: + msgView.AddScrollOffset(-msgView.Height() / 2) + case tcell.KeyUp: + msgView.AddScrollOffset(1) + case tcell.KeyDown: + msgView.AddScrollOffset(-1) + case tcell.KeyHome: + msgView.AddScrollOffset(msgView.TotalHeight()) + case tcell.KeyEnd: + msgView.AddScrollOffset(-msgView.TotalHeight()) } } else { return key @@ -212,9 +222,28 @@ func (view *MainView) KeyEventHandler(roomView *widget.RoomView, key *tcell.Even return nil } +const WheelScrollOffsetDiff = 3 + func (view *MainView) MouseEventHandler(roomView *widget.RoomView, event *tcell.EventMouse) *tcell.EventMouse { - if event.Buttons() != tcell.ButtonNone { - view.BumpFocus() + if event.Buttons() == tcell.ButtonNone { + return event + } + view.BumpFocus() + + msgView := roomView.MessageView() + x, y := event.Position() + + switch event.Buttons() { + case tcell.WheelUp: + if msgView.IsAtTop() { + go view.LoadHistory(roomView.Room.ID, false) + } else { + msgView.AddScrollOffset(WheelScrollOffsetDiff) + } + case tcell.WheelDown: + msgView.AddScrollOffset(-WheelScrollOffsetDiff) + default: + debug.Print("Mouse event received:", event.Buttons(), event.Modifiers(), x, y) } return event 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) { |