all: [API] replace tag parameter of Source.Event with per-filter tags

Until now, every event has had a particular target. We're about to simplify
key event delivery to match the first matching filter, so there is no
longer a global meaning to the tag argument to Source.Event.

Add fields to filters to specify their target tags.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-11-26 12:58:10 -06:00
parent 496fc3cc82
commit d9a007586c
16 changed files with 195 additions and 115 deletions
+3 -1
View File
@@ -294,6 +294,7 @@ func TestFocusScroll(t *testing.T) {
filters := []event.Filter{
key.FocusFilter{},
pointer.Filter{
Target: h,
Kinds: pointer.Scroll,
ScrollBounds: image.Rect(-100, -100, 100, 100),
},
@@ -322,7 +323,8 @@ func TestFocusClick(t *testing.T) {
filters := []event.Filter{
key.FocusFilter{},
pointer.Filter{
Kinds: pointer.Press | pointer.Release,
Target: h,
Kinds: pointer.Press | pointer.Release,
},
}
assertEventPointerTypeSequence(t, events(r, h, filters...), pointer.Cancel)
+32 -1
View File
@@ -19,6 +19,17 @@ import (
"gioui.org/op/clip"
)
func TestPointerNilTarget(t *testing.T) {
r := new(Router)
r.Event(pointer.Filter{Kinds: pointer.Press})
r.Frame(new(op.Ops))
r.Queue(pointer.Event{Kind: pointer.Press})
// Nil Targets should not receive events.
if _, ok := r.Event(pointer.Filter{Kinds: pointer.Press}); ok {
t.Errorf("nil target received event")
}
}
func TestPointerWakeup(t *testing.T) {
handler := new(int)
var ops op.Ops
@@ -55,9 +66,29 @@ func TestPointerDrag(t *testing.T) {
}
func events(r *Router, h event.Tag, filters ...event.Filter) []event.Event {
// Hack to facilitate transition to per-filter tags.
for i, f := range filters {
switch f := f.(type) {
case key.Filter:
f.Target = h
filters[i] = f
case key.FocusFilter:
f.Target = h
filters[i] = f
case transfer.SourceFilter:
f.Target = h
filters[i] = f
case transfer.TargetFilter:
f.Target = h
filters[i] = f
case pointer.Filter:
f.Target = h
filters[i] = f
}
}
var events []event.Event
for {
e, ok := r.Event(h, filters...)
e, ok := r.Event(filters...)
if !ok {
break
}
+77 -32
View File
@@ -35,30 +35,23 @@ type Router struct {
queue keyQueue
}
cqueue clipboardQueue
// states is the list of pending state changes resulting from
// incoming events. The first element, if present, contains the state
// and events for the current frame.
changes []stateChange
reader ops.Reader
reader ops.Reader
// InvalidateCmd summary.
wakeup bool
wakeupTime time.Time
// Changes queued for next call to Frame.
commands []Command
// transfers is the pending transfer.DataEvent.Open functions.
transfers []io.ReadCloser
// deferring is set if command execution and event delivery is deferred
// to the next frame.
deferring bool
// scratchFilter is for garbage-free construction of ephemeral filters.
scratchFilter filter
// scratchFilters is for garbage-free construction of ephemeral filters.
scratchFilters []taggedFilter
}
// Source implements the interface between a Router and user interface widgets.
@@ -131,6 +124,12 @@ type filter struct {
key keyFilter
}
// taggedFilter is a filter for a particular tag.
type taggedFilter struct {
tag event.Tag
filter filter
}
// stateChange represents the new state and outgoing events
// resulting from an incoming event.
type stateChange struct {
@@ -173,51 +172,94 @@ func (s Source) Enabled() bool {
return s.r != nil
}
// Event returns the next event for the handler tag that matches one
// or more of filters.
func (s Source) Event(k event.Tag, filters ...event.Filter) (event.Event, bool) {
// Event returns the next event that matches at least one of filters.
func (s Source) Event(filters ...event.Filter) (event.Event, bool) {
if !s.Enabled() {
return nil, false
}
return s.r.Event(k, filters...)
return s.r.Event(filters...)
}
func (q *Router) Event(k event.Tag, filters ...event.Filter) (event.Event, bool) {
h := q.stateFor(k)
q.scratchFilter.Reset()
// Record handler filters and add reset events.
func (q *Router) Event(filters ...event.Filter) (event.Event, bool) {
// Merge filters into scratch filters.
q.scratchFilters = q.scratchFilters[:0]
for _, f := range filters {
q.scratchFilter.Add(f)
switch f.(type) {
var t event.Tag
switch f := f.(type) {
case key.Filter:
t = f.Target
case transfer.SourceFilter:
t = f.Target
case transfer.TargetFilter:
t = f.Target
case key.FocusFilter:
t = f.Target
case pointer.Filter:
t = f.Target
}
if t == nil {
continue
}
var filter *filter
for i := range q.scratchFilters {
s := &q.scratchFilters[i]
if s.tag == t {
filter = &s.filter
break
}
}
if filter == nil {
n := len(q.scratchFilters)
q.scratchFilters = append(q.scratchFilters, taggedFilter{tag: t})
filter = &q.scratchFilters[n].filter
}
filter.Add(f)
}
for _, tf := range q.scratchFilters {
h := q.stateFor(tf.tag)
h.filter.Merge(tf.filter)
h.nextFilter.Merge(tf.filter)
}
// Deliver reset event, if any.
for _, f := range filters {
switch f := f.(type) {
case key.FocusFilter:
if f.Target == nil {
break
}
h := q.stateFor(f.Target)
if reset, ok := h.key.ResetEvent(); ok {
return reset, true
}
case pointer.Filter:
if f.Target == nil {
break
}
h := q.stateFor(f.Target)
if reset, ok := h.pointer.ResetEvent(); ok {
return reset, true
}
}
}
h.nextFilter.Merge(q.scratchFilter)
if !q.deferring {
for i := range q.changes {
change := &q.changes[i]
j := 0
for j < len(change.events) {
evt := change.events[j]
if evt.tag != k || !q.scratchFilter.Matches(evt.event) {
j++
continue
for j, evt := range change.events {
for _, tf := range q.scratchFilters {
if evt.tag == tf.tag && tf.filter.Matches(evt.event) {
change.events = append(change.events[:j], change.events[j+1:]...)
// Fast forward state to last matched.
q.collapseState(i)
return evt.event, true
}
}
change.events = append(change.events[:j], change.events[j+1:]...)
// Fast forward state to last matched.
q.collapseState(i)
return evt.event, true
}
}
}
h.processedFilter.Merge(q.scratchFilter)
for _, tf := range q.scratchFilters {
h := q.stateFor(tf.tag)
h.processedFilter.Merge(tf.filter)
}
return nil, false
}
@@ -691,6 +733,9 @@ func (q *Router) EditorState() EditorState {
}
func (q *Router) stateFor(tag event.Tag) *handler {
if tag == nil {
panic("internal error: nil tag")
}
s, ok := q.handlers[tag]
if !ok {
s = new(handler)
+1 -1
View File
@@ -16,7 +16,7 @@ func TestNoFilterAllocs(t *testing.T) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Event(nil, pointer.Filter{})
s.Event(pointer.Filter{})
}
})
if allocs := b.AllocsPerOp(); allocs != 0 {
+8 -3
View File
@@ -18,8 +18,9 @@ import (
"gioui.org/op"
)
// Filter matches [Event]s.
// Filter matches any [Event] that matches the parameters.
type Filter struct {
Target event.Tag
// Required is the set of modifiers that must be included in events matched.
Required Modifiers
// Optional is the set of modifiers that may be included in events matched.
@@ -108,8 +109,12 @@ type EditEvent struct {
Text string
}
// FocusFilter matches [FocusEvent]s.
type FocusFilter struct{}
// FocusFilter matches any [FocusEvent], [EditEvent], [SnippetEvent],
// or [SelectionEvent] with the specified target.
type FocusFilter struct {
// Target is a tag specified in a previous event.Op.
Target event.Tag
}
// InputHint changes the on-screen-keyboard type. That hints the
// type of data that might be entered by the user.
+7 -24
View File
@@ -5,36 +5,19 @@ Package pointer implements pointer events and operations.
A pointer is either a mouse controlled cursor or a touch
object such as a finger.
The InputOp operation is used to declare a handler ready for pointer
events. Use an event.Queue to receive events.
# Kinds
Only events that match a specified list of types are delivered to a handler.
For example, to receive Press, Drag, and Release events (but not Move, Enter,
Leave, or Scroll):
var ops op.Ops
var h *Handler = ...
pointer.InputOp{
Tag: h,
Kinds: pointer.Press | pointer.Drag | pointer.Release,
}.Add(ops)
Cancel events are always delivered.
The [event.Op] operation is used to declare a handler ready for pointer
events.
# Hit areas
Clip operations from package op/clip are used for specifying
hit areas where subsequent InputOps are active.
Clip operations from package [op/clip] are used for specifying
hit areas where handlers may receive events.
For example, to set up a handler with a rectangular hit area:
r := image.Rectangle{...}
area := clip.Rect(r).Push(ops)
pointer.InputOp{Tag: h}.Add(ops)
event.Op{Tag: h}.Add(ops)
area.Pop()
Note that hit areas behave similar to painting: the effective area of a stack
@@ -54,11 +37,11 @@ For example:
var h1, h2 *Handler
area := clip.Rect(...).Push(ops)
pointer.InputOp{Tag: h1}.Add(Ops)
event.Op{Tag: h1}.Add(Ops)
area.Pop()
area := clip.Rect(...).Push(ops)
pointer.InputOp{Tag: h2}.Add(ops)
event.Op{Tag: h2}.Add(ops)
area.Pop()
implies a tree of two inner nodes, each with one pointer handler attached.
+4 -1
View File
@@ -54,8 +54,11 @@ type PassStack struct {
macroID uint32
}
// Filter matches [Event]s.
// Filter matches every [Event] that target the Tag and whose kind is
// included in Kinds. Note that only tags specified in [event.Op] can
// be targeted by pointer events.
type Filter struct {
Target event.Tag
// Kinds is a bitwise-or of event types to match.
Kinds Kind
// ScrollBounds describe the maximum scrollable distances in both
+4
View File
@@ -42,6 +42,8 @@ func (OfferCmd) ImplementsCommand() {}
// as well as [InitiateEvent] and [CancelEvent].
// Use multiple filters to offer multiple types.
type SourceFilter struct {
// Target is a tag included in a previous event.Op.
Target event.Tag
// Type is the MIME type supported by this source.
Type string
}
@@ -49,6 +51,8 @@ type SourceFilter struct {
// TargetFilter filters for any [DataEvent] whose type matches a MIME type
// as well as [CancelEvent]. Use multiple filters to accept multiple types.
type TargetFilter struct {
// Target is a tag included in a previous event.Op.
Target event.Tag
// Type is the MIME type accepted by this target.
Type string
}