~eliasnaur/gio

ref: 7aa7bb3be40858efe2a98f866fcc7db2026b02fd gio/ui/input/queue.go -rw-r--r-- 1.0 KiB
7aa7bb3bElias Naur ui: rename ops to have Op suffixed, not prefixed 2 years ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package input

import (
	"gioui.org/ui"
	"gioui.org/ui/key"
	"gioui.org/ui/pointer"
)

// Queue is an Events implementation that merges events from
// all available input sources.
type Queue struct {
	pqueue pointerQueue
	kqueue keyQueue

	handlers handlerEvents
}

type handlerEvents map[Key][]Event

func (q *Queue) Next(k Key) (Event, bool) {
	events := q.handlers[k]
	if len(events) == 0 {
		return nil, false
	}
	e := events[0]
	q.handlers[k] = events[1:]
	return e, true
}

func (q *Queue) Frame(ops *ui.Ops) {
	q.init()
	for k := range q.handlers {
		delete(q.handlers, k)
	}
	q.pqueue.Frame(ops, q.handlers)
	q.kqueue.Frame(ops, q.handlers)
}

func (q *Queue) Add(e Event) {
	q.init()
	switch e := e.(type) {
	case pointer.Event:
		q.pqueue.Push(e, q.handlers)
	case key.Edit, key.Chord, key.Focus:
		q.kqueue.Push(e, q.handlers)
	}
}

func (q *Queue) InputState() key.TextInputState {
	return q.kqueue.InputState()
}

func (q *Queue) init() {
	if q.handlers == nil {
		q.handlers = make(handlerEvents)
	}
}