ui/layout: add sanity checks to Insets/Align Begin/End

Would have caught the invalid layout re-use fixed by a recent commit.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-17 17:35:54 +02:00
parent d67b7eca6c
commit c511858d30
+23 -4
View File
@@ -70,11 +70,16 @@ func ExactConstraints(size image.Point) Constraints {
type Insets struct { type Insets struct {
Top, Right, Bottom, Left float32 Top, Right, Bottom, Left float32
ops *ui.Ops ops *ui.Ops
cs Constraints begun bool
cs Constraints
} }
func (in *Insets) Begin(ops *ui.Ops, cs Constraints) Constraints { func (in *Insets) Begin(ops *ui.Ops, cs Constraints) Constraints {
if in.begun {
panic("must End before Begin")
}
in.begun = true
in.ops = ops in.ops = ops
in.cs = cs in.cs = cs
mcs := cs mcs := cs
@@ -105,6 +110,10 @@ func (in *Insets) Begin(ops *ui.Ops, cs Constraints) Constraints {
} }
func (in *Insets) End(dims Dimens) Dimens { func (in *Insets) End(dims Dimens) Dimens {
if !in.begun {
panic("must Begin before End")
}
in.begun = false
ops := in.ops ops := in.ops
ui.OpPop{}.Add(ops) ui.OpPop{}.Add(ops)
t, r, b, l := int(math.Round(float64(in.Top))), int(math.Round(float64(in.Right))), int(math.Round(float64(in.Bottom))), int(math.Round(float64(in.Left))) t, r, b, l := int(math.Round(float64(in.Top))), int(math.Round(float64(in.Right))), int(math.Round(float64(in.Bottom))), int(math.Round(float64(in.Left)))
@@ -147,12 +156,18 @@ func (s Sized) Constrain(cs Constraints) Constraints {
} }
type Align struct { type Align struct {
ops *ui.Ops
Alignment Direction Alignment Direction
cs Constraints
ops *ui.Ops
begun bool
cs Constraints
} }
func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints { func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints {
if a.begun {
panic("must End before Begin")
}
a.begun = true
a.ops = ops a.ops = ops
a.cs = cs a.cs = cs
ops.Begin() ops.Begin()
@@ -160,6 +175,10 @@ func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints {
} }
func (a *Align) End(dims Dimens) Dimens { func (a *Align) End(dims Dimens) Dimens {
if !a.begun {
panic("must Begin before End")
}
a.begun = false
ops := a.ops ops := a.ops
block := ops.End() block := ops.End()
sz := dims.Size sz := dims.Size