ui/layout: introduce Context

Context keeps the current Constraints and Dimensions so the layout
function scopes don't have to.

With

	ctx := new(layout.Context)

a label with margins and alignment goes from

	return al.Layout(ops, cs, func(cs layout.Constraints) layout.Dimensions {
		in := layout.Inset{...}
		return in.Layout(c, ops, cs, func(cs layout.Constraints) layout.Dimensions {
			return text.Label{...}.Layout(ops, cs)
		})
	})

to

	al.Layout(ops, ctx, func() {
		in := layout.Inset{...}
		in.Layout(c, ops, ctx, func() {
		       text.Label{...}.Layout(ops, ctx)
		})
	})

It was a difficult trade-off between the verbose functional approach
and the shorter but more complex Context.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-09-24 19:06:07 +02:00
parent 64add13d28
commit ce9bcee62b
9 changed files with 126 additions and 89 deletions
+36 -34
View File
@@ -19,22 +19,21 @@ var cfg = new(config)
func ExampleInset() {
ops := new(ui.Ops)
ctx := new(layout.Context)
// Loose constraints with no minimal size.
var cs layout.Constraints
cs.Width.Max = 100
cs.Height.Max = 100
ctx.Constraints.Width.Max = 100
ctx.Constraints.Height.Max = 100
// Inset all edges by 10.
inset := layout.UniformInset(ui.Dp(10))
dims := inset.Layout(cfg, ops, cs, func(cs layout.Constraints) layout.Dimensions {
inset.Layout(cfg, ops, ctx, func() {
// Lay out a 50x50 sized widget.
dims := layoutWidget(50, 50, cs)
fmt.Println(dims.Size)
return dims
layoutWidget(ctx, 50, 50)
fmt.Println(ctx.Dimensions.Size)
})
fmt.Println(dims.Size)
fmt.Println(ctx.Dimensions.Size)
// Output:
// (50,50)
@@ -43,19 +42,19 @@ func ExampleInset() {
func ExampleAlign() {
ops := new(ui.Ops)
ctx := new(layout.Context)
// Rigid constraints with both minimum and maximum set.
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
align := layout.Align{Alignment: layout.Center}
dims := align.Layout(ops, cs, func(cs layout.Constraints) layout.Dimensions {
align.Layout(ops, ctx, func() {
// Lay out a 50x50 sized widget.
dims := layoutWidget(50, 50, cs)
fmt.Println(dims.Size)
return dims
layoutWidget(ctx, 50, 50)
fmt.Println(ctx.Dimensions.Size)
})
fmt.Println(dims.Size)
fmt.Println(ctx.Dimensions.Size)
// Output:
// (50,50)
@@ -64,22 +63,23 @@ func ExampleAlign() {
func ExampleFlex() {
ops := new(ui.Ops)
ctx := new(layout.Context)
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
flex := layout.Flex{}
flex.Init(ops, cs)
flex.Init(ops, ctx)
// Rigid 10x10 widget.
child1 := flex.Rigid(func(cs layout.Constraints) layout.Dimensions {
fmt.Printf("Rigid: %v\n", cs.Width)
return layoutWidget(10, 10, cs)
child1 := flex.Rigid(func() {
fmt.Printf("Rigid: %v\n", ctx.Constraints.Width)
layoutWidget(ctx, 10, 10)
})
// Child with 50% space allowance.
child2 := flex.Flexible(0.5, func(cs layout.Constraints) layout.Dimensions {
fmt.Printf("50%%: %v\n", cs.Width)
return layoutWidget(10, 10, cs)
child2 := flex.Flexible(0.5, func() {
fmt.Printf("50%%: %v\n", ctx.Constraints.Width)
layoutWidget(ctx, 10, 10)
})
flex.Layout(child1, child2)
@@ -91,21 +91,22 @@ func ExampleFlex() {
func ExampleStack() {
ops := new(ui.Ops)
ctx := new(layout.Context)
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
stack := layout.Stack{}
stack.Init(ops, cs)
stack.Init(ops, ctx)
// Rigid 50x50 widget.
child1 := stack.Rigid(func(cs layout.Constraints) layout.Dimensions {
return layoutWidget(50, 50, cs)
child1 := stack.Rigid(func() {
layoutWidget(ctx, 50, 50)
})
// Force widget to the same size as the first.
child2 := stack.Expand(func(cs layout.Constraints) layout.Dimensions {
fmt.Printf("Expand: %v\n", cs)
return layoutWidget(10, 10, cs)
child2 := stack.Expand(func() {
fmt.Printf("Expand: %v\n", ctx.Constraints)
layoutWidget(ctx, 10, 10)
})
stack.Layout(child1, child2)
@@ -116,17 +117,18 @@ func ExampleStack() {
func ExampleList() {
ops := new(ui.Ops)
ctx := new(layout.Context)
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
// The list is 1e6 elements, but only 5 fit the constraints.
const listLen = 1e6
var list layout.List
count := 0
list.Layout(cfg, q, ops, cs, listLen, func(cs layout.Constraints, i int) layout.Dimensions {
list.Layout(cfg, q, ops, ctx, listLen, func(i int) {
count++
return layoutWidget(20, 20, cs)
layoutWidget(ctx, 20, 20)
})
fmt.Println(count)
@@ -135,8 +137,8 @@ func ExampleList() {
// 5
}
func layoutWidget(width, height int, cs layout.Constraints) layout.Dimensions {
return layout.Dimensions{
func layoutWidget(ctx *layout.Context, width, height int) {
ctx.Dimensions = layout.Dimensions{
Size: image.Point{
X: width,
Y: height,