-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunloop.go
75 lines (68 loc) · 1.47 KB
/
runloop.go
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
package touch
import "context"
var (
MainRunLoop RunLoop
)
type RunLoop struct {
Window *Window
Tasks chan<- func()
tasks chan func()
events chan TouchEvent
}
func (runloop *RunLoop) Init(window *Window) {
if runloop != &MainRunLoop {
panic("Only the main RunLoop may be Initialized")
}
runloop.tasks = make(chan func(), 100)
runloop.Tasks = runloop.tasks
runloop.Window = window
runloop.platformInit()
}
func (runloop *RunLoop) runInner(ctx context.Context) {
var eventTarget LayerTouchDelegate
var touchCanceled bool
cancelTouch := func() {
touchCanceled = true
if eventTarget != nil {
eventTarget.CancelTouch()
eventTarget = nil
}
}
win := runloop.Window
runloop.updateDisplay()
outer:
for {
select {
case event := <-runloop.events:
win.Calibrate(&event)
event.Cancel = cancelTouch
if touchCanceled {
// Ignore events until mouseup
if !event.Pressed {
touchCanceled = false
}
} else if event.Pressed {
if eventTarget != nil {
eventTarget.UpdateTouch(event)
} else {
// Only when there is no current event target, hit test for one.
if eventTarget = win.HitTest(event); eventTarget != nil {
eventTarget.StartTouch(event)
}
}
} else {
if eventTarget != nil {
eventTarget.EndTouch(event)
eventTarget = nil
}
}
case task := <-runloop.tasks:
task()
case <-win.redrawCh:
runloop.updateDisplay()
case <-ctx.Done():
runloop.cleanup()
break outer
}
}
}