From 52ccc183b54c2a3ccc04fe69cfa08eebaab662a1 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 25 Mar 2020 17:52:02 +0100 Subject: [PATCH] widget/material: propagate ButtonLayout minimum constraints to content The previous change fixed a regression where minimum constraints larger than 0 would not affect the button. This change moves the minimum constraints one level lower so the content widget will see them as well. The wrapping layout.Center ensures that any misbehaving widgets still end up centered. Add a test to lock in the new behaviour and the previous fix. Signed-off-by: Elias Naur --- widget/material/button.go | 4 ++-- widget/material/material_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 widget/material/material_test.go diff --git a/widget/material/button.go b/widget/material/button.go index 09053c72..28c4bc43 100644 --- a/widget/material/button.go +++ b/widget/material/button.go @@ -109,9 +109,9 @@ func (b ButtonLayout) Layout(gtx *layout.Context, button *widget.Button, w layou } }), layout.Stacked(func() { - gtx.Constraints.Width.Min = hmin - gtx.Constraints.Height.Min = vmin layout.Center.Layout(gtx, func() { + gtx.Constraints.Width.Min = hmin + gtx.Constraints.Height.Min = vmin b.Inset.Layout(gtx, func() { paint.ColorOp{Color: b.Color}.Add(gtx.Ops) w() diff --git a/widget/material/material_test.go b/widget/material/material_test.go new file mode 100644 index 00000000..cd084326 --- /dev/null +++ b/widget/material/material_test.go @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: Unlicense OR MIT + +package material + +import ( + "image" + "testing" + + "gioui.org/layout" + "gioui.org/widget" +) + +func TestButtonLayout(t *testing.T) { + var gtx layout.Context + gtx.Reset(nil, image.Point{X: 100, Y: 100}) + + ButtonLayout{}.Layout(>x, new(widget.Button), func() { + if got, exp := gtx.Constraints.Width.Min, 100; got != exp { + t.Errorf("minimum width is %d, expected %d", got, exp) + } + if got, exp := gtx.Constraints.Height.Min, 100; got != exp { + t.Errorf("minimum width is %d, expected %d", got, exp) + } + }) +}