A flow layout added

This commit is contained in:
Marcel Otte 2022-11-24 13:50:08 +01:00
parent 5bbc0ac046
commit a1defca772
7 changed files with 68 additions and 38 deletions

2
app.go
View File

@ -16,7 +16,7 @@ func Appmain() {
nil,
nil,
nil,
GetFieldEditor().Representation,
container.NewVScroll(GetFieldEditor().Representation),
// widget.NewButton("Hi!", func() {
// hello.SetText("Welcome :)")
// }),

View File

@ -4,7 +4,6 @@ import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
)
type FieldEditor struct {
@ -16,7 +15,7 @@ var fieldEditor *FieldEditor
func GetFieldEditor() *FieldEditor {
if fieldEditor == nil {
container := container.NewGridWrap(fyne.NewSize(200, 150))
container := NewFlowLayout()
fieldEditor = &FieldEditor{container, []*FieldCreator{}}
container.Add(GetAdder())
}

View File

@ -2,16 +2,23 @@ package npc
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
)
type FlowLayout struct {
LastKnownParentSize fyne.Size
LastMinDimensions fyne.Size
}
func (fl *FlowLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
func NewFlowLayout(o ...fyne.CanvasObject) *fyne.Container {
f := &FlowLayout{}
return container.New(f, o...)
}
return fyne.NewSize(0, 0)
// 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 {
@ -24,40 +31,35 @@ func getMinHeight(objects []fyne.CanvasObject) fyne.Size {
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
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++
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)
}

24
flowlayout_test.go Normal file
View File

@ -0,0 +1,24 @@
package npc
import (
"fmt"
"testing"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/widget"
)
func TestFlowLayout_Simple(t *testing.T) {
//padding := theme.Padding()
objs := []fyne.CanvasObject{
widget.NewLabel("Hello World"),
}
layout := NewFlowLayout(objs...)
win := test.NewWindow(layout)
defer win.Close()
win.Resize(fyne.NewSize(200, 200))
size := layout.Size()
fmt.Printf("%v", size)
}

5
go.mod
View File

@ -2,7 +2,10 @@ module gitea.mmo.to/NetworkPacketComposer/npc
go 1.19
require fyne.io/fyne/v2 v2.2.4
require (
fyne.io/fyne/v2 v2.2.4
github.com/magiconair/properties v1.8.5
)
require (
fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 // indirect

1
go.sum
View File

@ -208,6 +208,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucor/goinfo v0.0.0-20210802170112-c078a2b0f08b/go.mod h1:PRq09yoB+Q2OJReAmwzKivcYyremnibWGbK7WfftHzc=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=

View File

@ -1 +1,2 @@
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=