~eliasnaur/gio-patches

layout: refactor List.Layout and related functions v1 PROPOSED

Larry Clapp: 1
 layout: refactor List.Layout and related functions

 1 files changed, 13 insertions(+), 18 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/11571/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] layout: refactor List.Layout and related functions Export this patch

I found the interplay of List's Layout/init/next/more/end methods
somewhat confusing and hard to reason about, so I refactored them.
Signed-off-by: Larry Clapp <larry@theclapp.org>
---
 layout/list.go | 31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

diff --git a/layout/list.go b/layout/list.go
index dad83cf..2375fea 100644
--- a/layout/list.go
+++ b/layout/list.go
@@ -30,8 +30,6 @@ type List struct {
	Alignment Alignment

	ctx         Context
	macro       op.MacroOp
	child       op.MacroOp
	scroll      gesture.Scroll
	scrollDelta int

@@ -95,20 +93,21 @@ func (l *List) init(gtx Context, len int) {
		l.Position.Offset = 0
		l.Position.First = len
	}
	l.macro = op.Record(gtx.Ops)
	l.next()
}

// Layout the List.
func (l *List) Layout(gtx Context, len int, w ListElement) Dimensions {
	for l.init(gtx, len); l.more(); l.next() {
		crossMin, crossMax := axisCrossConstraint(l.Axis, l.ctx.Constraints)
		cs := axisConstraints(l.Axis, 0, inf, crossMin, crossMax)
		i := l.index()
		gtx.Constraints = cs
		l.end(w(gtx, i))
	l.init(gtx, len)
	crossMin, crossMax := axisCrossConstraint(l.Axis, gtx.Constraints)
	gtx.Constraints = axisConstraints(l.Axis, 0, inf, crossMin, crossMax)
	macro := op.Record(gtx.Ops)
	for l.next(); l.more(); l.next() {
		child := op.Record(gtx.Ops)
		dims := w(gtx, l.index())
		call := child.Stop()
		l.end(dims, call)
	}
	return l.layout()
	return l.layout(macro)
}

func (l *List) scrollToEnd() bool {
@@ -136,9 +135,6 @@ func (l *List) next() {
		l.Position.Offset += l.scrollDelta
		l.dir = l.nextDir()
	}
	if l.more() {
		l.child = op.Record(l.ctx.Ops)
	}
}

// index is current child's position in the underlying list.
@@ -180,8 +176,7 @@ func (l *List) nextDir() iterationDir {
}

// End the current child by specifying its dimensions.
func (l *List) end(dims Dimensions) {
	call := l.child.Stop()
func (l *List) end(dims Dimensions, call op.CallOp) {
	child := scrollChild{dims.Size, call}
	mainSize := axisMain(l.Axis, child.size)
	l.maxSize += mainSize
@@ -199,7 +194,7 @@ func (l *List) end(dims Dimensions) {
}

// Layout the List and return its dimensions.
func (l *List) layout() Dimensions {
func (l *List) layout(macro op.MacroOp) Dimensions {
	if l.more() {
		panic("unfinished child")
	}
@@ -277,7 +272,7 @@ func (l *List) layout() Dimensions {
		pos = mainMax
	}
	dims := axisPoint(l.Axis, pos, maxCross)
	call := l.macro.Stop()
	call := macro.Stop()
	defer op.Push(l.ctx.Ops).Pop()
	pointer.Rect(image.Rectangle{Max: dims}).Add(ops)
	l.scroll.Add(ops)
-- 
2.26.2