op: remove operation list argument from MacroOp.Add

The ability to invoke other operation lists belongs in the new CallOp.

While we're here, make MacroOp.Add use a pointer receiver to match the
other methods.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-12-11 23:16:59 +01:00
parent 0768fbe590
commit edc81ea0bb
9 changed files with 17 additions and 33 deletions
+2 -2
View File
@@ -31,7 +31,7 @@ const (
const (
TypeMacroDefLen = 1 + 4 + 4
TypeMacroLen = 1 + 4 + 4 + 4
TypeMacroLen = 1 + 4 + 4
TypeTransformLen = 1 + 4*2
TypeLayerLen = 1
TypeRedrawLen = 1 + 8
@@ -77,7 +77,7 @@ func (t OpType) Size() int {
func (t OpType) NumRefs() int {
switch t {
case TypeMacro, TypeKeyInput, TypePointerInput, TypeProfile, TypeCall:
case TypeKeyInput, TypePointerInput, TypeProfile, TypeCall:
return 1
case TypeImage:
return 2
+4 -15
View File
@@ -33,9 +33,7 @@ type Key struct {
// Shadow of op.MacroOp.
type macroOp struct {
ops *op.Ops
version int
pc pc
pc pc
}
// Shadow of op.CallOp.
@@ -119,16 +117,11 @@ func (r *Reader) Decode() (EncodedOp, bool) {
continue
case opconst.TypeMacro:
var op macroOp
op.decode(data, refs)
macroOps := op.ops
macroData := macroOps.Data()
macroData = macroData[op.pc.data:]
op.decode(data)
macroData := r.ops.Data()[op.pc.data:]
if opconst.OpType(macroData[0]) != opconst.TypeMacroDef {
panic("invalid macro reference")
}
if op.version != op.ops.Version() {
panic("invalid MacroOp reference to reset Ops")
}
var opDef opMacroDef
opDef.decode(macroData[:opconst.TypeMacroDef.Size()])
retPC := r.pc
@@ -139,7 +132,6 @@ func (r *Reader) Decode() (EncodedOp, bool) {
retPC: retPC,
endPC: opDef.endpc,
})
r.ops = macroOps
r.pc = op.pc
r.pc.data += opconst.TypeMacroDef.Size()
r.pc.refs += opconst.TypeMacroDef.NumRefs()
@@ -180,20 +172,17 @@ func (m *callOp) decode(data []byte, refs []interface{}) {
}
}
func (m *macroOp) decode(data []byte, refs []interface{}) {
func (m *macroOp) decode(data []byte) {
if opconst.OpType(data[0]) != opconst.TypeMacro {
panic("invalid op")
}
bo := binary.LittleEndian
dataIdx := int(int32(bo.Uint32(data[1:])))
refsIdx := int(int32(bo.Uint32(data[5:])))
version := int(int32(bo.Uint32(data[9:])))
*m = macroOp{
ops: refs[0].(*op.Ops),
pc: pc{
data: dataIdx,
refs: refsIdx,
},
version: version,
}
}
+1 -1
View File
@@ -173,7 +173,7 @@ func (f *Flex) Layout(gtx *Context, children ...FlexChild) {
var stack op.StackOp
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))).Add(gtx.Ops)
child.macro.Add(gtx.Ops)
child.macro.Add()
stack.Pop()
mainSize += axisMain(f.Axis, dims.Size)
if i < len(children)-1 {
+1 -1
View File
@@ -173,7 +173,7 @@ func (a Align) Layout(gtx *Context, w Widget) {
var stack op.StackOp
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops)
macro.Add(gtx.Ops)
macro.Add()
stack.Pop()
gtx.Dimensions = Dimensions{
Size: sz,
+2 -2
View File
@@ -262,7 +262,7 @@ func (l *List) layout() Dimensions {
stack.Push(ops)
clip.Rect{Rect: toRectF(r)}.Op(ops).Add(ops)
op.TransformOp{}.Offset(toPointF(axisPoint(l.Axis, pos, cross))).Add(ops)
child.macro.Add(ops)
child.macro.Add()
stack.Pop()
pos += childSize
}
@@ -276,7 +276,7 @@ func (l *List) layout() Dimensions {
l.macro.Stop()
pointer.Rect(image.Rectangle{Max: dims}).Add(ops)
l.scroll.Add(ops)
l.macro.Add(ops)
l.macro.Add()
return Dimensions{Size: dims}
}
+1 -1
View File
@@ -97,7 +97,7 @@ func (s *Stack) Layout(gtx *Context, children ...StackChild) {
var stack op.StackOp
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops)
ch.macro.Add(gtx.Ops)
ch.macro.Add()
stack.Pop()
if baseline == 0 {
if b := ch.dims.Baseline; b != 0 {
+1 -1
View File
@@ -40,7 +40,7 @@ type Op struct {
}
func (p Op) Add(o *op.Ops) {
p.macro.Add(o)
p.macro.Add()
data := o.Write(opconst.TypeClipLen)
data[0] = byte(opconst.TypeClip)
bo := binary.LittleEndian
+4 -9
View File
@@ -63,7 +63,7 @@ The MacroOp records a list of operations to be executed later:
macro.Stop()
// replay the recorded operations by calling Add:
macro.Add(ops)
macro.Add()
*/
package op
@@ -106,7 +106,6 @@ type StackOp struct {
type MacroOp struct {
recording bool
ops *Ops
version int
id stackID
pc pc
}
@@ -255,25 +254,21 @@ func (m *MacroOp) fill() {
bo := binary.LittleEndian
bo.PutUint32(data[1:], uint32(pc.data))
bo.PutUint32(data[5:], uint32(pc.refs))
m.version = m.ops.version
}
// Add the recorded list of operations. The Ops
// argument may be different than the Ops argument
// passed to Record.
func (m MacroOp) Add(o *Ops) {
// Add the recorded list of operations.
func (m *MacroOp) Add() {
if m.recording {
panic("a recording is in progress")
}
if m.ops == nil {
return
}
data := o.Write(opconst.TypeMacroLen, m.ops)
data := m.ops.Write(opconst.TypeMacroLen)
data[0] = byte(opconst.TypeMacro)
bo := binary.LittleEndian
bo.PutUint32(data[1:], uint32(m.pc.data))
bo.PutUint32(data[5:], uint32(m.pc.refs))
bo.PutUint32(data[9:], uint32(m.version))
}
func (r InvalidateOp) Add(o *Ops) {
+1 -1
View File
@@ -56,7 +56,7 @@ func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) {
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
editor.PaintText(gtx)
} else {
macro.Add(gtx.Ops)
macro.Add()
}
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
editor.PaintCaret(gtx)