Commit Graph

7 Commits

Author SHA1 Message Date
Elias Naur 29639565cd ui/layout: replace implicit begin/end scopes with explicit function scopes
Before this change, layout objects followed a pattern where a
begin method would set up the layout and return a tweaked set
of constraints, and a end method would take the widget dimensions
and return the tweaked dimensions.

As has been pointed out, this process is error prone, because the
scope of the layout objects are not clear and because it is easy
to swap two begins or two ends.

It turns out that it is possible to implement layout with function
scopes in garbage free way. A typical layout changes from

        al := layout.Align{Alignment: layout.NE}
	cs = al.Begin(ops, cs)
	in := layout.Inset{Top: ui.Dp(16)}
	cs = in.Begin(c, ops, cs)
	txt := fmt.Sprintf("m: %d %s", mallocs, u.profile.Timings)
	dims := text.Label{Material: theme.text, Face: u.face(fonts.mono, 10), Text: txt}.Layout(ops, cs)
	dims = in.End(dims)
	return al.End(dims)

to

        al := layout.Align{Alignment: layout.NE}
	return al.Layout(ops, cs, func(cs layout.Constraints) layout.Dimensions {
	       in := layout.Inset{Top: ui.Dp(16)}
	       return in.Layout(c, ops, cs, func(cs layout.Constraints) layout.Dimensions {
		       txt := fmt.Sprintf("m: %d %s", mallocs, u.profile.Timings)
		       return text.Label{Material: theme.text, Face: u.face(fonts.mono, 10), Text: txt}.Layout(ops, cs)
	       })
	})

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-09-18 19:31:36 +02:00
Elias Naur 12089ea62a all: rename layout.Dimens to layout.Dimensions
Dimens is only 4 characters shorter and not worth the abbreviation.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-08-30 15:00:17 +02:00
Elias Naur e3ae277841 ui/layout: add List example
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-08-10 18:54:18 +02:00
Elias Naur b3e8f5953e ui/layout: add Stack example
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-08-10 18:46:18 +02:00
Elias Naur 44d16d04e9 ui/layout: add Flex example
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-08-10 18:41:01 +02:00
Elias Naur 94f2752885 ui/layout: add Align example
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-08-10 18:28:19 +02:00
Elias Naur 4ce8f4ea51 ui/layout: add Inset example
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-08-10 18:18:18 +02:00