~eliasnaur/gio#203: 
layout: add Flexes and Rigids

I often run into situations where I only need Rigids or Flexes with same weight in Flex layouts.

Most of the time I need to draw widgets that are located in a slice or a map.

Having to make the slice of FlexChilds is easy but cumbersome, what about having dedicated types in the layout packaged that do it?

Something along the lines of https://git.sr.ht/~pierrec/giox/commit/2c5ee028295c046e95c45a7f776b0fb6177759ad

Status
REPORTED
Submitter
~pierrec
Assigned to
No-one
Submitted
a month ago
Updated
30 days ago
Labels
No labels applied.

~eliasnaur a month ago*

Not sure. Does it really save that much code and come up often enough?

What I've been thinking is that Flex/Stack.Layout taking a slice of children is often too rigid (heh). I sometimes want to specify children on the fly, like your slice or map backed data.

~pierrec a month ago

Yes, I guess the core issue is not being able to specify Flex children in terms of an index. Do we need it in the core library? I would tend to say yes given the number of times I have wished for it. But YMMV.

If so, what about something like this:

type FlexByIndex struct {
...
}

type FlexElement func(layout.Context, int) layout.FlexChild

func (f *FlexByIndex) Layout(gtx layout.Context, num int, children FlexElement) layout.Dimensions {
...
}

~eliasnaur a month ago

I'm thinking more in the direction of adding children incrementally. Flex and Stack used to work like this, but I changed it because you could easily interleave separate Flex or Stack by accident, leading to subtle layout problems.

However, this may not be a problem anymore, given the functional layout.Widget with value layout.Contexts and explicit layout.Dimensions return values.

So:

func (f *Flex) Flex(gtx Context, weight float32, w Widget)
func (f *Flex) Rigid(gtx Context, w Widget)

and then Flex.Layout would append the given list of widgets to widgets already specified with Flex and Rigid. Or you could leave them empty:

var f Flex
f.Flex(gtx, 1, ...)
f.Rigid(gtx, ...)
return f.Layout(gtx)

Perhaps we'd even remove FlexChild altogether if the incremental API turns out good enough.

WDYT?

~pierrec 30 days ago

This looks interesting, I did not know that it was how it used to work. I would make the new methods variadic:

func (f *Flex) Flex(gtx Context, weight float32, w ...Widget)
func (f *Flex) Rigid(gtx Context, w ...Widget)

However, we lose the alloc free property of Flex as it now needs to buffer the children, right?

~eliasnaur 30 days ago

On Sat Mar 13, 2021 at 10:18 CET, ~pierrec wrote:

This looks interesting, I did not know that it was how it used to work. I would make the new methods variadic:

func (f *Flex) Flex(gtx 
Context, weight float32, w ...Widget)
func (f *Flex) Rigid(gtx Context,
 w ...Widget)

Sounds good.

However, we lose the alloc free property of Flex as it now needs to buffer the children, right?

For dynamic Flexes, probably yes, but that's no different from today where you allocate a []layout.FlexChild for your slice or map data backend.

We can preserve the garbage-free property of static layouts by keeping the support for the variadic ...[]FlexChild argument to Flex.Layout.

Elias

Register here or Log in to comment, or comment via email.