diff --git a/ui/internal/ops/ops.go b/ui/internal/ops/ops.go index 6e577213..abc8f581 100644 --- a/ui/internal/ops/ops.go +++ b/ui/internal/ops/ops.go @@ -6,8 +6,8 @@ type OpType byte const firstOpIndex = 200 const ( - TypeBlockDef OpType = iota + firstOpIndex - TypeBlock + TypeMacroDef OpType = iota + firstOpIndex + TypeMacro TypeTransform TypeLayer TypeInvalidate @@ -27,8 +27,8 @@ const ( ) const ( - TypeBlockDefLen = 1 + 4 + 4 - TypeBlockLen = 1 + 4 + 4 + 4 + TypeMacroDefLen = 1 + 4 + 4 + TypeMacroLen = 1 + 4 + 4 + 4 TypeTransformLen = 1 + 4*2 TypeLayerLen = 1 TypeRedrawLen = 1 + 8 @@ -49,8 +49,8 @@ const ( func (t OpType) Size() int { return [...]int{ - TypeBlockDefLen, - TypeBlockLen, + TypeMacroDefLen, + TypeMacroLen, TypeTransformLen, TypeLayerLen, TypeRedrawLen, @@ -72,7 +72,7 @@ func (t OpType) Size() int { func (t OpType) NumRefs() int { switch t { - case TypeBlock, TypeImage, TypeKeyHandler, TypePointerHandler, TypeProfile: + case TypeMacro, TypeImage, TypeKeyHandler, TypePointerHandler, TypeProfile: return 1 default: return 0 diff --git a/ui/layout/flex.go b/ui/layout/flex.go index c7821401..f2dbeebe 100644 --- a/ui/layout/flex.go +++ b/ui/layout/flex.go @@ -24,7 +24,7 @@ type Flex struct { } type FlexChild struct { - block ui.BlockOp + macro ui.MacroOp dims Dimens } @@ -74,7 +74,7 @@ func (f *Flex) begin(mode flexMode) { panic("must End before adding a child") } f.mode = mode - f.ops.Begin() + f.ops.Record() } func (f *Flex) Rigid() Constraints { @@ -107,7 +107,7 @@ func (f *Flex) End(dims Dimens) FlexChild { if f.mode <= modeBegun { panic("End called without an active child") } - block := f.ops.End() + macro := f.ops.Stop() sz := axisMain(f.Axis, dims.Size) f.size += sz if f.mode == modeRigid { @@ -120,7 +120,7 @@ func (f *Flex) End(dims Dimens) FlexChild { if b := dims.Baseline; b > f.maxBaseline { f.maxBaseline = b } - return FlexChild{block, dims} + return FlexChild{macro, dims} } func (f *Flex) Layout(children ...FlexChild) Dimens { @@ -160,7 +160,7 @@ func (f *Flex) Layout(children ...FlexChild) Dimens { ui.TransformOp{ Transform: ui.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))), }.Add(f.ops) - child.block.Add(f.ops) + child.macro.Add(f.ops) ui.PopOp{}.Add(f.ops) mainSize += axisMain(f.Axis, dims.Size) if i < len(children)-1 { diff --git a/ui/layout/layout.go b/ui/layout/layout.go index b1622374..40b5b31a 100644 --- a/ui/layout/layout.go +++ b/ui/layout/layout.go @@ -129,7 +129,7 @@ func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints { a.begun = true a.ops = ops a.cs = cs - ops.Begin() + ops.Record() cs.Width.Min = 0 cs.Height.Min = 0 return cs @@ -141,7 +141,7 @@ func (a *Align) End(dims Dimens) Dimens { } a.begun = false ops := a.ops - block := ops.End() + macro := ops.Stop() sz := dims.Size if sz.X < a.cs.Width.Min { sz.X = a.cs.Width.Min @@ -164,7 +164,7 @@ func (a *Align) End(dims Dimens) Dimens { } ui.PushOp{}.Add(ops) ui.TransformOp{Transform: ui.Offset(toPointF(p))}.Add(ops) - block.Add(ops) + macro.Add(ops) ui.PopOp{}.Add(ops) return Dimens{ Size: sz, diff --git a/ui/layout/list.go b/ui/layout/list.go index 387529ea..6ab37ff3 100644 --- a/ui/layout/list.go +++ b/ui/layout/list.go @@ -14,7 +14,7 @@ import ( type scrollChild struct { size image.Point - block ui.BlockOp + macro ui.MacroOp } type List struct { @@ -70,7 +70,7 @@ func (l *List) Init(ops *ui.Ops, cs Constraints, len int) { if l.first > len { l.first = len } - ops.Begin() + ops.Record() l.Next() } @@ -103,7 +103,7 @@ func (l *List) Next() { i = l.len - 1 - i } l.index = i - l.ops.Begin() + l.ops.Record() } // Index is the current element index. @@ -146,8 +146,8 @@ func (l *List) next() (int, bool) { // Elem completes an element. func (l *List) Elem(dims Dimens) { - block := l.ops.End() - child := scrollChild{dims.Size, block} + macro := l.ops.Stop() + child := scrollChild{dims.Size, macro} switch l.dir { case iterateForward: mainSize := axisMain(l.Axis, child.size) @@ -227,7 +227,7 @@ func (l *List) Layout() Dimens { ui.TransformOp{ Transform: ui.Offset(toPointF(axisPoint(l.Axis, transPos, cross))), }.Add(ops) - child.block.Add(ops) + child.macro.Add(ops) ui.PopOp{}.Add(ops) pos += childSize } @@ -237,9 +237,9 @@ func (l *List) Layout() Dimens { l.scroll.Stop() } dims := axisPoint(l.Axis, mainc.Constrain(pos), maxCross) - block := ops.End() + macro := ops.Stop() pointer.RectAreaOp{Size: dims}.Add(ops) l.scroll.Add(ops) - block.Add(ops) + macro.Add(ops) return Dimens{Size: dims} } diff --git a/ui/layout/stack.go b/ui/layout/stack.go index f2cc2ec2..72fe54a2 100644 --- a/ui/layout/stack.go +++ b/ui/layout/stack.go @@ -20,7 +20,7 @@ type Stack struct { } type StackChild struct { - block ui.BlockOp + macro ui.MacroOp dims Dimens } @@ -54,7 +54,7 @@ func (s *Stack) begin() { panic("must End before adding a child") } s.begun = true - s.ops.Begin() + s.ops.Record() } func (s *Stack) Rigid() Constraints { @@ -71,7 +71,7 @@ func (s *Stack) Expand() Constraints { } func (s *Stack) End(dims Dimens) StackChild { - b := s.ops.End() + b := s.ops.Stop() s.begun = false if w := dims.Size.X; w > s.maxSZ.X { s.maxSZ.X = w @@ -105,7 +105,7 @@ func (s *Stack) Layout(children ...StackChild) Dimens { } ui.PushOp{}.Add(s.ops) ui.TransformOp{Transform: ui.Offset(toPointF(p))}.Add(s.ops) - ch.block.Add(s.ops) + ch.macro.Add(s.ops) ui.PopOp{}.Add(s.ops) } b := s.baseline diff --git a/ui/measure/measure.go b/ui/measure/measure.go index 923b03b8..e67dbcf7 100644 --- a/ui/measure/measure.go +++ b/ui/measure/measure.go @@ -30,7 +30,7 @@ type cachedLayout struct { type cachedPath struct { active bool - path ui.BlockOp + path ui.MacroOp } type layoutKey struct { @@ -119,7 +119,7 @@ func (f *textFace) Layout(str string, opts text.LayoutOptions) *text.Layout { return l } -func (f *textFace) Path(str text.String) ui.BlockOp { +func (f *textFace) Path(str text.String) ui.MacroOp { ppem := fixed.Int26_6(f.faces.Config.Px(f.size) * 64) pk := pathKey{ f: f.font.Font, @@ -227,14 +227,14 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOpt return &text.Layout{Lines: lines} } -func textPath(ppem fixed.Int26_6, f *opentype, str text.String) ui.BlockOp { +func textPath(ppem fixed.Int26_6, f *opentype, str text.String) ui.MacroOp { var lastPos f32.Point var builder draw.PathBuilder ops := new(ui.Ops) builder.Init(ops) var x fixed.Int26_6 var advIdx int - ops.Begin() + ops.Record() for _, r := range str.String { if !unicode.IsSpace(r) { segs, ok := f.LoadGlyph(ppem, r) @@ -287,5 +287,5 @@ func textPath(ppem fixed.Int26_6, f *opentype, str text.String) ui.BlockOp { advIdx++ } builder.End() - return ops.End() + return ops.Stop() } diff --git a/ui/ops.go b/ui/ops.go index 5857c55b..5fe671fa 100644 --- a/ui/ops.go +++ b/ui/ops.go @@ -10,7 +10,7 @@ import ( // Ops holds a list of serialized Ops. type Ops struct { - // Stack of block start indices. + // Stack of macro start indices. stack []pc ops opsData @@ -30,7 +30,7 @@ type opsData struct { // OpsReader parses an ops list. Internal use only. type OpsReader struct { pc pc - stack []block + stack []macro ops *opsData } @@ -49,7 +49,7 @@ type OpKey struct { version int } -type block struct { +type macro struct { ops *opsData retPC pc endPC pc @@ -64,13 +64,13 @@ type PushOp struct{} type PopOp struct{} -type BlockOp struct { +type MacroOp struct { ops *opsData version int pc pc } -type opBlockDef struct { +type opMacroDef struct { endpc pc } @@ -86,11 +86,12 @@ func (p PopOp) Add(o *Ops) { o.Write([]byte{byte(ops.TypePop)}) } -// Begin a block of ops. -func (o *Ops) Begin() { +// Record starts recording a macro. Multiple simultaneous +// recordings are supported. Stop ends the most recent. +func (o *Ops) Record() { o.stack = append(o.stack, o.ops.pc()) - // Make room for a block definition. Filled out in End. - o.Write(make([]byte, ops.TypeBlockDefLen)) + // Make room for a macro definition. Filled out in Stop. + o.Write(make([]byte, ops.TypeMacroDefLen)) } func (op *opAux) decode(data []byte) { @@ -103,14 +104,14 @@ func (op *opAux) decode(data []byte) { } } -func (op *opBlockDef) decode(data []byte) { - if ops.OpType(data[0]) != ops.TypeBlockDef { +func (op *opMacroDef) decode(data []byte) { + if ops.OpType(data[0]) != ops.TypeMacroDef { panic("invalid op") } bo := binary.LittleEndian dataIdx := int(bo.Uint32(data[1:])) refsIdx := int(bo.Uint32(data[5:])) - *op = opBlockDef{ + *op = opMacroDef{ endpc: pc{ data: dataIdx, refs: refsIdx, @@ -118,22 +119,22 @@ func (op *opBlockDef) decode(data []byte) { } } -// End the most recent block and return -// an op for invoking the completed block. -func (o *Ops) End() BlockOp { +// Stop the most recent recording and return the macro for later +// use. +func (o *Ops) Stop() MacroOp { if len(o.stack) == 0 { - panic(errors.New("End with no matching Begin")) + panic(errors.New("not recording a macro")) } start := o.stack[len(o.stack)-1] o.stack = o.stack[:len(o.stack)-1] pc := o.ops.pc() - // Write the block header reserved in Begin. - data := o.ops.data[start.data : start.data+ops.TypeBlockDefLen] - data[0] = byte(ops.TypeBlockDef) + // Write the macro header reserved in Begin. + data := o.ops.data[start.data : start.data+ops.TypeMacroDefLen] + data[0] = byte(ops.TypeMacroDef) bo := binary.LittleEndian bo.PutUint32(data[1:], uint32(pc.data)) bo.PutUint32(data[5:], uint32(pc.refs)) - return BlockOp{ops: &o.ops, pc: start, version: o.ops.version} + return MacroOp{ops: &o.ops, pc: start, version: o.ops.version} } // Reset the Ops, preparing it for re-use. @@ -198,15 +199,15 @@ func (d *opsData) pc() pc { return pc{data: len(d.data), refs: len(d.refs)} } -func (b *BlockOp) decode(data []byte, refs []interface{}) { - if ops.OpType(data[0]) != ops.TypeBlock { +func (b *MacroOp) decode(data []byte, refs []interface{}) { + if ops.OpType(data[0]) != ops.TypeMacro { panic("invalid op") } bo := binary.LittleEndian dataIdx := int(bo.Uint32(data[1:])) refsIdx := int(bo.Uint32(data[5:])) version := int(bo.Uint32(data[9:])) - *b = BlockOp{ + *b = MacroOp{ ops: refs[0].(*opsData), pc: pc{ data: dataIdx, @@ -216,12 +217,12 @@ func (b *BlockOp) decode(data []byte, refs []interface{}) { } } -func (b BlockOp) Add(o *Ops) { +func (b MacroOp) Add(o *Ops) { if b.ops == nil { return } - data := make([]byte, ops.TypeBlockLen) - data[0] = byte(ops.TypeBlock) + data := make([]byte, ops.TypeMacroLen) + data[0] = byte(ops.TypeMacro) bo := binary.LittleEndian bo.PutUint32(data[1:], uint32(b.pc.data)) bo.PutUint32(data[5:], uint32(b.pc.refs)) @@ -272,33 +273,33 @@ func (r *OpsReader) Decode() (EncodedOp, bool) { op.decode(data) n += op.len data = r.ops.data[r.pc.data : r.pc.data+n] - case ops.TypeBlock: - var op BlockOp + case ops.TypeMacro: + var op MacroOp op.decode(data, refs) - blockOps := op.ops - if ops.OpType(blockOps.data[op.pc.data]) != ops.TypeBlockDef { - panic("invalid block reference") + macroOps := op.ops + if ops.OpType(macroOps.data[op.pc.data]) != ops.TypeMacroDef { + panic("invalid macro reference") } if op.version != op.ops.version { - panic("invalid BlockOp reference to reset Ops") + panic("invalid MacroOp reference to reset Ops") } - var opDef opBlockDef - opDef.decode(blockOps.data[op.pc.data : op.pc.data+ops.TypeBlockDef.Size()]) + var opDef opMacroDef + opDef.decode(macroOps.data[op.pc.data : op.pc.data+ops.TypeMacroDef.Size()]) retPC := r.pc retPC.data += n retPC.refs += nrefs - r.stack = append(r.stack, block{ + r.stack = append(r.stack, macro{ ops: r.ops, retPC: retPC, endPC: opDef.endpc, }) - r.ops = blockOps + r.ops = macroOps r.pc = op.pc - r.pc.data += ops.TypeBlockDef.Size() - r.pc.refs += ops.TypeBlockDef.NumRefs() + r.pc.data += ops.TypeMacroDef.Size() + r.pc.refs += ops.TypeMacroDef.NumRefs() continue - case ops.TypeBlockDef: - var op opBlockDef + case ops.TypeMacroDef: + var op opMacroDef op.decode(data) r.pc = op.endpc continue diff --git a/ui/text/editor.go b/ui/text/editor.go index 1a1c61b0..bd4b03e8 100644 --- a/ui/text/editor.go +++ b/ui/text/editor.go @@ -28,9 +28,9 @@ type Editor struct { SingleLine bool Submit bool - Material ui.BlockOp + Material ui.MacroOp Hint string - HintMaterial ui.BlockOp + HintMaterial ui.MacroOp oldScale int blinkStart time.Time diff --git a/ui/text/label.go b/ui/text/label.go index 3c847578..56d21e23 100644 --- a/ui/text/label.go +++ b/ui/text/label.go @@ -18,7 +18,7 @@ import ( type Label struct { Face Face - Material ui.BlockOp + Material ui.MacroOp Alignment Alignment Text string MaxLines int diff --git a/ui/text/measure.go b/ui/text/measure.go index 158eae59..5a644191 100644 --- a/ui/text/measure.go +++ b/ui/text/measure.go @@ -40,7 +40,7 @@ type LayoutOptions struct { type Face interface { Layout(str string, opts LayoutOptions) *Layout - Path(str String) ui.BlockOp + Path(str String) ui.MacroOp } type Alignment uint8