io/router: don't panic if AppendSemantics is called without a frame

Fixes: https://todo.sr.ht/~eliasnaur/gio/328
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-12-30 10:23:23 +01:00
parent 6913c856a2
commit cce0a121e1
3 changed files with 22 additions and 5 deletions
+10 -4
View File
@@ -321,12 +321,18 @@ func (c *pointerCollector) offerOp(op transfer.OfferOp, events *handlerEvents) {
h.data = op.Data
}
func (c *pointerCollector) reset(q *pointerQueue) {
q.reset()
func (c *pointerCollector) reset() {
c.q.reset()
c.resetState()
c.nodeStack = c.nodeStack[:0]
c.q = q
// Add implicit root area for semantic descriptions to hang onto.
c.ensureRoot()
}
// Ensure implicit root area for semantic descriptions to hang onto.
func (c *pointerCollector) ensureRoot() {
if len(c.q.areas) > 0 {
return
}
c.pushArea(areaRect, f32.Rect(-1e6, -1e6, 1e6, 1e6))
// Make it semantic to ensure a single semantic root.
c.q.areas[0].semantic.valid = true
+4 -1
View File
@@ -183,13 +183,16 @@ func (q *Router) SemanticAt(pos f32.Point) (SemanticID, bool) {
// AppendSemantics appends the semantic tree to nodes, and returns the result.
// The root node is the first added.
func (q *Router) AppendSemantics(nodes []SemanticNode) []SemanticNode {
q.pointer.collector.q = &q.pointer.queue
q.pointer.collector.ensureRoot()
return q.pointer.queue.AppendSemantics(nodes)
}
func (q *Router) collect() {
q.transStack = q.transStack[:0]
pc := &q.pointer.collector
pc.reset(&q.pointer.queue)
pc.q = &q.pointer.queue
pc.reset()
kc := &q.key.collector
*kc = keyCollector{q: &q.key.queue}
q.key.queue.Reset()
+8
View File
@@ -15,6 +15,14 @@ import (
"gioui.org/op/clip"
)
func TestEmptySemantics(t *testing.T) {
var r Router
tree := r.AppendSemantics(nil)
if len(tree) != 1 {
t.Errorf("expected 1 semantic node for empty tree, got %d", len(tree))
}
}
func TestSemanticTree(t *testing.T) {
var (
ops op.Ops