From 64bcb1ccd51e790bcea194fb76a1646c2249624a Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Sun, 19 Sep 2021 08:36:10 +0200 Subject: [PATCH] layout: do not reset cross axis constraints in Direction for N, S, E and W Fixes #268 Signed-off-by: Pierre Curto --- layout/layout.go | 19 +++++++++++++------ layout/layout_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/layout/layout.go b/layout/layout.go index 0619b914..f7d6def1 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -162,16 +162,23 @@ func UniformInset(v unit.Value) Inset { // The widget is called with the context constraints minimum cleared. func (d Direction) Layout(gtx Context, w Widget) Dimensions { macro := op.Record(gtx.Ops) - cs := gtx.Constraints - gtx.Constraints.Min = image.Point{} + csn := gtx.Constraints.Min + switch d { + case N, S: + gtx.Constraints.Min.Y = 0 + case E, W: + gtx.Constraints.Min.X = 0 + default: + gtx.Constraints.Min = image.Point{} + } dims := w(gtx) call := macro.Stop() sz := dims.Size - if sz.X < cs.Min.X { - sz.X = cs.Min.X + if sz.X < csn.X { + sz.X = csn.X } - if sz.Y < cs.Min.Y { - sz.Y = cs.Min.Y + if sz.Y < csn.Y { + sz.Y = csn.Y } defer op.Save(gtx.Ops).Load() diff --git a/layout/layout_test.go b/layout/layout_test.go index f46fbfcd..8a44d0e9 100644 --- a/layout/layout_test.go +++ b/layout/layout_test.go @@ -29,3 +29,36 @@ func TestStack(t *testing.T) { t.Errorf("Stack ignored Expanded size, got %v expected %v", got, exp) } } + +func TestDirection(t *testing.T) { + max := image.Pt(100, 100) + for _, tc := range []struct { + dir Direction + exp image.Point + }{ + {N, image.Pt(max.X, 0)}, + {S, image.Pt(max.X, 0)}, + {E, image.Pt(0, max.Y)}, + {W, image.Pt(0, max.Y)}, + {NW, image.Pt(0, 0)}, + {NE, image.Pt(0, 0)}, + {SE, image.Pt(0, 0)}, + {SW, image.Pt(0, 0)}, + {Center, image.Pt(0, 0)}, + } { + t.Run(tc.dir.String(), func(t *testing.T) { + gtx := Context{ + Ops: new(op.Ops), + Constraints: Exact(max), + } + var min image.Point + tc.dir.Layout(gtx, func(gtx Context) Dimensions { + min = gtx.Constraints.Min + return Dimensions{} + }) + if got, exp := min, tc.exp; got != exp { + t.Errorf("got %v; expected %v", got, exp) + } + }) + } +}