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>
This commit is contained in:
Elias Naur
2023-10-16 17:57:27 -05:00
parent d2085ab7c5
commit ef8171b971
19 changed files with 521 additions and 504 deletions
+20
View File
@@ -3,6 +3,11 @@
// Package event contains types for event handling.
package event
import (
"gioui.org/internal/ops"
"gioui.org/op"
)
// Tag is the stable identifier for an event handler.
// For a handler h, the tag is typically &h.
type Tag interface{}
@@ -11,3 +16,18 @@ type Tag interface{}
type Event interface {
ImplementsEvent()
}
// Filter represents a filter for [Event] types.
type Filter interface {
ImplementsFilter()
}
// InputOp declares a tag for input routing at the current transformation
// and clip area hierarchy. It panics if tag is nil.
func InputOp(o *op.Ops, tag Tag) {
if tag == nil {
panic("Tag must be non-nil")
}
data := ops.Write1(&o.Internal, ops.TypeInputLen, tag)
data[0] = byte(ops.TypeInput)
}