From e0313081720e4a6fa8ec8c4252876648de314927 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 2 Oct 2019 15:31:52 +0200 Subject: [PATCH] layout: take a size, not constraints in Context.Reset Taking a constraint in Reset smells too much of a layout operation, whereas a size is simpler and only serves to set the context constraints to something sensible. Signed-off-by: Elias Naur --- layout/layout.go | 7 ++++--- layout/layout_test.go | 19 +++++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/layout/layout.go b/layout/layout.go index 4f890bb8..3ace2667 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -102,9 +102,10 @@ func (s *Context) Layout(cs Constraints, w Widget) Dimensions { return s.Dimensions } -// Reset the context. -func (c *Context) Reset(cfg Config, cs Constraints) { - c.Constraints = cs +// Reset the context. The constraints' minimum and maximum values are +// set to the size. +func (c *Context) Reset(cfg Config, size image.Point) { + c.Constraints = RigidConstraints(size) c.Dimensions = Dimensions{} c.Config = cfg if c.Ops == nil { diff --git a/layout/layout_test.go b/layout/layout_test.go index 0524e0d2..cc78c9bd 100644 --- a/layout/layout_test.go +++ b/layout/layout_test.go @@ -19,11 +19,10 @@ var cfg = new(config) func ExampleInset() { gtx := &layout.Context{Queue: q} + gtx.Reset(cfg, image.Point{X: 100, Y: 100}) // Loose constraints with no minimal size. - var cs layout.Constraints - cs.Width.Max = 100 - cs.Height.Max = 100 - gtx.Reset(cfg, cs) + gtx.Constraints.Width.Min = 0 + gtx.Constraints.Height.Min = 0 // Inset all edges by 10. inset := layout.UniformInset(unit.Dp(10)) @@ -43,8 +42,7 @@ func ExampleInset() { func ExampleAlign() { gtx := &layout.Context{Queue: q} // Rigid constraints with both minimum and maximum set. - cs := layout.RigidConstraints(image.Point{X: 100, Y: 100}) - gtx.Reset(cfg, cs) + gtx.Reset(cfg, image.Point{X: 100, Y: 100}) align := layout.Align(layout.Center) align.Layout(gtx, func() { @@ -62,8 +60,7 @@ func ExampleAlign() { func ExampleFlex() { gtx := &layout.Context{Queue: q} - cs := layout.RigidConstraints(image.Point{X: 100, Y: 100}) - gtx.Reset(cfg, cs) + gtx.Reset(cfg, image.Point{X: 100, Y: 100}) flex := layout.Flex{} flex.Init(gtx) @@ -89,8 +86,7 @@ func ExampleFlex() { func ExampleStack() { gtx := &layout.Context{Queue: q} - cs := layout.RigidConstraints(image.Point{X: 100, Y: 100}) - gtx.Reset(cfg, cs) + gtx.Reset(cfg, image.Point{X: 100, Y: 100}) stack := layout.Stack{} stack.Init(gtx) @@ -114,8 +110,7 @@ func ExampleStack() { func ExampleList() { gtx := &layout.Context{Queue: q} - cs := layout.RigidConstraints(image.Point{X: 100, Y: 100}) - gtx.Reset(cfg, cs) + gtx.Reset(cfg, image.Point{X: 100, Y: 100}) // The list is 1e6 elements, but only 5 fit the constraints. const listLen = 1e6