package ui import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/theme" ) type FlowLayout struct { LastKnownParentSize fyne.Size LastMinDimensions fyne.Size } func NewFlowLayout(o ...fyne.CanvasObject) fyne.Layout { f := &FlowLayout{} return f } // MinSize is a kind of boogie function for the flow layout. func (fl *FlowLayout) MinSize(objects []fyne.CanvasObject) fyne.Size { return fl.LastMinDimensions } 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 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 func (fl *FlowLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) { fl.LastKnownParentSize = size 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 } child.Move(fyne.NewPos(x, y)) child.Resize(child.MinSize()) x += child.MinSize().Width + theme.Padding() i++ } y += getMinHeight(objects[lastRowEnd:i]).Height + theme.Padding() fl.LastMinDimensions = fyne.NewSize(w/2, y) }