Files
gio-patched/widget/dnd_test.go
T
Elias Naur ef8171b971 io: [API] introduce event filters; convert pointer input to use them
Instead of having to supply the predicates for event filtering at the
time of layout, the new Filter type allows widgets to filter at the time
of calling Source.Events. There is then only the need for a single input
op type, in package event.

Filters most importantly allow the use of one tag for several event types,
and we can define that a widget w has &w as its primary tag, by convention.
This allows the replacement of per-widget Focus methods with direct uses
of FocusCmd{&w}, and the later addition of Source.Focused(&w) queries.

Note that the TestCursor test needed restructuring to avoid its use of
InputOps.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2024-02-05 10:59:51 +00:00

89 lines
1.8 KiB
Go

package widget
import (
"image"
"testing"
"gioui.org/f32"
"gioui.org/io/event"
"gioui.org/io/input"
"gioui.org/io/pointer"
"gioui.org/io/transfer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
)
func TestDraggable(t *testing.T) {
var r input.Router
gtx := layout.Context{
Constraints: layout.Exact(image.Pt(100, 100)),
Source: r.Source(),
Ops: new(op.Ops),
}
drag := &Draggable{
Type: "file",
}
defer pointer.PassOp{}.Push(gtx.Ops).Pop()
dims := drag.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: gtx.Constraints.Min}
}, nil)
stack := clip.Rect{Max: dims.Size}.Push(gtx.Ops)
event.InputOp(gtx.Ops, drag)
stack.Pop()
drag.Update(gtx)
r.Events(drag, transfer.TargetFilter{Type: drag.Type})
r.Frame(gtx.Ops)
r.Queue(
pointer.Event{
Position: f32.Pt(10, 10),
Kind: pointer.Press,
},
pointer.Event{
Position: f32.Pt(20, 10),
Kind: pointer.Move,
},
pointer.Event{
Position: f32.Pt(20, 10),
Kind: pointer.Release,
},
)
ofr := &offer{data: "hello"}
drag.Offer(gtx, "file", ofr)
drag.Update(gtx)
r.Events(drag, transfer.TargetFilter{Type: drag.Type})
r.Frame(gtx.Ops)
evs := r.Events(drag, transfer.TargetFilter{Type: drag.Type})
if len(evs) != 2 {
t.Fatalf("expected 2 event, got %d", len(evs))
}
ev := evs[0].(transfer.DataEvent)
ev.Open = nil
if got, want := ev.Type, "file"; got != want {
t.Errorf("expected %v; got %v", got, want)
}
if ofr.closed {
t.Error("offer closed prematurely")
}
r.Frame(gtx.Ops)
if !ofr.closed {
t.Error("offer was not closed")
}
}
// offer satisfies io.ReadCloser for use in data transfers.
type offer struct {
data string
closed bool
}
func (*offer) Read([]byte) (int, error) { return 0, nil }
func (o *offer) Close() error {
o.closed = true
return nil
}