io/input,io/key: [API] replace SelectionOp with command

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-09 17:04:22 -05:00
parent 627e028d3c
commit 813d836641
5 changed files with 29 additions and 54 deletions
+16 -6
View File
@@ -42,6 +42,7 @@ type keyHandler struct {
order int
dirOrder int
filter key.Set
trans f32.Affine2D
}
type dirFocusEntry struct {
@@ -290,19 +291,28 @@ func (q *keyQueue) handlerFor(tag event.Tag, area int, bounds image.Rectangle) *
return h
}
func (q *keyQueue) inputOp(op key.InputOp, area int, bounds image.Rectangle) {
func (q *keyQueue) inputOp(op key.InputOp, t f32.Affine2D, area int, bounds image.Rectangle) {
h := q.handlerFor(op.Tag, area, bounds)
h.visible = true
h.hint = op.Hint
h.filter = op.Keys
h.trans = t
}
func (q *keyQueue) selectionOp(t f32.Affine2D, op key.SelectionOp) {
if op.Tag == q.focus {
q.content.Selection.Range = op.Range
q.content.Selection.Caret = op.Caret
q.content.Selection.Transform = t
func (q *keyQueue) setSelection(req key.SelectionCmd) {
if req.Tag != q.focus {
return
}
q.content.Selection.Range = req.Range
q.content.Selection.Caret = req.Caret
}
func (q *keyQueue) editorState() EditorState {
s := q.content
if f := q.focus; f != nil {
s.Selection.Transform = q.handlers[f].trans
}
return s
}
func (q *keyQueue) snippetOp(op key.SnippetOp) {
+4 -20
View File
@@ -6,7 +6,6 @@ import (
"encoding/binary"
"image"
"io"
"math"
"strings"
"time"
@@ -214,6 +213,8 @@ func (q *Router) queue(f Command) {
func (q *Router) executeCommands() {
for _, req := range q.commands {
switch req := req.(type) {
case key.SelectionCmd:
q.key.queue.setSelection(req)
case key.FocusCmd:
q.key.queue.Focus(req.Tag, &q.handlers)
case key.SoftKeyboardCmd:
@@ -403,7 +404,7 @@ func (q *Router) AppendSemantics(nodes []SemanticNode) []SemanticNode {
// EditorState returns the editor state for the focused handler, or the
// zero value if there is none.
func (q *Router) EditorState() EditorState {
return q.key.queue.content
return q.key.queue.editorState()
}
func (q *Router) collect() {
@@ -516,7 +517,7 @@ func (q *Router) collect() {
a := pc.currentArea()
b := pc.currentAreaBounds()
pc.keyInputOp(op)
kq.inputOp(op, a, b)
kq.inputOp(op, t, a, b)
case ops.TypeSnippet:
op := key.SnippetOp{
Tag: encOp.Refs[0].(event.Tag),
@@ -529,23 +530,6 @@ func (q *Router) collect() {
},
}
kq.snippetOp(op)
case ops.TypeSelection:
op := key.SelectionOp{
Tag: encOp.Refs[0].(event.Tag),
Range: key.Range{
Start: int(int32(bo.Uint32(encOp.Data[1:]))),
End: int(int32(bo.Uint32(encOp.Data[5:]))),
},
Caret: key.Caret{
Pos: f32.Point{
X: math.Float32frombits(bo.Uint32(encOp.Data[9:])),
Y: math.Float32frombits(bo.Uint32(encOp.Data[13:])),
},
Ascent: math.Float32frombits(bo.Uint32(encOp.Data[17:])),
Descent: math.Float32frombits(bo.Uint32(encOp.Data[21:])),
},
}
kq.selectionOp(t, op)
// Semantic ops.
case ops.TypeSemanticLabel: