layout: make List approximate its length in Position

This commit adds a Length field to the
layout.Position. This field contains an approximation
of the overall length of the list's contents.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2021-07-01 14:48:38 -04:00
committed by Elias Naur
parent 2e991f31be
commit 990029985a
+13
View File
@@ -74,6 +74,8 @@ type Position struct {
OffsetLast int
// Count is the number of visible children.
Count int
// Length is the estimated total size of all children, measured in pixels.
Length int
}
const (
@@ -106,11 +108,22 @@ func (l *List) Layout(gtx Context, len int, w ListElement) Dimensions {
crossMin, crossMax := l.Axis.crossConstraint(gtx.Constraints)
gtx.Constraints = l.Axis.constraints(0, inf, crossMin, crossMax)
macro := op.Record(gtx.Ops)
laidOutTotalLength := 0
numLaidOut := 0
for l.next(); l.more(); l.next() {
child := op.Record(gtx.Ops)
dims := w(gtx, l.index())
call := child.Stop()
l.end(dims, call)
laidOutTotalLength += l.Axis.Convert(dims.Size).X
numLaidOut++
}
if numLaidOut > 0 {
l.Position.Length = laidOutTotalLength * len / numLaidOut
} else {
l.Position.Length = 0
}
return l.layout(gtx.Ops, macro)
}