Compress mouse scroll and motion events #4

Merged
sashakoshka merged 3 commits from compress-scroll into main 2022-11-17 18:32:35 -07:00
4 changed files with 83 additions and 6 deletions

View File

@ -63,6 +63,12 @@ func (application *Application) OnMouseMove (
application.callbackManager.onMouseMove = onMouseMove
}
func (application *Application) OnScroll (
onScroll func (x, y int),
) {
application.callbackManager.onScroll = onScroll
}
func (application *Application) OnStart (
onStart func (),
) {

View File

@ -8,6 +8,23 @@ import "github.com/jezek/xgbutil/xevent"
import "git.tebibyte.media/sashakoshka/stone"
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 ++
}
}
func (backend *Backend) Run () {
backend.callbackManager.RunStart()
backend.Draw()
@ -68,7 +85,14 @@ func (backend *Backend) handleButtonPress (
event xevent.ButtonPressEvent,
) {
buttonEvent := *event.ButtonPressEvent
backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127))
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 {
sum := scrollSum { }
sum.add(buttonEvent.Detail)
backend.compressScrollSum(&sum)
backend.callbackManager.RunScroll(sum.x, sum.y)
} else {
backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127))
}
}
func (backend *Backend) handleButtonRelease (
@ -76,6 +100,7 @@ func (backend *Backend) handleButtonRelease (
event xevent.ButtonReleaseEvent,
) {
buttonEvent := *event.ButtonReleaseEvent
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return }
backend.callbackManager.RunRelease(stone.Button(buttonEvent.Detail + 127))
}
@ -101,7 +126,7 @@ func (backend *Backend) handleMotionNotify (
connection *xgbutil.XUtil,
event xevent.MotionNotifyEvent,
) {
motionEvent := *event.MotionNotifyEvent
motionEvent := backend.compressMotionNotify(*event.MotionNotifyEvent)
x, y := backend.cellAt (image.Point {
X: int(motionEvent.EventX),
Y: int(motionEvent.EventY),
@ -132,3 +157,47 @@ func (backend *Backend) compressConfigureNotify (
return
}
func (backend *Backend) compressMotionNotify (
firstEvent xproto.MotionNotifyEvent,
) (
lastEvent xproto.MotionNotifyEvent,
) {
backend.connection.Sync()
xevent.Read(backend.connection, false)
lastEvent = firstEvent
for index, untypedEvent := range xevent.Peek(backend.connection) {
if untypedEvent.Err != nil { continue }
typedEvent, ok := untypedEvent.Event.(xproto.MotionNotifyEvent)
if !ok { continue }
lastEvent = typedEvent
defer func (index int) {
xevent.DequeueAt(backend.connection, index)
} (index)
}
return
}
func (backend *Backend) compressScrollSum (sum *scrollSum) {
backend.connection.Sync()
xevent.Read(backend.connection, false)
for index, untypedEvent := range xevent.Peek(backend.connection) {
if untypedEvent.Err != nil { continue }
typedEvent, ok := untypedEvent.Event.(xproto.ButtonPressEvent)
if !ok { continue }
sum.add(typedEvent.Detail)
defer func (index int) {
xevent.DequeueAt(backend.connection, index)
} (index)
}
return
}

View File

@ -6,6 +6,7 @@ type CallbackManager struct {
onRelease func (button Button)
onResize func ()
onMouseMove func (x, y int)
onScroll func (x, y int)
onStart func ()
}
@ -34,6 +35,11 @@ func (manager *CallbackManager) RunMouseMove (x, y int) {
manager.onMouseMove(x, y)
}
func (manager *CallbackManager) RunScroll (x, y int) {
if manager.onScroll == nil { return }
manager.onScroll(x, y)
}
func (manager *CallbackManager) RunStart () {
if manager.onStart == nil { return }
manager.onStart()

View File

@ -52,10 +52,6 @@ const (
MouseButtonLeft Button = MouseButton1
MouseButtonMiddle Button = MouseButton2
MouseButtonRight Button = MouseButton3
MouseButtonScrollUp Button = MouseButton4
MouseButtonScrollDown Button = MouseButton5
MouseButtonScrollLeft Button = MouseButton6
MouseButtonScrollRight Button = MouseButton7
MouseButtonBack Button = MouseButton8
MouseButtonForward Button = MouseButton9