Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
io/router: add addPointerHandler convenience function
Browse files Browse the repository at this point in the history
Remove a redundant call and shorten image.Rectangle literals while here.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
  • Loading branch information
eliasnaur committed May 31, 2020
1 parent 2487842 commit 23c2d44
Showing 1 changed file with 19 additions and 80 deletions.
99 changes: 19 additions & 80 deletions io/router/pointer_test.go
Expand Up @@ -16,13 +16,7 @@ import (
func TestPointerDrag(t *testing.T) {
handler := new(int)
var ops op.Ops
pointer.Rect(image.Rectangle{
Max: image.Point{
X: 100,
Y: 100,
},
}).Add(&ops)
pointer.InputOp{Tag: handler}.Add(&ops)
addPointerHandler(&ops, handler, image.Rect(0, 0, 100, 100))

var r Router
r.Frame(&ops)
Expand Down Expand Up @@ -56,24 +50,10 @@ func TestPointerMove(t *testing.T) {
var ops op.Ops

// Handler 1 area: (0, 0) - (100, 100)
pointer.Rect(image.Rectangle{
Max: image.Point{
X: 100,
Y: 100,
},
}).Add(&ops)
pointer.Rect(image.Rect(0, 0, 100, 100)).Add(&ops)
pointer.InputOp{Tag: handler1}.Add(&ops)
// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
pointer.Rect(image.Rectangle{
Min: image.Point{
X: 50,
Y: 50,
},
Max: image.Point{
X: 200,
Y: 200,
},
}).Add(&ops)
pointer.Rect(image.Rect(50, 50, 200, 200)).Add(&ops)
pointer.InputOp{Tag: handler2}.Add(&ops)

var r Router
Expand Down Expand Up @@ -137,32 +117,11 @@ func TestPointerEnterLeave(t *testing.T) {
handler2 := new(int)
var 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()
addPointerHandler(&ops, handler1, image.Rect(0, 0, 100, 100))

// Handler 2 area: (50, 50) - (100, 100) (areas intersect).
stack.Push(&ops)
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()
// Handler 2 area: (50, 50) - (100, 100) (areas overlap).
addPointerHandler(&ops, handler2, image.Rect(50, 50, 200, 200))

var r Router
r.Frame(&ops)
Expand Down Expand Up @@ -258,25 +217,11 @@ func TestPointerEnterLeaveNested(t *testing.T) {
var ops op.Ops

// Handler 1 area: (0, 0) - (100, 100)
pointer.Rect(image.Rectangle{
Max: image.Point{
X: 100,
Y: 100,
},
}).Add(&ops)
pointer.Rect(image.Rect(0, 0, 100, 100)).Add(&ops)
pointer.InputOp{Tag: handler1}.Add(&ops)

// Handler 2 area: (25, 25) - (75, 75) (nested within first).
pointer.Rect(image.Rectangle{
Min: image.Point{
X: 25,
Y: 25,
},
Max: image.Point{
X: 75,
Y: 75,
},
}).Add(&ops)
pointer.Rect(image.Rect(25, 25, 75, 75)).Add(&ops)
pointer.InputOp{Tag: handler2}.Add(&ops)

var r Router
Expand Down Expand Up @@ -366,28 +311,12 @@ func TestPointerEnterLeaveNested(t *testing.T) {
func TestPointerActiveInputDisappears(t *testing.T) {
handler1 := new(int)
// 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 r Router

renderHandler1(&ops)

// Draw handler.
ops.Reset()
renderHandler1(&ops)
addPointerHandler(&ops, handler1, image.Rect(0, 0, 100, 100))
r.Frame(&ops)
r.Add(
pointer.Event{
Expand Down Expand Up @@ -415,6 +344,16 @@ func TestPointerActiveInputDisappears(t *testing.T) {
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
// that all input events are of underlying type pointer.Event, and thus will
// panic if some are not.
Expand Down

0 comments on commit 23c2d44

Please sign in to comment.