op: add op.Push and op.Record funcs

The funcs replace stack.Push and macro.Record, which become private.
This makes stack and macro faster to write, in particular for stacks
where you can just write the following line to save and restore the
state :

  defer op.Push(ops).Pop()

This usage requires Push to return a pointer (since Pop has a pointer
receiver), or else the code doesn't compile.

For consistancy, I tried to do the same for op.Record, but this implied
to turn all the MacroOp fields into pointers, and this caused some
panics. As a result, op.Record doesn't return a pointer.

An other side effect pointed by Larry Clapp: StackOp and MacroOp are not
re-usable any more, you have to allocate a new one for each usage, using
the described funcs above.

Signed-off-by: Thomas Bruyelle <thomas.bruyelle@gmail.com>
This commit is contained in:
Thomas Bruyelle
2020-05-28 10:50:48 +02:00
committed by Elias Naur
parent bade277876
commit ae8a377cda
16 changed files with 62 additions and 79 deletions
+7 -10
View File
@@ -89,15 +89,14 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions {
}
crossMin, crossMax := axisCrossConstraint(f.Axis, cs)
cs = axisConstraints(f.Axis, 0, mainMax, crossMin, crossMax)
var m op.MacroOp
m.Record(gtx.Ops)
macro := op.Record(gtx.Ops)
gtx := gtx
gtx.Constraints = cs
dims := child.widget(gtx)
m.Stop()
macro.Stop()
sz := axisMain(f.Axis, dims.Size)
size += sz
children[i].macro = m
children[i].macro = macro
children[i].dims = dims
}
rigidSize := size
@@ -124,15 +123,14 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions {
}
crossMin, crossMax := axisCrossConstraint(f.Axis, cs)
cs = axisConstraints(f.Axis, flexSize, flexSize, crossMin, crossMax)
var m op.MacroOp
m.Record(gtx.Ops)
macro := op.Record(gtx.Ops)
gtx := gtx
gtx.Constraints = cs
dims := child.widget(gtx)
m.Stop()
macro.Stop()
sz := axisMain(f.Axis, dims.Size)
size += sz
children[i].macro = m
children[i].macro = macro
children[i].dims = dims
}
var maxCross int
@@ -176,8 +174,7 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions {
cross = maxBaseline - b
}
}
var stack op.StackOp
stack.Push(gtx.Ops)
stack := op.Push(gtx.Ops)
op.TransformOp{}.Offset(FPt(axisPoint(f.Axis, mainSize, cross))).Add(gtx.Ops)
child.macro.Add()
stack.Pop()
+3 -6
View File
@@ -135,8 +135,7 @@ func (in Inset) Layout(gtx Context, w Widget) Dimensions {
if mcs.Min.Y > mcs.Max.Y {
mcs.Min.Y = mcs.Max.Y
}
var stack op.StackOp
stack.Push(gtx.Ops)
stack := op.Push(gtx.Ops)
op.TransformOp{}.Offset(FPt(image.Point{X: left, Y: top})).Add(gtx.Ops)
gtx.Constraints = mcs
dims := w(gtx)
@@ -155,8 +154,7 @@ func UniformInset(v unit.Value) Inset {
// Layout a widget according to the direction.
func (a Direction) Layout(gtx Context, w Widget) Dimensions {
var macro op.MacroOp
macro.Record(gtx.Ops)
macro := op.Record(gtx.Ops)
cs := gtx.Constraints
gtx.Constraints.Min = image.Point{}
dims := w(gtx)
@@ -181,8 +179,7 @@ func (a Direction) Layout(gtx Context, w Widget) Dimensions {
case SW, S, SE:
p.Y = sz.Y - dims.Size.Y
}
var stack op.StackOp
stack.Push(gtx.Ops)
stack := op.Push(gtx.Ops)
op.TransformOp{}.Offset(FPt(p)).Add(gtx.Ops)
macro.Add()
stack.Pop()
+4 -7
View File
@@ -95,7 +95,7 @@ func (l *List) init(gtx Context, len int) {
l.Position.Offset = 0
l.Position.First = len
}
l.macro.Record(gtx.Ops)
l.macro = op.Record(gtx.Ops)
l.next()
}
@@ -137,7 +137,7 @@ func (l *List) next() {
l.dir = l.nextDir()
}
if l.more() {
l.child.Record(l.ctx.Ops)
l.child = op.Record(l.ctx.Ops)
}
}
@@ -257,8 +257,7 @@ func (l *List) layout() Dimensions {
Min: axisPoint(l.Axis, min, -inf),
Max: axisPoint(l.Axis, max, inf),
}
var stack op.StackOp
stack.Push(ops)
stack := op.Push(ops)
clip.Rect{Rect: FRect(r)}.Op(ops).Add(ops)
op.TransformOp{}.Offset(FPt(axisPoint(l.Axis, pos, cross))).Add(ops)
child.macro.Add()
@@ -279,9 +278,7 @@ func (l *List) layout() Dimensions {
}
dims := axisPoint(l.Axis, pos, maxCross)
l.macro.Stop()
var st op.StackOp
st.Push(l.ctx.Ops)
defer st.Pop()
defer op.Push(l.ctx.Ops).Pop()
pointer.Rect(image.Rectangle{Max: dims}).Add(ops)
l.scroll.Add(ops)
l.macro.Add()
+7 -10
View File
@@ -54,19 +54,18 @@ func (s Stack) Layout(gtx Context, children ...StackChild) Dimensions {
if w.expanded {
continue
}
var m op.MacroOp
m.Record(gtx.Ops)
macro := op.Record(gtx.Ops)
gtx := gtx
gtx.Constraints.Min = image.Pt(0, 0)
dims := w.widget(gtx)
m.Stop()
macro.Stop()
if w := dims.Size.X; w > maxSZ.X {
maxSZ.X = w
}
if h := dims.Size.Y; h > maxSZ.Y {
maxSZ.Y = h
}
children[i].macro = m
children[i].macro = macro
children[i].dims = dims
}
// Then lay out Expanded children.
@@ -74,21 +73,20 @@ func (s Stack) Layout(gtx Context, children ...StackChild) Dimensions {
if !w.expanded {
continue
}
var m op.MacroOp
m.Record(gtx.Ops)
macro := op.Record(gtx.Ops)
gtx := gtx
gtx.Constraints = Constraints{
Min: maxSZ, Max: gtx.Constraints.Max,
}
dims := w.widget(gtx)
m.Stop()
macro.Stop()
if w := dims.Size.X; w > maxSZ.X {
maxSZ.X = w
}
if h := dims.Size.Y; h > maxSZ.Y {
maxSZ.Y = h
}
children[i].macro = m
children[i].macro = macro
children[i].dims = dims
}
@@ -109,8 +107,7 @@ func (s Stack) Layout(gtx Context, children ...StackChild) Dimensions {
case SW, S, SE:
p.Y = maxSZ.Y - sz.Y
}
var stack op.StackOp
stack.Push(gtx.Ops)
stack := op.Push(gtx.Ops)
op.TransformOp{}.Offset(FPt(p)).Add(gtx.Ops)
ch.macro.Add()
stack.Pop()