aboutsummaryrefslogtreecommitdiff
path: root/go/ui/eventHandlers.go
blob: d21b6e40babb748726e16f02d31ec8b408ac13a5 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package ui

type (
	ClickHandler  = func(int, int)
	HoverHandler  = func(bool, int, int)
	ScrollHandler = func(int, int)
	UpdateHandler = func() error
	FocusHandler  = func(bool)
)

type EventHandlers interface {
	IsClickable() bool
	Click(int, int)
	IsHoverable() bool
	Hover(bool, int, int)
	IsScrollable() bool
	Scroll(int, int)
	IsUpdatable() bool
	Update() error
	IsFocusable() bool
	Focus(bool)
}

// TODO: cache events (in a bitmask)
type EventHandlersMap map[string]any

func NewEventHandlersMap() EventHandlersMap {
	return make(EventHandlersMap)
}

func (m EventHandlersMap) RegisterHandler(ev string, handler any) {
	// TODO: sanitize input
	m[ev] = handler
}

func (m EventHandlersMap) IsClickable() bool {
	_, found := m["click"]
	return found
}

func (m EventHandlersMap) Click(x, y int) {
	onclick := m["click"]
	onclick.(ClickHandler)(x, y)
}

func (m EventHandlersMap) IsHoverable() bool {
	_, found := m["hover"]
	return found
}

func (m EventHandlersMap) Hover(start bool, x, y int) {
	onhover := m["hover"]
	onhover.(HoverHandler)(start, x, y)
}

func (m EventHandlersMap) IsScrollable() bool {
	_, found := m["update"]
	return found
}

func (m EventHandlersMap) Scroll(x, y int) {
	onscroll := m["scroll"]
	onscroll.(ScrollHandler)(x, y)
}

func (m EventHandlersMap) IsUpdatable() bool {
	_, found := m["update"]
	return found
}

func (m EventHandlersMap) Update() error {
	onupdate := m["update"]
	return onupdate.(UpdateHandler)()
}

func (m EventHandlersMap) IsFocusable() bool {
	_, found := m["focus"]
	return found
}

func (m EventHandlersMap) Focus(focus bool) {
	onfocus := m["focus"]
	onfocus.(FocusHandler)(focus)
}