The scroll wheel scrolls the scroll box

This commit is contained in:
Sasha Koshka 2023-01-20 16:44:07 -05:00
parent b85285f326
commit 2f53c942ac
2 changed files with 38 additions and 16 deletions

View File

@ -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)

View File

@ -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 { }
}
}