From 990029985ad7b01ef304b5a419b05204f83cac8a Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Thu, 1 Jul 2021 14:48:38 -0400 Subject: [PATCH] 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 --- layout/list.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/layout/list.go b/layout/list.go index 07858a6a..e257525b 100644 --- a/layout/list.go +++ b/layout/list.go @@ -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) }