From 2d75181b516ac746224ebde16bb6e325394ae849 Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Fri, 3 Dec 2021 20:40:35 +0100 Subject: [PATCH] layout: fix dimensions of empty list When the Min constraints are set but the list has no item to display, use those as the list returned dimensions. Signed-off-by: Pierre Curto --- layout/list.go | 5 +++++ layout/list_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/layout/list.go b/layout/list.go index ea68c57c..aa78f1b5 100644 --- a/layout/list.go +++ b/layout/list.go @@ -293,6 +293,11 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions { if pos > mainMax { pos = mainMax } + if crossMin, crossMax := l.Axis.crossConstraint(l.cs); maxCross < crossMin { + maxCross = crossMin + } else if maxCross > crossMax { + maxCross = crossMax + } dims := l.Axis.Convert(image.Pt(pos, maxCross)) call := macro.Stop() defer clip.Rect(image.Rectangle{Max: dims}).Push(ops).Pop() diff --git a/layout/list_test.go b/layout/list_test.go index f9d0a0a7..eae2ec9f 100644 --- a/layout/list_test.go +++ b/layout/list_test.go @@ -13,6 +13,18 @@ import ( "gioui.org/op" ) +func TestEmptyList(t *testing.T) { + var l List + gtx := Context{ + Ops: new(op.Ops), + Constraints: Exact(image.Pt(20, 10)), + } + dims := l.Layout(gtx, 0, nil) + if got, want := dims.Size, gtx.Constraints.Min; got != want { + t.Errorf("got %v; want %v", got, want) + } +} + func TestListPosition(t *testing.T) { _s := func(e ...event.Event) []event.Event { return e } r := new(router.Router)