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
+1 -2
View File
@@ -188,8 +188,7 @@ func drawInk(gtx layout.Context, c widget.Press) {
return
}
t = t / duration
var stack op.StackOp
stack.Push(gtx.Ops)
stack := op.Push(gtx.Ops)
size := float32(gtx.Px(unit.Dp(700))) * t
rr := size * .5
col := byte(0xaa * (1 - t*t))
+2 -5
View File
@@ -39,10 +39,8 @@ func Editor(th *Theme, editor *widget.Editor, hint string) EditorStyle {
}
func (e EditorStyle) Layout(gtx layout.Context) layout.Dimensions {
var stack op.StackOp
stack.Push(gtx.Ops)
var macro op.MacroOp
macro.Record(gtx.Ops)
defer op.Push(gtx.Ops).Pop()
macro := op.Record(gtx.Ops)
paint.ColorOp{Color: e.HintColor}.Add(gtx.Ops)
tl := widget.Label{Alignment: e.Editor.Alignment}
dims := tl.Layout(gtx, e.shaper, e.Font, e.TextSize, e.Hint)
@@ -62,6 +60,5 @@ func (e EditorStyle) Layout(gtx layout.Context) layout.Dimensions {
}
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
e.Editor.PaintCaret(gtx)
stack.Pop()
return dims
}
+6 -10
View File
@@ -36,8 +36,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
trackOff := float32(thumbSize-trackHeight) * .5
// Draw track.
var stack op.StackOp
stack.Push(gtx.Ops)
stack := op.Push(gtx.Ops)
trackCorner := float32(trackHeight) / 2
trackRect := f32.Rectangle{Max: f32.Point{
X: float32(trackWidth),
@@ -53,7 +52,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
stack.Pop()
// Compute thumb offset and color.
stack.Push(gtx.Ops)
stack = op.Push(gtx.Ops)
col := rgb(0xffffff)
if s.Switch.Value {
off := trackWidth - thumbSize
@@ -63,8 +62,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
// Draw thumb shadow, a translucent disc slightly larger than the
// thumb itself.
var shadowStack op.StackOp
shadowStack.Push(gtx.Ops)
shadowStack := op.Push(gtx.Ops)
shadowSize := float32(2)
// Center shadow horizontally and slightly adjust its Y.
op.TransformOp{}.Offset(f32.Point{X: -shadowSize / 2, Y: -.75}).Add(gtx.Ops)
@@ -76,7 +74,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
stack.Pop()
// Draw thumb ink.
stack.Push(gtx.Ops)
stack = op.Push(gtx.Ops)
inkSize := float32(gtx.Px(unit.Dp(44)))
rr := inkSize * .5
inkOff := f32.Point{
@@ -97,7 +95,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
stack.Pop()
// Set up click area.
stack.Push(gtx.Ops)
stack = op.Push(gtx.Ops)
clickSize := gtx.Px(unit.Dp(40))
clickOff := f32.Point{
X: (float32(trackWidth) - float32(clickSize)) * .5,
@@ -114,8 +112,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
}
func drawDisc(ops *op.Ops, sz float32, col color.RGBA) {
var stack op.StackOp
stack.Push(ops)
defer op.Push(ops).Pop()
rr := sz / 2
r := f32.Rectangle{Max: f32.Point{X: sz, Y: sz}}
clip.Rect{
@@ -124,5 +121,4 @@ func drawDisc(ops *op.Ops, sz float32, col color.RGBA) {
}.Op(ops).Add(ops)
paint.ColorOp{Color: col}.Add(ops)
paint.PaintOp{Rect: r}.Add(ops)
stack.Pop()
}