aboutsummaryrefslogtreecommitdiff
path: root/go/ui/hoverDetector.go
blob: 49161f4f06e532a0918f3ea0094f4fdf49f5e363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 {
	last_x, last_y int
	ticks          int
	hovering       Hoverable
}

func (h *hoverDetector) update(collection *Collection, x, y int, w Widget) {
	if x == h.last_x && y == h.last_y {
		h.hover(collection, x, y, w)
	} else {
		h.reset(x, y)
		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
}