aboutsummaryrefslogtreecommitdiff
path: root/go/ui/eventHandlers.go
blob: b81606f856da83e025d6ce1db5795cf5fa1d9b9c (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
package ui

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

type EventHandlers interface {
	IsClickable() bool
	Click(int, int)
	// IsHoverable() bool
	// Hover(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) 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) IsHoverable() bool {
	_, found := m["hover"]
	return found
}

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

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