mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
ui/layout: add common state to Context
Almost every layout and widget need the ui.Config for its environment, an ui.Ops to store operations. Stateful widgets need an input.Queue for events. Add all these common objects to Context, greatly simplifying the function signatures for Gio programs. Fixes gio#33 Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+36
-40
@@ -18,22 +18,22 @@ var q queue
|
||||
var cfg = new(config)
|
||||
|
||||
func ExampleInset() {
|
||||
ops := new(ui.Ops)
|
||||
ctx := new(layout.Context)
|
||||
|
||||
c := &layout.Context{Queue: q}
|
||||
// Loose constraints with no minimal size.
|
||||
ctx.Constraints.Width.Max = 100
|
||||
ctx.Constraints.Height.Max = 100
|
||||
var cs layout.Constraints
|
||||
cs.Width.Max = 100
|
||||
cs.Height.Max = 100
|
||||
c.Reset(cfg, cs)
|
||||
|
||||
// Inset all edges by 10.
|
||||
inset := layout.UniformInset(ui.Dp(10))
|
||||
inset.Layout(cfg, ops, ctx, func() {
|
||||
inset.Layout(c, func() {
|
||||
// Lay out a 50x50 sized widget.
|
||||
layoutWidget(ctx, 50, 50)
|
||||
fmt.Println(ctx.Dimensions.Size)
|
||||
layoutWidget(c, 50, 50)
|
||||
fmt.Println(c.Dimensions.Size)
|
||||
})
|
||||
|
||||
fmt.Println(ctx.Dimensions.Size)
|
||||
fmt.Println(c.Dimensions.Size)
|
||||
|
||||
// Output:
|
||||
// (50,50)
|
||||
@@ -41,20 +41,19 @@ func ExampleInset() {
|
||||
}
|
||||
|
||||
func ExampleAlign() {
|
||||
ops := new(ui.Ops)
|
||||
ctx := new(layout.Context)
|
||||
|
||||
c := &layout.Context{Queue: q}
|
||||
// Rigid constraints with both minimum and maximum set.
|
||||
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c.Reset(cfg, cs)
|
||||
|
||||
align := layout.Align(layout.Center)
|
||||
align.Layout(ops, ctx, func() {
|
||||
align.Layout(c, func() {
|
||||
// Lay out a 50x50 sized widget.
|
||||
layoutWidget(ctx, 50, 50)
|
||||
fmt.Println(ctx.Dimensions.Size)
|
||||
layoutWidget(c, 50, 50)
|
||||
fmt.Println(c.Dimensions.Size)
|
||||
})
|
||||
|
||||
fmt.Println(ctx.Dimensions.Size)
|
||||
fmt.Println(c.Dimensions.Size)
|
||||
|
||||
// Output:
|
||||
// (50,50)
|
||||
@@ -62,24 +61,23 @@ func ExampleAlign() {
|
||||
}
|
||||
|
||||
func ExampleFlex() {
|
||||
ops := new(ui.Ops)
|
||||
ctx := new(layout.Context)
|
||||
|
||||
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c := &layout.Context{Queue: q}
|
||||
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c.Reset(cfg, cs)
|
||||
|
||||
flex := layout.Flex{}
|
||||
flex.Init(ops, ctx)
|
||||
flex.Init(c)
|
||||
|
||||
// Rigid 10x10 widget.
|
||||
child1 := flex.Rigid(func() {
|
||||
fmt.Printf("Rigid: %v\n", ctx.Constraints.Width)
|
||||
layoutWidget(ctx, 10, 10)
|
||||
fmt.Printf("Rigid: %v\n", c.Constraints.Width)
|
||||
layoutWidget(c, 10, 10)
|
||||
})
|
||||
|
||||
// Child with 50% space allowance.
|
||||
child2 := flex.Flexible(0.5, func() {
|
||||
fmt.Printf("50%%: %v\n", ctx.Constraints.Width)
|
||||
layoutWidget(ctx, 10, 10)
|
||||
fmt.Printf("50%%: %v\n", c.Constraints.Width)
|
||||
layoutWidget(c, 10, 10)
|
||||
})
|
||||
|
||||
flex.Layout(child1, child2)
|
||||
@@ -90,23 +88,22 @@ func ExampleFlex() {
|
||||
}
|
||||
|
||||
func ExampleStack() {
|
||||
ops := new(ui.Ops)
|
||||
ctx := new(layout.Context)
|
||||
|
||||
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c := &layout.Context{Queue: q}
|
||||
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c.Reset(cfg, cs)
|
||||
|
||||
stack := layout.Stack{}
|
||||
stack.Init(ops, ctx)
|
||||
stack.Init(c)
|
||||
|
||||
// Rigid 50x50 widget.
|
||||
child1 := stack.Rigid(func() {
|
||||
layoutWidget(ctx, 50, 50)
|
||||
layoutWidget(c, 50, 50)
|
||||
})
|
||||
|
||||
// Force widget to the same size as the first.
|
||||
child2 := stack.Expand(func() {
|
||||
fmt.Printf("Expand: %v\n", ctx.Constraints)
|
||||
layoutWidget(ctx, 10, 10)
|
||||
fmt.Printf("Expand: %v\n", c.Constraints)
|
||||
layoutWidget(c, 10, 10)
|
||||
})
|
||||
|
||||
stack.Layout(child1, child2)
|
||||
@@ -116,19 +113,18 @@ func ExampleStack() {
|
||||
}
|
||||
|
||||
func ExampleList() {
|
||||
ops := new(ui.Ops)
|
||||
ctx := new(layout.Context)
|
||||
|
||||
ctx.Constraints = layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c := &layout.Context{Queue: q}
|
||||
cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
|
||||
c.Reset(cfg, cs)
|
||||
|
||||
// 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, ctx, listLen, func(i int) {
|
||||
list.Layout(c, listLen, func(i int) {
|
||||
count++
|
||||
layoutWidget(ctx, 20, 20)
|
||||
layoutWidget(c, 20, 20)
|
||||
})
|
||||
|
||||
fmt.Println(count)
|
||||
|
||||
Reference in New Issue
Block a user