layout: delete Init methods from Flex and Stack

With Context containing all the necessary information, separate
Init methods no longer makes much sense. Delete them and thereby
remove a source of runtime panics.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-10-06 09:17:41 +02:00
parent bb796ddf37
commit 8d82a3eaa6
3 changed files with 58 additions and 107 deletions
+28 -61
View File
@@ -20,9 +20,6 @@ type Flex struct {
// Alignment is the alignment in the cross axis. // Alignment is the alignment in the cross axis.
Alignment Alignment Alignment Alignment
ctx *Context
macro op.MacroOp
mode flexMode
size int size int
rigidSize int rigidSize int
// fraction is the rounding error from a Flexible weighting. // fraction is the rounding error from a Flexible weighting.
@@ -60,58 +57,29 @@ const (
SpaceEvenly SpaceEvenly
) )
const (
modeNone flexMode = iota
modeBegun
modeRigid
modeFlex
)
// Init must be called before Rigid or Flexible.
func (f *Flex) Init(gtx *Context) *Flex {
if f.mode > modeBegun {
panic("must End the current child before calling Init again")
}
f.mode = modeBegun
f.ctx = gtx
f.size = 0
f.rigidSize = 0
f.maxCross = 0
f.maxBaseline = 0
return f
}
func (f *Flex) begin(mode flexMode) {
switch {
case f.mode == modeNone:
panic("must Init before adding a child")
case f.mode > modeBegun:
panic("must End before adding a child")
}
f.mode = mode
f.macro.Record(f.ctx.Ops)
}
// Rigid lays out a widget with the main axis constrained to the range // Rigid lays out a widget with the main axis constrained to the range
// from 0 to the remaining space. // from 0 to the remaining space.
func (f *Flex) Rigid(w Widget) FlexChild { func (f *Flex) Rigid(gtx *Context, w Widget) FlexChild {
f.begin(modeRigid) cs := gtx.Constraints
cs := f.ctx.Constraints
mainc := axisMainConstraint(f.Axis, cs) mainc := axisMainConstraint(f.Axis, cs)
mainMax := mainc.Max - f.size mainMax := mainc.Max - f.size
if mainMax < 0 { if mainMax < 0 {
mainMax = 0 mainMax = 0
} }
cs = axisConstraints(f.Axis, Constraint{Max: mainMax}, axisCrossConstraint(f.Axis, cs)) cs = axisConstraints(f.Axis, Constraint{Max: mainMax}, axisCrossConstraint(f.Axis, cs))
dims := f.ctx.Layout(cs, w) var m op.MacroOp
return f.end(dims) m.Record(gtx.Ops)
dims := gtx.Layout(cs, w)
m.Stop()
f.rigidSize += axisMain(f.Axis, dims.Size)
f.expand(dims)
return FlexChild{m, dims}
} }
// Flexible is like Rigid, where the main axis size is also constrained to a // Flexible is like Rigid, where the main axis size is also constrained to a
// fraction of the space not taken up by Rigid children. // fraction of the space not taken up by Rigid children.
func (f *Flex) Flexible(weight float32, w Widget) FlexChild { func (f *Flex) Flexible(gtx *Context, weight float32, w Widget) FlexChild {
f.begin(modeFlex) cs := gtx.Constraints
cs := f.ctx.Constraints
mainc := axisMainConstraint(f.Axis, cs) mainc := axisMainConstraint(f.Axis, cs)
var flexSize int var flexSize int
if mainc.Max > f.size { if mainc.Max > f.size {
@@ -127,36 +95,31 @@ func (f *Flex) Flexible(weight float32, w Widget) FlexChild {
} }
submainc := Constraint{Max: flexSize} submainc := Constraint{Max: flexSize}
cs = axisConstraints(f.Axis, submainc, axisCrossConstraint(f.Axis, cs)) cs = axisConstraints(f.Axis, submainc, axisCrossConstraint(f.Axis, cs))
dims := f.ctx.Layout(cs, w) var m op.MacroOp
return f.end(dims) m.Record(gtx.Ops)
dims := gtx.Layout(cs, w)
m.Stop()
f.expand(dims)
return FlexChild{m, dims}
} }
// End a child by specifying its dimensions. Pass the returned layout result // End a child by specifying its dimensions. Pass the returned layout result
// to Layout. // to Layout.
func (f *Flex) end(dims Dimensions) FlexChild { func (f *Flex) expand(dims Dimensions) {
if f.mode <= modeBegun {
panic("End called without an active child")
}
f.macro.Stop()
sz := axisMain(f.Axis, dims.Size) sz := axisMain(f.Axis, dims.Size)
f.size += sz f.size += sz
if f.mode == modeRigid {
f.rigidSize += sz
}
f.mode = modeBegun
if c := axisCross(f.Axis, dims.Size); c > f.maxCross { if c := axisCross(f.Axis, dims.Size); c > f.maxCross {
f.maxCross = c f.maxCross = c
} }
if b := dims.Baseline; b > f.maxBaseline { if b := dims.Baseline; b > f.maxBaseline {
f.maxBaseline = b f.maxBaseline = b
} }
return FlexChild{f.macro, dims}
} }
// Layout a list of children. The order of the children determines their laid // Layout a list of children. The order of the children determines their laid
// out order. // out order.
func (f *Flex) Layout(children ...FlexChild) { func (f *Flex) Layout(gtx *Context, children ...FlexChild) {
cs := f.ctx.Constraints cs := gtx.Constraints
mainc := axisMainConstraint(f.Axis, cs) mainc := axisMainConstraint(f.Axis, cs)
crossSize := axisCrossConstraint(f.Axis, cs).Constrain(f.maxCross) crossSize := axisCrossConstraint(f.Axis, cs).Constrain(f.maxCross)
var space int var space int
@@ -190,9 +153,9 @@ func (f *Flex) Layout(children ...FlexChild) {
} }
} }
var stack op.StackOp var stack op.StackOp
stack.Push(f.ctx.Ops) stack.Push(gtx.Ops)
op.TransformOp{}.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))).Add(f.ctx.Ops) op.TransformOp{}.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))).Add(gtx.Ops)
child.macro.Add(f.ctx.Ops) child.macro.Add(gtx.Ops)
stack.Pop() stack.Pop()
mainSize += axisMain(f.Axis, dims.Size) mainSize += axisMain(f.Axis, dims.Size)
if i < len(children)-1 { if i < len(children)-1 {
@@ -223,7 +186,11 @@ func (f *Flex) Layout(children ...FlexChild) {
if baseline == 0 { if baseline == 0 {
baseline = sz.Y baseline = sz.Y
} }
f.ctx.Dimensions = Dimensions{Size: sz, Baseline: baseline} gtx.Dimensions = Dimensions{Size: sz, Baseline: baseline}
f.size = 0
f.rigidSize = 0
f.maxCross = 0
f.maxBaseline = 0
} }
func axisPoint(a Axis, main, cross int) image.Point { func axisPoint(a Axis, main, cross int) image.Point {
+6 -8
View File
@@ -63,21 +63,20 @@ func ExampleFlex() {
gtx.Reset(cfg, image.Point{X: 100, Y: 100}) gtx.Reset(cfg, image.Point{X: 100, Y: 100})
flex := layout.Flex{} flex := layout.Flex{}
flex.Init(gtx)
// Rigid 10x10 widget. // Rigid 10x10 widget.
child1 := flex.Rigid(func() { child1 := flex.Rigid(gtx, func() {
fmt.Printf("Rigid: %v\n", gtx.Constraints.Width) fmt.Printf("Rigid: %v\n", gtx.Constraints.Width)
layoutWidget(gtx, 10, 10) layoutWidget(gtx, 10, 10)
}) })
// Child with 50% space allowance. // Child with 50% space allowance.
child2 := flex.Flexible(0.5, func() { child2 := flex.Flexible(gtx, 0.5, func() {
fmt.Printf("50%%: %v\n", gtx.Constraints.Width) fmt.Printf("50%%: %v\n", gtx.Constraints.Width)
layoutWidget(gtx, 10, 10) layoutWidget(gtx, 10, 10)
}) })
flex.Layout(child1, child2) flex.Layout(gtx, child1, child2)
// Output: // Output:
// Rigid: {0 100} // Rigid: {0 100}
@@ -89,20 +88,19 @@ func ExampleStack() {
gtx.Reset(cfg, image.Point{X: 100, Y: 100}) gtx.Reset(cfg, image.Point{X: 100, Y: 100})
stack := layout.Stack{} stack := layout.Stack{}
stack.Init(gtx)
// Rigid 50x50 widget. // Rigid 50x50 widget.
child1 := stack.Rigid(func() { child1 := stack.Rigid(gtx, func() {
layoutWidget(gtx, 50, 50) layoutWidget(gtx, 50, 50)
}) })
// Force widget to the same size as the first. // Force widget to the same size as the first.
child2 := stack.Expand(func() { child2 := stack.Expand(gtx, func() {
fmt.Printf("Expand: %v\n", gtx.Constraints) fmt.Printf("Expand: %v\n", gtx.Constraints)
layoutWidget(gtx, 10, 10) layoutWidget(gtx, 10, 10)
}) })
stack.Layout(child1, child2) stack.Layout(gtx, child1, child2)
// Output: // Output:
// Expand: {{50 50} {50 50}} // Expand: {{50 50} {50 50}}
+24 -38
View File
@@ -15,11 +15,8 @@ type Stack struct {
// smaller than the available space. // smaller than the available space.
Alignment Direction Alignment Direction
macro op.MacroOp maxSZ image.Point
constrained bool baseline int
ctx *Context
maxSZ image.Point
baseline int
} }
// StackChild is the layout result of a call to End. // StackChild is the layout result of a call to End.
@@ -28,45 +25,33 @@ type StackChild struct {
dims Dimensions dims Dimensions
} }
// Init a stack before calling Rigid or Expand.
func (s *Stack) Init(gtx *Context) *Stack {
s.ctx = gtx
s.constrained = true
s.maxSZ = image.Point{}
s.baseline = 0
return s
}
func (s *Stack) begin() {
if !s.constrained {
panic("must Init before adding a child")
}
s.macro.Record(s.ctx.Ops)
}
// Rigid lays out a widget with the same constraints that were // Rigid lays out a widget with the same constraints that were
// passed to Init. // passed to Init.
func (s *Stack) Rigid(w Widget) StackChild { func (s *Stack) Rigid(gtx *Context, w Widget) StackChild {
s.begin() var m op.MacroOp
dims := s.ctx.Layout(s.ctx.Constraints, w) m.Record(gtx.Ops)
return s.end(dims) dims := gtx.Layout(gtx.Constraints, w)
m.Stop()
s.expand(dims)
return StackChild{m, dims}
} }
// Expand lays out a widget with constraints that exactly match // Expand lays out a widget with constraints that exactly match
// the biggest child previously added. // the biggest child previously added.
func (s *Stack) Expand(w Widget) StackChild { func (s *Stack) Expand(gtx *Context, w Widget) StackChild {
s.begin() var m op.MacroOp
m.Record(gtx.Ops)
cs := Constraints{ cs := Constraints{
Width: Constraint{Min: s.maxSZ.X, Max: s.maxSZ.X}, Width: Constraint{Min: s.maxSZ.X, Max: s.maxSZ.X},
Height: Constraint{Min: s.maxSZ.Y, Max: s.maxSZ.Y}, Height: Constraint{Min: s.maxSZ.Y, Max: s.maxSZ.Y},
} }
dims := s.ctx.Layout(cs, w) dims := gtx.Layout(cs, w)
return s.end(dims) m.Stop()
s.expand(dims)
return StackChild{m, dims}
} }
// End a child by specifying its dimensions. func (s *Stack) expand(dims Dimensions) {
func (s *Stack) end(dims Dimensions) StackChild {
s.macro.Stop()
if w := dims.Size.X; w > s.maxSZ.X { if w := dims.Size.X; w > s.maxSZ.X {
s.maxSZ.X = w s.maxSZ.X = w
} }
@@ -78,12 +63,11 @@ func (s *Stack) end(dims Dimensions) StackChild {
s.baseline = b s.baseline = b
} }
} }
return StackChild{s.macro, dims}
} }
// Layout a list of children. The order of the children determines their laid // Layout a list of children. The order of the children determines their laid
// out order. // out order.
func (s *Stack) Layout(children ...StackChild) { func (s *Stack) Layout(gtx *Context, children ...StackChild) {
for _, ch := range children { for _, ch := range children {
sz := ch.dims.Size sz := ch.dims.Size
var p image.Point var p image.Point
@@ -100,17 +84,19 @@ func (s *Stack) Layout(children ...StackChild) {
p.Y = s.maxSZ.Y - sz.Y p.Y = s.maxSZ.Y - sz.Y
} }
var stack op.StackOp var stack op.StackOp
stack.Push(s.ctx.Ops) stack.Push(gtx.Ops)
op.TransformOp{}.Offset(toPointF(p)).Add(s.ctx.Ops) op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops)
ch.macro.Add(s.ctx.Ops) ch.macro.Add(gtx.Ops)
stack.Pop() stack.Pop()
} }
b := s.baseline b := s.baseline
if b == 0 { if b == 0 {
b = s.maxSZ.Y b = s.maxSZ.Y
} }
s.ctx.Dimensions = Dimensions{ gtx.Dimensions = Dimensions{
Size: s.maxSZ, Size: s.maxSZ,
Baseline: b, Baseline: b,
} }
s.maxSZ = image.Point{}
s.baseline = 0
} }