io/key: [API] replace key.InputOp with a filter

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-20 09:08:39 -05:00
parent 73c3849da4
commit 27ef6dd7a2
21 changed files with 294 additions and 383 deletions
+35 -7
View File
@@ -39,10 +39,11 @@ type keyHandler struct {
visible bool
new bool
focusable bool
active bool
hint key.InputHint
order int
dirOrder int
filter key.Set
filters []key.Filter
trans f32.Affine2D
}
@@ -116,6 +117,7 @@ func (q *keyQueue) Frame(events *handlerEvents) {
h.new = false
h.visible = false
h.focusable = false
h.active = false
}
q.updateFocusLayout()
}
@@ -255,7 +257,25 @@ func (q *keyQueue) AreaFor(t event.Tag) int {
}
func (q *keyQueue) Accepts(t event.Tag, e key.Event) bool {
return q.handlers[t].filter.Contains(e.Name, e.Modifiers)
for _, f := range q.handlers[t].filters {
if keyFilterMatch(f, e) {
return true
}
}
return false
}
func keyFilterMatch(f key.Filter, e key.Event) bool {
if f.Name != e.Name {
return false
}
if e.Modifiers&f.Required != f.Required {
return false
}
if e.Modifiers&^(f.Required|f.Optional) != 0 {
return false
}
return true
}
func (q *keyQueue) Focus(focus event.Tag, events *handlerEvents) {
@@ -288,6 +308,15 @@ func (q *keyQueue) softKeyboard(show bool) {
}
}
func (q *keyQueue) filter(tag event.Tag, f key.Filter) {
h := q.handlerFor(tag)
if !h.active {
h.active = true
h.filters = h.filters[:0]
}
h.filters = append(h.filters, f)
}
func (q *keyQueue) focusable(tag event.Tag) {
h := q.handlerFor(tag)
h.focusable = true
@@ -305,14 +334,13 @@ func (q *keyQueue) handlerFor(tag event.Tag) *keyHandler {
return h
}
func (q *keyQueue) inputOp(op key.InputOp, t f32.Affine2D, area int, bounds image.Rectangle) {
h := q.handlerFor(op.Tag)
func (q *keyQueue) inputOp(tag event.Tag, t f32.Affine2D, area int, bounds image.Rectangle) {
h := q.handlerFor(tag)
if h.order == -1 {
h.order = len(q.order)
q.order = append(q.order, op.Tag)
q.dirOrder = append(q.dirOrder, dirFocusEntry{tag: op.Tag, area: area, bounds: bounds})
q.order = append(q.order, tag)
q.dirOrder = append(q.dirOrder, dirFocusEntry{tag: tag, area: area, bounds: bounds})
}
h.filter = op.Keys
h.visible = true
h.trans = t
}
+63 -50
View File
@@ -15,10 +15,10 @@ import (
"gioui.org/op/clip"
)
func TestKeyWakeup(t *testing.T) {
func TestInputWakeup(t *testing.T) {
handler := new(int)
var ops op.Ops
key.InputOp{Tag: handler}.Add(&ops)
event.InputOp(&ops, handler)
var r Router
// Test that merely adding a handler doesn't trigger redraw.
@@ -39,12 +39,12 @@ func TestKeyMultiples(t *testing.T) {
r := new(Router)
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.FocusCmd{Tag: &handlers[2]})
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
// The last one must be focused:
key.InputOp{Tag: &handlers[2]}.Add(ops)
event.InputOp(ops, &handlers[2])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -64,14 +64,14 @@ func TestKeyStacked(t *testing.T) {
ops := new(op.Ops)
r := new(Router)
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.FocusCmd{})
r.Source().Queue(key.SoftKeyboardCmd{Show: false})
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Source().Queue(key.FocusCmd{Tag: &handlers[1]})
key.InputOp{Tag: &handlers[2]}.Add(ops)
event.InputOp(ops, &handlers[2])
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
key.InputOp{Tag: &handlers[3]}.Add(ops)
event.InputOp(ops, &handlers[3])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -107,35 +107,39 @@ func TestKeyRemoveFocus(t *testing.T) {
r := new(Router)
// New InputOp with Focus and Keyboard:
key.InputOp{Tag: &handlers[0], Keys: "Short-Tab"}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
// New InputOp without any focus:
key.InputOp{Tag: &handlers[1], Keys: "Short-Tab"}.Add(ops)
event.InputOp(ops, &handlers[1])
filters := []event.Filter{
key.FocusFilter{},
key.Filter{Name: key.NameTab, Required: key.ModShortcut},
}
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
r.Events(&handlers[i], filters...)
}
r.Frame(ops)
// Add some key events:
event := event.Event(key.Event{Name: key.NameTab, Modifiers: key.ModShortcut, State: key.Press})
r.Queue(event)
evt := event.Event(key.Event{Name: key.NameTab, Modifiers: key.ModShortcut, State: key.Press})
r.Queue(evt)
assertKeyEvent(t, r.Events(&handlers[0], key.FocusFilter{}), true, event)
assertKeyEvent(t, r.Events(&handlers[1], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[0], filters...), true, evt)
assertKeyEvent(t, r.Events(&handlers[1], filters...), false)
assertFocus(t, r, &handlers[0])
assertKeyboard(t, r, TextInputOpen)
ops.Reset()
// Will get the focus removed:
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
// Unchanged:
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
// Remove focus by focusing on a tag that don't exist.
r.Source().Queue(key.FocusCmd{Tag: new(int)})
@@ -148,9 +152,8 @@ func TestKeyRemoveFocus(t *testing.T) {
ops.Reset()
key.InputOp{Tag: &handlers[0]}.Add(ops)
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[0])
event.InputOp(ops, &handlers[1])
r.Frame(ops)
@@ -164,11 +167,11 @@ func TestKeyRemoveFocus(t *testing.T) {
// Set focus to InputOp which already
// exists in the previous frame:
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
// Remove focus.
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Source().Queue(key.FocusCmd{})
r.Frame(ops)
@@ -185,11 +188,11 @@ func TestKeyFocusedInvisible(t *testing.T) {
// Set new InputOp with focus:
r.Source().Queue(key.FocusCmd{Tag: &handlers[0]})
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
r.Source().Queue(key.SoftKeyboardCmd{Show: true})
// Set new InputOp without focus:
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -209,7 +212,7 @@ func TestKeyFocusedInvisible(t *testing.T) {
//
// Unchanged:
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Frame(ops)
@@ -221,7 +224,7 @@ func TestKeyFocusedInvisible(t *testing.T) {
r.Frame(ops)
// Unchanged
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
r.Frame(ops)
@@ -229,10 +232,10 @@ func TestKeyFocusedInvisible(t *testing.T) {
// Respawn the first element:
// It must receive one `Event{Focus: false}`.
key.InputOp{Tag: &handlers[0]}.Add(ops)
event.InputOp(ops, &handlers[0])
// Unchanged
key.InputOp{Tag: &handlers[1]}.Add(ops)
event.InputOp(ops, &handlers[1])
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
@@ -263,7 +266,7 @@ func TestDirectionalFocus(t *testing.T) {
for i, bounds := range handlers {
cl := clip.Rect(bounds).Push(ops)
key.InputOp{Tag: &handlers[i]}.Add(ops)
event.InputOp(ops, &handlers[i])
cl.Pop()
r.Events(&handlers[i], key.FocusFilter{})
}
@@ -307,7 +310,6 @@ func TestFocusScroll(t *testing.T) {
r.Events(h, filters...)
parent := clip.Rect(image.Rect(1, 1, 14, 39)).Push(ops)
cl := clip.Rect(image.Rect(10, -20, 20, 30)).Push(ops)
key.InputOp{Tag: h}.Add(ops)
event.InputOp(ops, h)
// Test that h is scrolled even if behind another handler.
event.InputOp(ops, new(int))
@@ -334,7 +336,6 @@ func TestFocusClick(t *testing.T) {
}
assertEventPointerTypeSequence(t, r.Events(h, filters...), pointer.Cancel)
cl := clip.Rect(image.Rect(0, 0, 10, 10)).Push(ops)
key.InputOp{Tag: h}.Add(ops)
event.InputOp(ops, h)
cl.Pop()
r.Frame(ops)
@@ -359,21 +360,31 @@ func TestKeyRouting(t *testing.T) {
rect := clip.Rect{Max: image.Pt(10, 10)}
macro := op.Record(macroOps)
key.InputOp{Tag: &handlers[0], Keys: "A"}.Add(ops)
event.InputOp(ops, &handlers[0])
cl1 := rect.Push(ops)
key.InputOp{Tag: &handlers[1], Keys: "B"}.Add(ops)
key.InputOp{Tag: &handlers[2], Keys: "A"}.Add(ops)
event.InputOp(ops, &handlers[1])
event.InputOp(ops, &handlers[2])
cl1.Pop()
cl2 := rect.Push(ops)
key.InputOp{Tag: &handlers[3]}.Add(ops)
key.InputOp{Tag: &handlers[4], Keys: "A"}.Add(ops)
event.InputOp(ops, &handlers[3])
event.InputOp(ops, &handlers[4])
cl2.Pop()
call := macro.Stop()
call.Add(ops)
for i := range handlers {
r.Events(&handlers[i], key.FocusFilter{})
fa := []event.Filter{
key.FocusFilter{},
key.Filter{Name: "A"},
}
fb := []event.Filter{
key.FocusFilter{},
key.Filter{Name: "B"},
}
r.Events(&handlers[0], fa...)
r.Events(&handlers[1], fb...)
r.Events(&handlers[2], fa...)
r.Events(&handlers[3], key.FocusFilter{})
r.Events(&handlers[4], fa...)
r.Frame(ops)
@@ -382,17 +393,19 @@ func TestKeyRouting(t *testing.T) {
// With no focus, the events should traverse the final branch of the hit tree
// searching for handlers.
assertKeyEvent(t, r.Events(&handlers[4], key.FocusFilter{}), false, A)
assertKeyEvent(t, r.Events(&handlers[4], fa...), false, A)
assertKeyEvent(t, r.Events(&handlers[3], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[2], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[1], key.FocusFilter{}), false, B)
assertKeyEvent(t, r.Events(&handlers[0], key.FocusFilter{}), false)
assertKeyEvent(t, r.Events(&handlers[2], fa...), false)
assertKeyEvent(t, r.Events(&handlers[1], fb...), false, B)
assertKeyEvent(t, r.Events(&handlers[0], fa...), false)
r2 := new(Router)
for i := range handlers {
r2.Events(&handlers[i], key.FocusFilter{})
}
r2.Events(&handlers[0], fa...)
r2.Events(&handlers[1], fb...)
r2.Events(&handlers[2], fa...)
r2.Events(&handlers[3], key.FocusFilter{})
r2.Events(&handlers[4], fa...)
r2.Source().Queue(key.FocusCmd{Tag: &handlers[3]})
r2.Frame(ops)
@@ -401,11 +414,11 @@ func TestKeyRouting(t *testing.T) {
// With focus, the events should traverse the branch of the hit tree
// containing the focused element.
assertKeyEvent(t, r2.Events(&handlers[4], key.FocusFilter{}), false)
assertKeyEvent(t, r2.Events(&handlers[4], fa...), false)
assertKeyEvent(t, r2.Events(&handlers[3], key.FocusFilter{}), true)
assertKeyEvent(t, r2.Events(&handlers[2], key.FocusFilter{}), false)
assertKeyEvent(t, r2.Events(&handlers[1], key.FocusFilter{}), false)
assertKeyEvent(t, r2.Events(&handlers[0], key.FocusFilter{}), false, A)
assertKeyEvent(t, r2.Events(&handlers[2], fa...), false)
assertKeyEvent(t, r2.Events(&handlers[1], fb...), false)
assertKeyEvent(t, r2.Events(&handlers[0], fa...), false, A)
}
func assertKeyEvent(t *testing.T, events []event.Event, expectedFocus bool, expectedInputs ...event.Event) {
-11
View File
@@ -10,7 +10,6 @@ import (
f32internal "gioui.org/internal/f32"
"gioui.org/internal/ops"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/semantic"
"gioui.org/io/system"
@@ -43,7 +42,6 @@ type hitNode struct {
// For handler nodes.
tag event.Tag
ktag event.Tag
pass bool
}
@@ -262,15 +260,6 @@ func (q *pointerQueue) handlerFor(tag event.Tag, events *handlerEvents) *pointer
return h
}
func (c *pointerCollector) keyInputOp(op key.InputOp) {
areaID := c.currentArea()
c.addHitNode(hitNode{
area: areaID,
ktag: op.Tag,
pass: true,
})
}
func (c *pointerCollector) actionInputOp(act system.Action) {
areaID := c.currentArea()
area := &c.q.areas[areaID]
+1 -1
View File
@@ -1008,7 +1008,7 @@ func TestDeferredInputOp(t *testing.T) {
var r Router
m := op.Record(&ops)
key.InputOp{Tag: new(int)}.Add(&ops)
event.InputOp(&ops, new(int))
call := m.Stop()
op.Defer(&ops, call)
+17 -36
View File
@@ -131,6 +131,8 @@ func (s Source) Events(k event.Tag, filters ...event.Filter) []event.Event {
func (q *Router) Events(k event.Tag, filters ...event.Filter) []event.Event {
for _, f := range filters {
switch f := f.(type) {
case key.Filter:
q.key.queue.filter(k, f)
case key.FocusFilter:
q.key.queue.focusable(k)
case pointer.Filter:
@@ -167,25 +169,6 @@ func (q *Router) Frame(frame *op.Ops) {
}
}
// Queue key events to the topmost handler.
func (q *Router) QueueTopmost(events ...key.Event) bool {
var topmost event.Tag
pq := &q.pointer.queue
for _, h := range pq.hitTree {
if h.ktag != nil {
topmost = h.ktag
break
}
}
if topmost == nil {
return false
}
for _, e := range events {
q.handlers.Add(topmost, e)
}
return q.handlers.HadEvents()
}
// Queue events and report whether at least one handler had an event queued.
func (q *Router) Queue(events ...event.Event) bool {
for _, e := range events {
@@ -273,7 +256,7 @@ func (q *Router) queueKeyEvent(e key.Event) {
if focused {
// If there is a focused tag, traverse its ancestry through the
// hit tree to search for handlers.
for ; pq.hitTree[idx].ktag != f; idx-- {
for ; pq.hitTree[idx].tag != f; idx-- {
}
}
for idx != -1 {
@@ -283,11 +266,11 @@ func (q *Router) queueKeyEvent(e key.Event) {
} else {
idx--
}
if n.ktag == nil {
if n.tag == nil {
continue
}
if kq.Accepts(n.ktag, e) {
q.handlers.Add(n.ktag, e)
if kq.Accepts(n.tag, e) {
q.handlers.Add(n.tag, e)
break
}
}
@@ -479,6 +462,9 @@ func (q *Router) collect() {
case ops.TypeInput:
tag := encOp.Refs[0].(event.Tag)
pc.inputOp(tag, &q.handlers)
a := pc.currentArea()
b := pc.currentAreaBounds()
kq.inputOp(tag, t, a, b)
// Pointer ops.
case ops.TypePass:
@@ -491,17 +477,6 @@ func (q *Router) collect() {
case ops.TypeActionInput:
act := system.Action(encOp.Data[1])
pc.actionInputOp(act)
case ops.TypeKeyInput:
filter := key.Set(*encOp.Refs[1].(*string))
op := key.InputOp{
Tag: encOp.Refs[0].(event.Tag),
Keys: filter,
}
a := pc.currentArea()
b := pc.currentAreaBounds()
pc.keyInputOp(op)
kq.inputOp(op, t, a, b)
case ops.TypeKeyInputHint:
op := key.InputHintOp{
Tag: encOp.Refs[0].(event.Tag),
@@ -583,6 +558,14 @@ func (h *handlerEvents) Events(k event.Tag, filters ...event.Filter) []event.Eve
func filtersMatches(filters []event.Filter, e event.Event) bool {
switch e := e.(type) {
case key.Event:
for _, f := range filters {
if f, ok := f.(key.Filter); ok {
if keyFilterMatch(f, e) {
return true
}
}
}
case key.FocusEvent, key.SnippetEvent, key.EditEvent, key.SelectionEvent:
for _, f := range filters {
if _, ok := f.(key.FocusFilter); ok {
@@ -614,8 +597,6 @@ func filtersMatches(filters []event.Filter, e event.Event) bool {
return true
}
}
default:
return true
}
return false
}