ppforge/flowlayout.go

64 lines
1.2 KiB
Go

package npc
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)
type FlowLayout struct {
LastKnownParentSize fyne.Size
}
func (fl *FlowLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
return fyne.NewSize(0, 0)
}
func getMinHeight(objects []fyne.CanvasObject) fyne.Size {
ret := fyne.NewSize(0, 0)
for _, v := range objects {
if v.MinSize().Height > ret.Height {
ret.Height = v.MinSize().Height
}
}
return ret
}
func (fl *FlowLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
fl.LastKnownParentSize = size
if fl.LastKnownParentSize.Height > size.Height {
i, x, y := 0, float32(0), float32(0)
lastRowMaxH, lastRowEnd := float32(0), 0
for _, child := range objects {
if !child.Visible() {
continue
}
if i > 1 {
if x+objects[i-1].MinSize().Width > size.Width {
x = 0
y = y + getMinHeight(objects[lastRowEnd:i]).Height
lastRowEnd = i
} else {
x = x + objects[i-1].MinSize().Width
}
}
if i%g.colCount == 0 {
g.rowCount++
}
child.Move(fyne.NewPos(x, y))
child.Resize(g.CellSize)
if (i+1)%g.colCount == 0 {
x = 0
y += g.CellSize.Height + theme.Padding()
} else {
x += g.CellSize.Width + theme.Padding()
}
i++
}
}
}