~eliasnaur/gio

9e85b43b0c72217cc315345f1f359d23ec73ed57 — Egon Elbre 2 months ago 3627df7
layout: expose Direction.Position calculation

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
1 files changed, 22 insertions(+), 12 deletions(-)

M layout/layout.go
M layout/layout.go => layout/layout.go +22 -12
@@ 173,27 173,37 @@ func (d Direction) Layout(gtx Context, w Widget) Dimensions {
	if sz.Y < cs.Min.Y {
		sz.Y = cs.Min.Y
	}

	defer op.Save(gtx.Ops).Load()
	p := d.Position(dims.Size, sz)
	op.Offset(FPt(p)).Add(gtx.Ops)
	call.Add(gtx.Ops)

	return Dimensions{
		Size:     sz,
		Baseline: dims.Baseline + sz.Y - dims.Size.Y - p.Y,
	}
}

// Position calculates widget position according to the direction.
func (d Direction) Position(widget, bounds image.Point) image.Point {
	var p image.Point

	switch d {
	case N, S, Center:
		p.X = (sz.X - dims.Size.X) / 2
		p.X = (bounds.X - widget.X) / 2
	case NE, SE, E:
		p.X = sz.X - dims.Size.X
		p.X = bounds.X - widget.X
	}

	switch d {
	case W, Center, E:
		p.Y = (sz.Y - dims.Size.Y) / 2
		p.Y = (bounds.Y - widget.Y) / 2
	case SW, S, SE:
		p.Y = sz.Y - dims.Size.Y
	}
	stack := op.Save(gtx.Ops)
	op.Offset(FPt(p)).Add(gtx.Ops)
	call.Add(gtx.Ops)
	stack.Load()
	return Dimensions{
		Size:     sz,
		Baseline: dims.Baseline + sz.Y - dims.Size.Y - p.Y,
		p.Y = bounds.Y - widget.Y
	}

	return p
}

// Spacer adds space between widgets.