layout: change Widget to take explicit Context and return explicit Dimensions

Change the definition of Widget from the implicit

        type Widget func()

to the explicit functional

        type Widget func(gtx layout.Context) layout.Dimensions

The advantages are numerous:

- Clearer connection between the incoming context and the output dimensions.
- Returning the Dimensions are impossible to omit.
- Contexts passed by value, so its fields can be exported
and freely mutated by the program.

The only disadvantage is the longer function literals and the many "returns".
What tipped the scales in favour of the explicit Widget variant is that type
aliases can dramatically shorten the literals:

	type (
		C = layout.Context
		D = layout.Dimensions
	)

	widget := func(gtx C) D {
		...
	}

Note that the aliases are not part of the Gio API and it is up to each user
whether they want to use them.

Finally the Go proposal for lightweight function literals,
https://github.com/golang/go/issues/21498, may remove the disadvantage
completely in future.

Context becomes a plain struct with only public fields, and its Reset is
replaced by a NewContext convenience constructor.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-23 19:55:02 +02:00
parent af10307f4a
commit 3af01a3f43
25 changed files with 226 additions and 208 deletions
+14 -9
View File
@@ -5,22 +5,27 @@ package layout
import (
"image"
"testing"
"gioui.org/op"
)
func TestStack(t *testing.T) {
var gtx Context
gtx.Reset(nil, nil, image.Point{X: 100, Y: 100})
gtx.Constraints.Min = image.Point{}
gtx := Context{
Ops: new(op.Ops),
Constraints: Constraints{
Max: image.Pt(100, 100),
},
}
exp := image.Point{X: 60, Y: 70}
Stack{Alignment: Center}.Layout(&gtx,
Expanded(func() {
gtx.Dimensions.Size = exp
dims := Stack{Alignment: Center}.Layout(gtx,
Expanded(func(gtx Context) Dimensions {
return Dimensions{Size: exp}
}),
Stacked(func() {
gtx.Dimensions.Size = image.Point{X: 50, Y: 50}
Stacked(func(gtx Context) Dimensions {
return Dimensions{Size: image.Point{X: 50, Y: 50}}
}),
)
if got := gtx.Dimensions.Size; got != exp {
if got := dims.Size; got != exp {
t.Errorf("Stack ignored Expanded size, got %v expected %v", got, exp)
}
}