mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +00:00
io/router: add addPointerHandler convenience function
Remove a redundant call and shorten image.Rectangle literals while here. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+19
-80
@@ -16,13 +16,7 @@ import (
|
|||||||
func TestPointerDrag(t *testing.T) {
|
func TestPointerDrag(t *testing.T) {
|
||||||
handler := new(int)
|
handler := new(int)
|
||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
pointer.Rect(image.Rectangle{
|
addPointerHandler(&ops, handler, image.Rect(0, 0, 100, 100))
|
||||||
Max: image.Point{
|
|
||||||
X: 100,
|
|
||||||
Y: 100,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler}.Add(&ops)
|
|
||||||
|
|
||||||
var r Router
|
var r Router
|
||||||
r.Frame(&ops)
|
r.Frame(&ops)
|
||||||
@@ -56,24 +50,10 @@ func TestPointerMove(t *testing.T) {
|
|||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
|
|
||||||
// Handler 1 area: (0, 0) - (100, 100)
|
// Handler 1 area: (0, 0) - (100, 100)
|
||||||
pointer.Rect(image.Rectangle{
|
pointer.Rect(image.Rect(0, 0, 100, 100)).Add(&ops)
|
||||||
Max: image.Point{
|
|
||||||
X: 100,
|
|
||||||
Y: 100,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler1}.Add(&ops)
|
pointer.InputOp{Tag: handler1}.Add(&ops)
|
||||||
// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
|
// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
|
||||||
pointer.Rect(image.Rectangle{
|
pointer.Rect(image.Rect(50, 50, 200, 200)).Add(&ops)
|
||||||
Min: image.Point{
|
|
||||||
X: 50,
|
|
||||||
Y: 50,
|
|
||||||
},
|
|
||||||
Max: image.Point{
|
|
||||||
X: 200,
|
|
||||||
Y: 200,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler2}.Add(&ops)
|
pointer.InputOp{Tag: handler2}.Add(&ops)
|
||||||
|
|
||||||
var r Router
|
var r Router
|
||||||
@@ -137,32 +117,11 @@ func TestPointerEnterLeave(t *testing.T) {
|
|||||||
handler2 := new(int)
|
handler2 := new(int)
|
||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
|
|
||||||
var stack op.StackOp
|
|
||||||
stack.Push(&ops)
|
|
||||||
// Handler 1 area: (0, 0) - (100, 100)
|
// Handler 1 area: (0, 0) - (100, 100)
|
||||||
pointer.Rect(image.Rectangle{
|
addPointerHandler(&ops, handler1, image.Rect(0, 0, 100, 100))
|
||||||
Max: image.Point{
|
|
||||||
X: 100,
|
|
||||||
Y: 100,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler1}.Add(&ops)
|
|
||||||
stack.Pop()
|
|
||||||
|
|
||||||
// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
|
// Handler 2 area: (50, 50) - (100, 100) (areas overlap).
|
||||||
stack.Push(&ops)
|
addPointerHandler(&ops, handler2, image.Rect(50, 50, 200, 200))
|
||||||
pointer.Rect(image.Rectangle{
|
|
||||||
Min: image.Point{
|
|
||||||
X: 50,
|
|
||||||
Y: 50,
|
|
||||||
},
|
|
||||||
Max: image.Point{
|
|
||||||
X: 200,
|
|
||||||
Y: 200,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler2}.Add(&ops)
|
|
||||||
stack.Pop()
|
|
||||||
|
|
||||||
var r Router
|
var r Router
|
||||||
r.Frame(&ops)
|
r.Frame(&ops)
|
||||||
@@ -258,25 +217,11 @@ func TestPointerEnterLeaveNested(t *testing.T) {
|
|||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
|
|
||||||
// Handler 1 area: (0, 0) - (100, 100)
|
// Handler 1 area: (0, 0) - (100, 100)
|
||||||
pointer.Rect(image.Rectangle{
|
pointer.Rect(image.Rect(0, 0, 100, 100)).Add(&ops)
|
||||||
Max: image.Point{
|
|
||||||
X: 100,
|
|
||||||
Y: 100,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler1}.Add(&ops)
|
pointer.InputOp{Tag: handler1}.Add(&ops)
|
||||||
|
|
||||||
// Handler 2 area: (25, 25) - (75, 75) (nested within first).
|
// Handler 2 area: (25, 25) - (75, 75) (nested within first).
|
||||||
pointer.Rect(image.Rectangle{
|
pointer.Rect(image.Rect(25, 25, 75, 75)).Add(&ops)
|
||||||
Min: image.Point{
|
|
||||||
X: 25,
|
|
||||||
Y: 25,
|
|
||||||
},
|
|
||||||
Max: image.Point{
|
|
||||||
X: 75,
|
|
||||||
Y: 75,
|
|
||||||
},
|
|
||||||
}).Add(&ops)
|
|
||||||
pointer.InputOp{Tag: handler2}.Add(&ops)
|
pointer.InputOp{Tag: handler2}.Add(&ops)
|
||||||
|
|
||||||
var r Router
|
var r Router
|
||||||
@@ -366,28 +311,12 @@ func TestPointerEnterLeaveNested(t *testing.T) {
|
|||||||
func TestPointerActiveInputDisappears(t *testing.T) {
|
func TestPointerActiveInputDisappears(t *testing.T) {
|
||||||
handler1 := new(int)
|
handler1 := new(int)
|
||||||
// Save this logic so we can redo it later.
|
// Save this logic so we can redo it later.
|
||||||
renderHandler1 := func(ops *op.Ops) {
|
|
||||||
var stack op.StackOp
|
|
||||||
stack.Push(ops)
|
|
||||||
// Handler 1 area: (0, 0) - (100, 100)
|
|
||||||
pointer.Rect(image.Rectangle{
|
|
||||||
Max: image.Point{
|
|
||||||
X: 100,
|
|
||||||
Y: 100,
|
|
||||||
},
|
|
||||||
}).Add(ops)
|
|
||||||
pointer.InputOp{Tag: handler1}.Add(ops)
|
|
||||||
stack.Pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
var r Router
|
var r Router
|
||||||
|
|
||||||
renderHandler1(&ops)
|
|
||||||
|
|
||||||
// Draw handler.
|
// Draw handler.
|
||||||
ops.Reset()
|
ops.Reset()
|
||||||
renderHandler1(&ops)
|
addPointerHandler(&ops, handler1, image.Rect(0, 0, 100, 100))
|
||||||
r.Frame(&ops)
|
r.Frame(&ops)
|
||||||
r.Add(
|
r.Add(
|
||||||
pointer.Event{
|
pointer.Event{
|
||||||
@@ -415,6 +344,16 @@ func TestPointerActiveInputDisappears(t *testing.T) {
|
|||||||
assertEventSequence(t, r.Events(handler1), pointer.Cancel)
|
assertEventSequence(t, r.Events(handler1), pointer.Cancel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addPointerHandler adds a pointer.InputOp for the tag in a
|
||||||
|
// rectangular area.
|
||||||
|
func addPointerHandler(ops *op.Ops, tag event.Tag, area image.Rectangle) {
|
||||||
|
var stack op.StackOp
|
||||||
|
stack.Push(ops)
|
||||||
|
defer stack.Pop()
|
||||||
|
pointer.Rect(area).Add(ops)
|
||||||
|
pointer.InputOp{Tag: tag}.Add(ops)
|
||||||
|
}
|
||||||
|
|
||||||
// toTypes converts a sequence of event.Event to their pointer.Types. It assumes
|
// toTypes converts a sequence of event.Event to their pointer.Types. It assumes
|
||||||
// that all input events are of underlying type pointer.Event, and thus will
|
// that all input events are of underlying type pointer.Event, and thus will
|
||||||
// panic if some are not.
|
// panic if some are not.
|
||||||
|
|||||||
Reference in New Issue
Block a user