op: move Ops internal methods and state to internal package ops

Merge package opconsts into ops as well; it only existed to break
import cycles.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-10-04 15:44:48 +02:00
parent 936c266b03
commit 391725b9d0
18 changed files with 558 additions and 561 deletions
+8 -9
View File
@@ -3,7 +3,6 @@
package router
import (
"gioui.org/internal/opconst"
"gioui.org/internal/ops"
"gioui.org/io/event"
"gioui.org/io/key"
@@ -61,7 +60,7 @@ func (q *keyQueue) Frame(root *op.Ops, events *handlerEvents) {
for _, h := range q.handlers {
h.visible, h.new = false, false
}
q.reader.Reset(root)
q.reader.Reset(&root.Internal)
focus, changed, state := q.resolveFocus(events)
for k, h := range q.handlers {
@@ -104,19 +103,19 @@ 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 opconst.OpType(encOp.Data[0]) {
case opconst.TypeKeyFocus:
switch ops.OpType(encOp.Data[0]) {
case ops.TypeKeyFocus:
op := decodeFocusOp(encOp.Data, encOp.Refs)
changed = true
focus = op.Tag
case opconst.TypeKeySoftKeyboard:
case ops.TypeKeySoftKeyboard:
op := decodeSoftKeyboardOp(encOp.Data, encOp.Refs)
if op.Show {
state = TextInputOpen
} else {
state = TextInputClose
}
case opconst.TypeKeyInput:
case ops.TypeKeyInput:
op := decodeKeyInputOp(encOp.Data, encOp.Refs)
h, ok := q.handlers[op.Tag]
if !ok {
@@ -131,7 +130,7 @@ func (q *keyQueue) resolveFocus(events *handlerEvents) (focus event.Tag, changed
}
func decodeKeyInputOp(d []byte, refs []interface{}) key.InputOp {
if opconst.OpType(d[0]) != opconst.TypeKeyInput {
if ops.OpType(d[0]) != ops.TypeKeyInput {
panic("invalid op")
}
return key.InputOp{
@@ -141,7 +140,7 @@ func decodeKeyInputOp(d []byte, refs []interface{}) key.InputOp {
}
func decodeSoftKeyboardOp(d []byte, refs []interface{}) key.SoftKeyboardOp {
if opconst.OpType(d[0]) != opconst.TypeKeySoftKeyboard {
if ops.OpType(d[0]) != ops.TypeKeySoftKeyboard {
panic("invalid op")
}
return key.SoftKeyboardOp{
@@ -150,7 +149,7 @@ func decodeSoftKeyboardOp(d []byte, refs []interface{}) key.SoftKeyboardOp {
}
func decodeFocusOp(d []byte, refs []interface{}) key.FocusOp {
if opconst.OpType(d[0]) != opconst.TypeKeyFocus {
if ops.OpType(d[0]) != ops.TypeKeyFocus {
panic("invalid op")
}
return key.FocusOp{
+11 -12
View File
@@ -7,7 +7,6 @@ import (
"image"
"gioui.org/f32"
"gioui.org/internal/opconst"
"gioui.org/internal/ops"
"gioui.org/io/event"
"gioui.org/io/pointer"
@@ -107,15 +106,15 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents) {
}
reset()
for encOp, ok := r.Decode(); ok; encOp, ok = r.Decode() {
switch opconst.OpType(encOp.Data[0]) {
case opconst.TypeSave:
switch ops.OpType(encOp.Data[0]) {
case ops.TypeSave:
id := ops.DecodeSave(encOp.Data)
q.save(id, state.t)
case opconst.TypeLoad:
case ops.TypeLoad:
reset()
id := ops.DecodeLoad(encOp.Data)
state.t = q.states[id]
case opconst.TypeArea:
case ops.TypeArea:
var op areaOp
op.Decode(encOp.Data)
area := -1
@@ -130,21 +129,21 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents) {
area: len(q.areas) - 1,
})
state.node = len(q.hitTree) - 1
case opconst.TypePopArea:
case ops.TypePopArea:
n := len(q.nodeStack)
state.node = q.nodeStack[n-1]
q.nodeStack = q.nodeStack[:n-1]
case opconst.TypeTransform:
case ops.TypeTransform:
dop, push := ops.DecodeTransform(encOp.Data)
if push {
q.transStack = append(q.transStack, state.t)
}
state.t = state.t.Mul(dop)
case opconst.TypePopTransform:
case ops.TypePopTransform:
n := len(q.transStack)
state.t = q.transStack[n-1]
q.transStack = q.transStack[:n-1]
case opconst.TypePointerInput:
case ops.TypePointerInput:
op := pointer.InputOp{
Tag: encOp.Refs[0].(event.Tag),
Grab: encOp.Data[1] != 0,
@@ -184,7 +183,7 @@ func (q *pointerQueue) collectHandlers(r *ops.Reader, events *handlerEvents) {
Y: int(int32(bo(encOp.Data[15:]))),
},
}
case opconst.TypeCursor:
case ops.TypeCursor:
q.cursors = append(q.cursors, cursorNode{
name: encOp.Refs[0].(pointer.CursorName),
area: len(q.areas) - 1,
@@ -258,7 +257,7 @@ func (q *pointerQueue) Frame(root *op.Ops, events *handlerEvents) {
q.nodeStack = q.nodeStack[:0]
q.transStack = q.transStack[:0]
q.cursors = q.cursors[:0]
q.reader.Reset(root)
q.reader.Reset(&root.Internal)
q.collectHandlers(&q.reader, events)
for k, h := range q.handlers {
if !h.active {
@@ -476,7 +475,7 @@ func opDecodeFloat32(d []byte) float32 {
}
func (op *areaOp) Decode(d []byte) {
if opconst.OpType(d[0]) != opconst.TypeArea {
if ops.OpType(d[0]) != ops.TypeArea {
panic("invalid op")
}
rect := f32.Rectangle{
+2 -2
View File
@@ -761,14 +761,14 @@ func BenchmarkAreaOp_Decode(b *testing.B) {
ops := new(op.Ops)
pointer.Rect(image.Rectangle{Max: image.Pt(100, 100)}).Push(ops).Pop()
for i := 0; i < b.N; i++ {
benchAreaOp.Decode(ops.Data())
benchAreaOp.Decode(ops.Internal.Data())
}
}
func BenchmarkAreaOp_Hit(b *testing.B) {
ops := new(op.Ops)
pointer.Rect(image.Rectangle{Max: image.Pt(100, 100)}).Push(ops).Pop()
benchAreaOp.Decode(ops.Data())
benchAreaOp.Decode(ops.Internal.Data())
for i := 0; i < b.N; i++ {
benchAreaOp.Hit(f32.Pt(50, 50))
}
+8 -9
View File
@@ -14,7 +14,6 @@ import (
"encoding/binary"
"time"
"gioui.org/internal/opconst"
"gioui.org/internal/ops"
"gioui.org/io/clipboard"
"gioui.org/io/event"
@@ -68,7 +67,7 @@ func (q *Router) Frame(ops *op.Ops) {
for k := range q.profHandlers {
delete(q.profHandlers, k)
}
q.reader.Reset(ops)
q.reader.Reset(&ops.Internal)
q.collect()
q.pqueue.Frame(ops, &q.handlers)
@@ -126,22 +125,22 @@ func (q *Router) Cursor() pointer.CursorName {
func (q *Router) collect() {
for encOp, ok := q.reader.Decode(); ok; encOp, ok = q.reader.Decode() {
switch opconst.OpType(encOp.Data[0]) {
case opconst.TypeInvalidate:
switch ops.OpType(encOp.Data[0]) {
case ops.TypeInvalidate:
op := decodeInvalidateOp(encOp.Data)
if !q.wakeup || op.At.Before(q.wakeupTime) {
q.wakeup = true
q.wakeupTime = op.At
}
case opconst.TypeProfile:
case ops.TypeProfile:
op := decodeProfileOp(encOp.Data, encOp.Refs)
if q.profHandlers == nil {
q.profHandlers = make(map[event.Tag]struct{})
}
q.profHandlers[op.Tag] = struct{}{}
case opconst.TypeClipboardRead:
case ops.TypeClipboardRead:
q.cqueue.ProcessReadClipboard(encOp.Refs)
case opconst.TypeClipboardWrite:
case ops.TypeClipboardWrite:
q.cqueue.ProcessWriteClipboard(encOp.Refs)
}
}
@@ -206,7 +205,7 @@ func (h *handlerEvents) Clear() {
}
func decodeProfileOp(d []byte, refs []interface{}) profile.Op {
if opconst.OpType(d[0]) != opconst.TypeProfile {
if ops.OpType(d[0]) != ops.TypeProfile {
panic("invalid op")
}
return profile.Op{
@@ -216,7 +215,7 @@ func decodeProfileOp(d []byte, refs []interface{}) profile.Op {
func decodeInvalidateOp(d []byte) op.InvalidateOp {
bo := binary.LittleEndian
if opconst.OpType(d[0]) != opconst.TypeInvalidate {
if ops.OpType(d[0]) != ops.TypeInvalidate {
panic("invalid op")
}
var o op.InvalidateOp