~eliasnaur/gio-experiments

2ff064cc0cd9dc9835219a978fd7b2d661793786 — Elias Naur 1 year, 3 months ago 7ce24a7
layout: bump gio version

Signed-off-by: Elias Naur <mail@eliasnaur.com>
3 files changed, 76 insertions(+), 101 deletions(-)

M go.mod
M go.sum
M layout/format.go
M go.mod => go.mod +1 -1
@@ 2,4 2,4 @@ module eliasnaur.com/giox

go 1.14

require gioui.org v0.0.0-20200113204813-a7dc7c01c0f5
require gioui.org v0.0.0-20200610092923-bb8bb504d829

M go.sum => go.sum +3 -3
@@ 1,6 1,6 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20200113204813-a7dc7c01c0f5 h1:1CDWs+aNGYck65Hcqmp4ySl5zD8UrjRq3nqD07j5hZc=
gioui.org v0.0.0-20200113204813-a7dc7c01c0f5/go.mod h1:KqFFi2Dq5gYA3FJ0sDOt8OBXoMsuxMtE8v2f0JExXAY=
gioui.org v0.0.0-20200610092923-bb8bb504d829 h1:PXjoSNRvuLtq1svw6Kk0UnurdOdoIeZx0/MvrLD2BOI=
gioui.org v0.0.0-20200610092923-bb8bb504d829/go.mod h1:AHI9rFr6AEEHCb8EPVtb/p5M+NMJRKH58IOp8O3Je04=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=


@@ 17,7 17,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

M layout/format.go => layout/format.go +72 -97
@@ 4,6 4,7 @@ package layout

import (
	"fmt"
	"image"
	"strconv"

	"gioui.org/layout"


@@ 67,9 68,9 @@ type formatError string
// on the form e(widget).
// If alignment is specified it must be one of the directions listed
// above.
func Format(gtx *layout.Context, format string, widgets ...layout.Widget) {
func Format(gtx layout.Context, format string, widgets ...layout.Widget) layout.Dimensions {
	if format == "" {
		return
		return layout.Dimensions{}
	}
	f := formatter{
		orig: format,


@@ 85,115 86,94 @@ func Format(gtx *layout.Context, format string, widgets ...layout.Widget) {
			panic(fmt.Errorf("Format: %s:%d: %s", msg, pos, err))
		}
	}()
	formatExpr(gtx, &f, widgets)
	return formatExpr(gtx, &f, widgets)
}

func formatExpr(gtx *layout.Context, f *formatter, widgets []layout.Widget) {
func formatExpr(gtx layout.Context, f *formatter, widgets []layout.Widget) layout.Dimensions {
	switch peek(f) {
	case '_':
		formatWidget(gtx, f, widgets)
		return formatWidget(gtx, f, widgets)
	default:
		formatLayout(gtx, f, widgets)
		return formatLayout(gtx, f, widgets)
	}
}

func formatLayout(gtx *layout.Context, f *formatter, widgets []layout.Widget) {
func formatLayout(gtx layout.Context, f *formatter, widgets []layout.Widget) layout.Dimensions {
	name := parseName(f)
	if name == "" {
		errorf("missing layout name")
	}
	expect(f, "(")
	fexpr := func() {
		formatExpr(gtx, f, widgets)
	fexpr := func(gtx layout.Context) layout.Dimensions {
		return formatExpr(gtx, f, widgets)
	}
	align, ok := dirFor(name)
	var dims layout.Dimensions
	if ok {
		layout.Align(align).Layout(gtx, fexpr)
		dims = align.Layout(gtx, fexpr)
		expect(f, ")")
		return
		return dims
	}
	switch name {
	case "inset":
		in := parseInset(gtx, f, widgets)
		in.Layout(gtx, fexpr)
		dims = in.Layout(gtx, fexpr)
	case "hflex":
		formatFlex(gtx, layout.Horizontal, f, widgets)
		dims = formatFlex(gtx, layout.Horizontal, f, widgets)
	case "vflex":
		formatFlex(gtx, layout.Vertical, f, widgets)
		dims = formatFlex(gtx, layout.Vertical, f, widgets)
	case "stack":
		formatStack(gtx, f, widgets)
		dims = formatStack(gtx, f, widgets)
	case "hmax":
		cs := gtx.Constraints
		cs.Width.Min = cs.Width.Max
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		gtx.Constraints.Min.X = gtx.Constraints.Max.X
		dims = formatExpr(gtx, f, widgets)
	case "vmax":
		cs := gtx.Constraints
		cs.Height.Min = cs.Height.Max
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
		dims = formatExpr(gtx, f, widgets)
	case "max":
		cs := gtx.Constraints
		cs.Width.Min = cs.Width.Max
		cs.Height.Min = cs.Height.Max
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		gtx.Constraints.Min = gtx.Constraints.Max
		dims = formatExpr(gtx, f, widgets)
	case "hmin":
		cs := gtx.Constraints
		cs.Width.Max = cs.Width.Min
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		gtx.Constraints.Max.X = gtx.Constraints.Min.X
		dims = formatExpr(gtx, f, widgets)
	case "vmin":
		cs := gtx.Constraints
		cs.Height.Max = cs.Height.Min
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		gtx.Constraints.Max.Y = gtx.Constraints.Min.Y
		dims = formatExpr(gtx, f, widgets)
	case "min":
		cs := gtx.Constraints
		cs.Width.Max = cs.Width.Min
		cs.Height.Max = cs.Height.Min
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		gtx.Constraints.Max = gtx.Constraints.Min
		dims = formatExpr(gtx, f, widgets)
	case "hcap":
		w := parseValue(f)
		w := gtx.Px(parseValue(f))
		expect(f, ",")
		cs := gtx.Constraints
		cs.Width.Max = cs.Width.Constrain(gtx.Px(w))
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		cs.Max = cs.Constrain(image.Pt(w, cs.Max.X))
		dims = formatExpr(gtx, f, widgets)
	case "vcap":
		h := parseValue(f)
		h := gtx.Px(parseValue(f))
		expect(f, ",")
		cs := gtx.Constraints
		cs.Height.Max = cs.Height.Constrain(gtx.Px(h))
		ctxLayout(gtx, cs, func() {
			formatExpr(gtx, f, widgets)
		})
		cs.Max = cs.Constrain(image.Pt(cs.Max.X, h))
		dims = formatExpr(gtx, f, widgets)
	default:
		errorf("invalid layout %q", name)
	}
	expect(f, ")")
	return dims
}

func formatWidget(gtx *layout.Context, f *formatter, widgets []layout.Widget) {
func formatWidget(gtx layout.Context, f *formatter, widgets []layout.Widget) layout.Dimensions {
	expect(f, "_")
	if i, max := f.current, len(widgets)-1; i > max {
		errorf("widget index %d out of bounds [0;%d]", i, max)
	}
	if f.skip == 0 {
		widgets[f.current]()
		return widgets[f.current](gtx)
	}
	f.current++
	return layout.Dimensions{}
}

func formatStack(gtx *layout.Context, f *formatter, widgets []layout.Widget) {
func formatStack(gtx layout.Context, f *formatter, widgets []layout.Widget) layout.Dimensions {
	st := layout.Stack{}
	backup := *f
	// Parse alignment, if present.


@@ 212,38 192,40 @@ loop:
		case ')':
			break loop
		case 'r':
			w := func() {
			w := func(gtx layout.Context) layout.Dimensions {
				expect(f, "r(")
				formatExpr(gtx, f, widgets)
				dims := formatExpr(gtx, f, widgets)
				expect(f, ")")
				if peek(f) == ',' {
					expect(f, ",")
				}
				return dims
			}
			backup := *f
			f.skip++
			w()
			w(gtx)
			f.skip--
			children = append(children, layout.Stacked(func() {
			children = append(children, layout.Stacked(func(gtx layout.Context) layout.Dimensions {
				*f = backup
				w()
				return w(gtx)
			}))
		case 'e':
			w := func() {
			w := func(gtx layout.Context) layout.Dimensions {
				expect(f, "e(")
				formatExpr(gtx, f, widgets)
				dims := formatExpr(gtx, f, widgets)
				expect(f, ")")
				if peek(f) == ',' {
					expect(f, ",")
				}
				return dims
			}
			backup := *f
			f.skip++
			w()
			w(gtx)
			f.skip--
			children = append(children, layout.Expanded(func() {
			children = append(children, layout.Expanded(func(gtx layout.Context) layout.Dimensions {
				*f = backup
				w()
				return w(gtx)
			}))
		default:
			errorf("invalid stack child")


@@ 251,12 233,14 @@ loop:
	}
	if f.skip == 0 {
		backup := *f
		st.Layout(gtx, children...)
		dims := st.Layout(gtx, children...)
		*f = backup
		return dims
	}
	return layout.Dimensions{}
}

func formatFlex(gtx *layout.Context, axis layout.Axis, f *formatter, widgets []layout.Widget) {
func formatFlex(gtx layout.Context, axis layout.Axis, f *formatter, widgets []layout.Widget) layout.Dimensions {
	fl := layout.Flex{Axis: axis}
	backup := *f
	// Parse alignment, if present.


@@ 275,41 259,43 @@ loop:
		case ')':
			break loop
		case 'r':
			w := func() {
			w := func(gtx layout.Context) layout.Dimensions {
				expect(f, "r(")
				formatExpr(gtx, f, widgets)
				dims := formatExpr(gtx, f, widgets)
				expect(f, ")")
				if peek(f) == ',' {
					expect(f, ",")
				}
				return dims
			}
			backup := *f
			f.skip++
			w()
			w(gtx)
			f.skip--
			children = append(children, layout.Rigid(func() {
			children = append(children, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
				*f = backup
				w()
				return w(gtx)
			}))
		case 'f':
			var weight float32
			w := func() {
			w := func(gtx layout.Context) layout.Dimensions {
				expect(f, "f(")
				weight = parseFloat(f)
				expect(f, ",")
				formatExpr(gtx, f, widgets)
				dims := formatExpr(gtx, f, widgets)
				expect(f, ")")
				if peek(f) == ',' {
					expect(f, ",")
				}
				return dims
			}
			backup := *f
			f.skip++
			w()
			w(gtx)
			f.skip--
			children = append(children, layout.Flexed(weight, func() {
			children = append(children, layout.Flexed(weight, func(gtx layout.Context) layout.Dimensions {
				*f = backup
				w()
				return w(gtx)
			}))
		default:
			errorf("invalid flex child")


@@ 317,12 303,14 @@ loop:
	}
	if f.skip == 0 {
		backup := *f
		fl.Layout(gtx, children...)
		dims := fl.Layout(gtx, children...)
		*f = backup
		return dims
	}
	return layout.Dimensions{}
}

func parseInset(gtx *layout.Context, f *formatter, widgets []layout.Widget) layout.Inset {
func parseInset(gtx layout.Context, f *formatter, widgets []layout.Widget) layout.Inset {
	v1 := parseValue(f)
	if peek(f) == ',' {
		expect(f, ",")


@@ 512,16 500,3 @@ func errorf(f string, args ...interface{}) {
func (e formatError) Error() string {
	return string(e)
}

// layout a widget with a set of constraints and return its
// dimensions. The widget dimensions are constrained abd the previous
// constraints are restored after layout.
func ctxLayout(gtx *layout.Context, cs layout.Constraints, w layout.Widget) layout.Dimensions {
	saved := gtx.Constraints
	gtx.Constraints = cs
	gtx.Dimensions = layout.Dimensions{}
	w()
	gtx.Dimensions.Size = cs.Constrain(gtx.Dimensions.Size)
	gtx.Constraints = saved
	return gtx.Dimensions
}