aboutsummaryrefslogtreecommitdiff
path: root/go/ui/hoverDetector.go
blob: 8404e736ac20d626f82a5c0670b7c0799e6e88f3 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
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
	threshold      int
}

var (
	hoverDetector        = _hoverDetector{threshold: 60}
	longtapHoverDetector = _longtapHoverDetector{}
)

func (h *_hoverDetector) update(x, y int) (ev InputEvent, ch bool) {
	if x == h.last_x && y == h.last_y {
		h.ticks = h.ticks + 1
		if h.ticks == h.threshold {
			return InputEvent{HoverStart, x, y, nil}, true
		}
	} else {
		if h.ticks > h.threshold {
			ev = InputEvent{HoverEnd, h.last_x, h.last_y, nil}
			ch = true
		}
		h.ticks = 0
		h.last_x, h.last_y = x, y
	}

	return
}

type _longtapHoverDetector struct {
	active *longtap
}

func (h *_longtapHoverDetector) update(longtap *longtap) (ev InputEvent, ch bool) {
	if longtap != nil && h.active == nil {
		h.active = longtap
		return InputEvent{HoverStart, longtap.x, longtap.y, nil}, true
	}
	return InputEvent{}, false
}

func (h *_longtapHoverDetector) reset() (ev InputEvent, ch bool) {
	if h.active != nil {
		ev := InputEvent{HoverEnd, h.active.x, h.active.y, nil}
		h.active = nil
		return ev, true
	}
	return InputEvent{}, false
}