io/router: avoid a decode round for key ops

Like a previous change for pointer ops, process key ops during the
router decode of ops. This is a performance optimization and preparation
for processing future accessibility ops without without another decode
loop.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-10-22 14:38:08 +02:00
parent 60846d112b
commit e4b96cb779
3 changed files with 81 additions and 48 deletions
+53 -39
View File
@@ -6,7 +6,6 @@ import (
"gioui.org/internal/ops"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/op"
)
type TextInputState uint8
@@ -14,7 +13,6 @@ type TextInputState uint8
type keyQueue struct {
focus event.Tag
handlers map[event.Tag]*keyHandler
reader ops.Reader
state TextInputState
hint key.InputHint
}
@@ -27,6 +25,14 @@ type keyHandler struct {
hint key.InputHint
}
// keyCollector tracks state required to update a keyQueue
// from key ops.
type keyCollector struct {
q *keyQueue
focus event.Tag
changed bool
}
const (
TextInputKeep TextInputState = iota
TextInputClose
@@ -53,46 +59,46 @@ func (q *keyQueue) InputHint() (key.InputHint, bool) {
return q.hint, old != q.hint
}
func (q *keyQueue) Frame(root *op.Ops, events *handlerEvents) {
func (q *keyQueue) Reset() {
if q.handlers == nil {
q.handlers = make(map[event.Tag]*keyHandler)
}
for _, h := range q.handlers {
h.visible, h.new = false, false
}
q.reader.Reset(&root.Internal)
q.state = TextInputKeep
}
focus, changed, state := q.resolveFocus(events)
func (q *keyQueue) Frame(events *handlerEvents, collector keyCollector) {
for k, h := range q.handlers {
if !h.visible {
delete(q.handlers, k)
if q.focus == k {
// Remove the focus from the handler that is no longer visible.
q.focus = nil
state = TextInputClose
q.state = TextInputClose
}
} else if h.new && k != focus {
} else if h.new && k != collector.focus {
// Reset the handler on (each) first appearance, but don't trigger redraw.
events.AddNoRedraw(k, key.FocusEvent{Focus: false})
}
}
if changed && focus != nil {
if _, exists := q.handlers[focus]; !exists {
focus = nil
if collector.changed && collector.focus != nil {
if _, exists := q.handlers[collector.focus]; !exists {
collector.focus = nil
}
}
if changed && focus != q.focus {
if collector.changed && collector.focus != q.focus {
if q.focus != nil {
events.Add(q.focus, key.FocusEvent{Focus: false})
}
q.focus = focus
q.focus = collector.focus
if q.focus != nil {
events.Add(q.focus, key.FocusEvent{Focus: true})
} else {
state = TextInputClose
q.state = TextInputClose
}
}
q.state = state
}
func (q *keyQueue) Push(e event.Event, events *handlerEvents) {
@@ -101,32 +107,27 @@ func (q *keyQueue) Push(e event.Event, events *handlerEvents) {
}
}
func (q *keyQueue) resolveFocus(events *handlerEvents) (focus event.Tag, changed bool, state TextInputState) {
for encOp, ok := q.reader.Decode(); ok; encOp, ok = q.reader.Decode() {
switch ops.OpType(encOp.Data[0]) {
case ops.TypeKeyFocus:
op := decodeFocusOp(encOp.Data, encOp.Refs)
changed = true
focus = op.Tag
case ops.TypeKeySoftKeyboard:
op := decodeSoftKeyboardOp(encOp.Data, encOp.Refs)
if op.Show {
state = TextInputOpen
} else {
state = TextInputClose
}
case ops.TypeKeyInput:
op := decodeKeyInputOp(encOp.Data, encOp.Refs)
h, ok := q.handlers[op.Tag]
if !ok {
h = &keyHandler{new: true}
q.handlers[op.Tag] = h
}
h.visible = true
h.hint = op.Hint
}
func (k *keyCollector) focusOp(tag event.Tag) {
k.focus = tag
k.changed = true
}
func (k *keyCollector) softKeyboard(show bool) {
if show {
k.q.state = TextInputOpen
} else {
k.q.state = TextInputClose
}
return
}
func (k *keyCollector) inputOp(op key.InputOp) {
h, ok := k.q.handlers[op.Tag]
if !ok {
h = &keyHandler{new: true}
k.q.handlers[op.Tag] = h
}
h.visible = true
h.hint = op.Hint
}
func decodeKeyInputOp(d []byte, refs []interface{}) key.InputOp {
@@ -156,3 +157,16 @@ func decodeFocusOp(d []byte, refs []interface{}) key.FocusOp {
Tag: refs[0],
}
}
func (t TextInputState) String() string {
switch t {
case TextInputKeep:
return "Keep"
case TextInputClose:
return "Close"
case TextInputOpen:
return "Open"
default:
panic("unexpected value")
}
}