layout: add List.Gap for spacing out items

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
Egon Elbre
2026-02-17 22:12:34 +02:00
committed by Elias Naur
parent 9b38545fc2
commit 4ed9695d57
2 changed files with 107 additions and 7 deletions
+20 -7
View File
@@ -31,6 +31,8 @@ type List struct {
Alignment Alignment
// ScrollAnyAxis allows any scroll axis to scroll the list, not just the main axis.
ScrollAnyAxis bool
// Gap is the space in pixels between children.
Gap int
cs Constraints
scroll gesture.Scroll
@@ -130,7 +132,7 @@ func (l *List) Layout(gtx Context, len int, w ListElement) Dimensions {
}
if numLaidOut > 0 {
l.Position.Length = laidOutTotalLength * len / numLaidOut
l.Position.Length = laidOutTotalLength*len/numLaidOut + l.Gap*(len-1)
} else {
l.Position.Length = 0
}
@@ -223,11 +225,11 @@ func (l *List) nextDir() iterationDir {
if len(l.children) > 0 {
if l.Position.First > 0 {
firstChild := l.children[0]
firstSize = l.Axis.Convert(firstChild.size).X
firstSize = l.Axis.Convert(firstChild.size).X + l.Gap
}
if last < l.len {
lastChild := l.children[len(l.children)-1]
lastSize = l.Axis.Convert(lastChild.size).X
lastSize = l.Axis.Convert(lastChild.size).X + l.Gap
}
}
switch {
@@ -245,6 +247,9 @@ func (l *List) nextDir() iterationDir {
func (l *List) end(dims Dimensions, call op.CallOp) {
child := scrollChild{dims.Size, call}
mainSize := l.Axis.Convert(child.size).X
if len(l.children) > 0 {
l.maxSize += l.Gap
}
l.maxSize += mainSize
switch l.dir {
case iterateForward:
@@ -254,7 +259,7 @@ func (l *List) end(dims Dimensions, call op.CallOp) {
copy(l.children[1:], l.children)
l.children[0] = child
l.Position.First--
l.Position.Offset += mainSize
l.Position.Offset += mainSize + l.Gap
default:
panic("call Next before End")
}
@@ -279,7 +284,7 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
break
}
l.Position.First++
l.Position.Offset -= mainSize
l.Position.Offset -= mainSize + l.Gap
first = child
children = children[1:]
}
@@ -291,6 +296,9 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
if c := sz.Y; c > maxCross {
maxCross = c
}
if i > 0 {
size += l.Gap
}
size += sz.X
if size >= mainMax {
if i < len(children)-1 {
@@ -326,14 +334,19 @@ func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
// Lay out leading invisible child.
if first != (scrollChild{}) {
sz := l.Axis.Convert(first.size)
pos -= sz.X
pos -= sz.X + l.Gap
layout(first)
pos += l.Gap
}
for _, child := range children {
for i, child := range children {
if i > 0 {
pos += l.Gap
}
layout(child)
}
// Lay out trailing invisible child.
if last != (scrollChild{}) {
pos += l.Gap
layout(last)
}
atStart := l.Position.First == 0 && l.Position.Offset <= 0