From ea95473090323df013391d1df8a06bb637ea8725 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 5 Aug 2023 17:56:15 -0400 Subject: [PATCH] Add mouse motion event --- box.go | 12 ++++++++++++ event.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ system.go | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/box.go b/box.go index a32988a..4d7297a 100644 --- a/box.go +++ b/box.go @@ -313,6 +313,18 @@ func (this *box) handleFocusLeave () { this.on.focusLeave.Broadcast() } +func (this *box) handleMouseEnter () { + this.on.mouseEnter.Broadcast() +} + +func (this *box) handleMouseLeave () { + this.on.mouseLeave.Broadcast() +} + +func (this *box) handleMouseMove () { + this.on.mouseMove.Broadcast() +} + func (this *box) handleMouseDown (button input.Button) { if this.focusable { this.SetFocused(true) diff --git a/event.go b/event.go index 62baac4..3476f41 100644 --- a/event.go +++ b/event.go @@ -261,6 +261,7 @@ func (window *window) handleButtonPress ( if !insideWindow && window.shy && !scrolling { window.Close() } else if scrolling { + // TODO // underneath := window.scrollTargetChildAt(point) // if underneath != nil { // if child, ok := underneath.element.(ability.ScrollTarget); ok { @@ -298,6 +299,27 @@ func (window *window) handleButtonRelease ( } } +func (window *window) handleMotionNotify ( + connection *xgbutil.XUtil, + event xevent.MotionNotifyEvent, +) { + if window.hasModal { return } + + motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent) + x := int(motionEvent.EventX) + y := int(motionEvent.EventY) + + handled := false + for _, child := range window.drags { + if child == nil { continue } + child.handleMouseMove() + handled = true + } + + if !handled { + window.boxUnder(image.Pt(x, y)).handleMouseMove() + } +} func (window *window) compressExpose ( firstEvent xproto.ExposeEvent, @@ -364,6 +386,32 @@ func (window *window) compressConfigureNotify ( return } +func (window *window) compressMotionNotify ( + firstEvent xproto.MotionNotifyEvent, +) ( + lastEvent xproto.MotionNotifyEvent, +) { + window.backend.x.Sync() + xevent.Read(window.backend.x, false) + lastEvent = firstEvent + + for index, untypedEvent := range xevent.Peek(window.backend.x) { + if untypedEvent.Err != nil { continue } + + typedEvent, ok := untypedEvent.Event.(xproto.MotionNotifyEvent) + if !ok { continue } + + if firstEvent.Event == typedEvent.Event { + lastEvent = typedEvent + defer func (index int) { + xevent.DequeueAt(window.backend.x, index) + } (index) + } + } + + return +} + func (window *window) updateModifiers (state uint16) { modifiers := xgbkb.StateToModifiers(state) window.modifiers.Shift = modifiers.Shift || modifiers.ShiftLock diff --git a/system.go b/system.go index 41f33b6..aec0613 100644 --- a/system.go +++ b/system.go @@ -58,7 +58,7 @@ type anyBox interface { // handleDndDrop (data.Data) // handleMouseEnter () // handleMouseLeave () - // handleMouseMove () + handleMouseMove () handleMouseDown (input.Button) handleMouseUp (input.Button) // handleScroll (float64, float64)