From 778487d25a79721539cea1e47331e3c2f985c8cc Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 6 Aug 2025 18:02:44 +0200 Subject: rework hover detection --- go/ui/hoverDetector.go | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) (limited to 'go/ui/hoverDetector.go') diff --git a/go/ui/hoverDetector.go b/go/ui/hoverDetector.go index 49161f4f..385b2ffd 100644 --- a/go/ui/hoverDetector.go +++ b/go/ui/hoverDetector.go @@ -3,39 +3,32 @@ package ui // A simple structure to detect if the cursor has not moved or started moving again. // hoverDetector.update must be called during each Update invocation to track the // frame and the current cursor position. -type hoverDetector struct { +type _hoverDetector struct { last_x, last_y int ticks int - hovering Hoverable } -func (h *hoverDetector) update(collection *Collection, x, y int, w Widget) { +const HOVER_THRESHOLD = 60 + +var ( + hoverDetector _hoverDetector + longtapHoverDetector _hoverDetector +) + +func (h *_hoverDetector) update(x, y int) (ev InputEvent, ch bool) { if x == h.last_x && y == h.last_y { - h.hover(collection, x, y, w) + h.ticks = h.ticks + 1 + if h.ticks == HOVER_THRESHOLD { + return InputEvent{HoverStart, x, y, nil}, true + } } else { - h.reset(x, y) + if h.ticks > HOVER_THRESHOLD { + ev = InputEvent{HoverEnd, h.last_x, h.last_y, nil} + ch = true + } + h.ticks = 0 h.last_x, h.last_y = x, y } -} - -func (h *hoverDetector) hover(collection *Collection, x, y int, w Widget) { - h.ticks++ - if w == nil || h.hovering != nil { - return - } - - if hoverable, ok := w.(Hoverable); ok && hoverable.onHover(h.ticks, x, y, collection) { - h.hovering = hoverable - } -} - -func (h *hoverDetector) reset(x, y int) { - if h.hovering != nil { - if !h.hovering.stopHover(h.ticks, x, y) { - return - } - h.hovering = nil - } - h.ticks = 0 + return } -- cgit v1.2.3