~pierrec/giox

ref: c164e218831e giox/eventx/eventx.go -rw-r--r-- 1.3 KiB
c164e218Pierre Curto cm/iconx: update the set of icons 4 months 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
package eventx

import (
	"gioui.org/io/event"
	"gioui.org/layout"
)

// Queue implements an op.Queue that collects all events from a composite
// widget components and make them available when collecting events for that
// composite.
//
// It can be used to catch specific keys in a widget.Editor for instance,
// such as the ESC key to unfocus the editor.
type Queue struct {
	source    event.Queue
	composite event.Tag
	tags      []queueItem
}

type queueItem struct {
	tag    event.Tag
	events []event.Event
}

// Init returns a context suitable for composite widget events collection.
func (q *Queue) Init(gtx layout.Context, composite event.Tag) layout.Context {
	q.source = gtx.Queue
	q.composite = composite
	gtx.Queue = q
	return gtx
}

// Events returns events for the tag. If the tag is the composite widget,
// then its events are completed with all of the events from its components.
func (q *Queue) Events(tag event.Tag) []event.Event {
	evs := q.source.Events(tag)
	if tag == q.composite {
		// Return all events for the composite registered tags and clear them.
		for i, item := range q.tags {
			evs = append(evs, item.events...)
			q.tags[i].events = nil
		}
		return evs
	}
	for i, item := range q.tags {
		if item.tag == tag {
			q.tags[i].events = evs
			return evs
		}
	}
	q.tags = append(q.tags, queueItem{tag: tag, events: evs})
	return evs
}