ppforge/internal/ui/flowlayout.go

65 lines
1.5 KiB
Go
Raw Normal View History

package ui
2022-11-23 20:33:07 +01:00
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)
type FlowLayout struct {
LastKnownParentSize fyne.Size
2022-11-24 13:50:08 +01:00
LastMinDimensions fyne.Size
2022-11-23 20:33:07 +01:00
}
2022-11-24 21:49:10 +01:00
func NewFlowLayout(o ...fyne.CanvasObject) fyne.Layout {
2022-11-24 13:50:08 +01:00
f := &FlowLayout{}
2022-11-24 21:49:10 +01:00
return f
2022-11-24 13:50:08 +01:00
}
2022-11-23 20:33:07 +01:00
2022-11-24 13:50:08 +01:00
// MinSize is a kind of boogie function for the flow layout.
func (fl *FlowLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
return fl.LastMinDimensions
2022-11-23 20:33:07 +01:00
}
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
}
2022-11-24 13:50:08 +01:00
/*func getAvgWidth(objects []fyne.CanvasObject) fyne.Size {
ret := fyne.NewSize(0, 0)
for _, v := range objects {
ret.Width += v.MinSize().Width
}
ret.Width = ret.Width / float32(len(objects))
return ret
}*/
// Layout arranges the objects according to the flow
2022-11-23 20:33:07 +01:00
func (fl *FlowLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
fl.LastKnownParentSize = size
2022-11-24 13:50:08 +01:00
i, x, y, w := 0, float32(0), float32(0), float32(0)
lastRowEnd := 0
for _, child := range objects {
if !child.Visible() {
continue
}
if x+child.MinSize().Width > size.Width {
w = x
x = 0
y += getMinHeight(objects[lastRowEnd:i]).Height + theme.Padding()
lastRowEnd = i
2022-11-23 20:33:07 +01:00
}
2022-11-24 13:50:08 +01:00
child.Move(fyne.NewPos(x, y))
child.Resize(child.MinSize())
x += child.MinSize().Width + theme.Padding()
i++
2022-11-23 20:33:07 +01:00
}
2022-11-24 13:50:08 +01:00
y += getMinHeight(objects[lastRowEnd:i]).Height + theme.Padding()
fl.LastMinDimensions = fyne.NewSize(w/2, y)
2022-11-23 20:33:07 +01:00
}