From 2f53c942ac85ed7a6262f960390bf38a97702a2b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 20 Jan 2023 16:44:07 -0500 Subject: [PATCH] The scroll wheel scrolls the scroll box --- backends/x/event.go | 45 ++++++++++++++++++++++--------- elements/basic/scrollcontainer.go | 9 ++++--- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/backends/x/event.go b/backends/x/event.go index 5b71f77..271e744 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -10,17 +10,36 @@ type scrollSum struct { x, y int } -func (sum *scrollSum) add (button xproto.Button) { - switch button { - case 4: - sum.y -- - case 5: - sum.y ++ - case 6: - sum.x -- - case 7: - sum.x ++ +const scrollDistance = 16 + +func (sum *scrollSum) add (button xproto.Button, window *Window, state uint16) { + shift := + (state & xproto.ModMaskShift) > 0 || + (state & window.backend.modifierMasks.shiftLock) > 0 + if shift { + switch button { + case 4: + sum.x -= scrollDistance + case 5: + sum.x += scrollDistance + case 6: + sum.y -= scrollDistance + case 7: + sum.y += scrollDistance + } + } else { + switch button { + case 4: + sum.y -= scrollDistance + case 5: + sum.y += scrollDistance + case 6: + sum.x -= scrollDistance + case 7: + sum.x += scrollDistance + } } + } func (window *Window) handleConfigureNotify ( @@ -52,7 +71,7 @@ func (window *Window) handleKeyPress ( connection *xgbutil.XUtil, event xevent.KeyPressEvent, ) { - if window.child == nil { return} + if window.child == nil { return } keyEvent := *event.KeyPressEvent key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State) @@ -120,7 +139,7 @@ func (window *Window) handleButtonPress ( buttonEvent := *event.ButtonPressEvent if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { sum := scrollSum { } - sum.add(buttonEvent.Detail) + sum.add(buttonEvent.Detail, window, buttonEvent.State) window.compressScrollSum(buttonEvent, &sum) child.HandleMouseScroll ( int(buttonEvent.EventX), @@ -212,7 +231,7 @@ func (window *Window) compressScrollSum ( typedEvent.Detail >= 4 && typedEvent.Detail <= 7 { - sum.add(typedEvent.Detail) + sum.add(typedEvent.Detail, window, typedEvent.State) defer func (index int) { xevent.DequeueAt(window.backend.connection, index) } (index) diff --git a/elements/basic/scrollcontainer.go b/elements/basic/scrollcontainer.go index e174a01..24793f7 100644 --- a/elements/basic/scrollcontainer.go +++ b/elements/basic/scrollcontainer.go @@ -131,7 +131,10 @@ func (element *ScrollContainer) HandleMouseScroll ( x, y int, deltaX, deltaY float64, ) { - // TODO: use this to scroll. do not pass this down to the child + scrollPoint := element.child.ScrollViewportBounds().Min.Add(image.Pt ( + int(deltaX), + int(deltaY))) + element.child.ScrollTo(scrollPoint) } func (element *ScrollContainer) Selected () (selected bool) { @@ -282,10 +285,10 @@ func (element *ScrollContainer) recalculate () { } // if the scroll bars are out of bounds, don't display them. - if !horizontal.bar.In(horizontal.gutter) { + if horizontal.bar.Dx() >= horizontal.gutter.Dx() { horizontal.bar = image.Rectangle { } } - if !vertical.bar.In(vertical.gutter) { + if vertical.bar.Dy() >= vertical.gutter.Dy() { vertical.bar = image.Rectangle { } } }