~eliasnaur/gio

edc81ea0bb18d88194bde84b27cc6f929cc81466 — Elias Naur 1 year, 10 months ago 0768fbe
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>
M internal/opconst/ops.go => internal/opconst/ops.go +2 -2
@@ 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

M internal/ops/reader.go => internal/ops/reader.go +4 -15
@@ 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,
	}
}

M layout/flex.go => layout/flex.go +1 -1
@@ 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 {

M layout/layout.go => layout/layout.go +1 -1
@@ 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,

M layout/list.go => layout/list.go +2 -2
@@ 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}
}


M layout/stack.go => layout/stack.go +1 -1
@@ 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 {

M op/clip/clip.go => op/clip/clip.go +1 -1
@@ 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

M op/op.go => op/op.go +4 -9
@@ 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) {

M widget/material/editor.go => widget/material/editor.go +1 -1
@@ 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)