From a1defca77275373188bb74931abc560830fc26af Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Thu, 24 Nov 2022 13:50:08 +0100 Subject: [PATCH] A flow layout added --- app.go | 2 +- fieldeditor.go | 3 +- flowlayout.go | 70 ++++++++++++++++++++++++---------------------- flowlayout_test.go | 24 ++++++++++++++++ go.mod | 5 +++- go.sum | 1 + go.work.sum | 1 + 7 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 flowlayout_test.go diff --git a/app.go b/app.go index d2aeb39..8509d45 100644 --- a/app.go +++ b/app.go @@ -16,7 +16,7 @@ func Appmain() { nil, nil, nil, - GetFieldEditor().Representation, + container.NewVScroll(GetFieldEditor().Representation), // widget.NewButton("Hi!", func() { // hello.SetText("Welcome :)") // }), diff --git a/fieldeditor.go b/fieldeditor.go index 11e2786..1a80036 100644 --- a/fieldeditor.go +++ b/fieldeditor.go @@ -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()) } diff --git a/flowlayout.go b/flowlayout.go index 8162bb2..aa89c5e 100644 --- a/flowlayout.go +++ b/flowlayout.go @@ -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) } diff --git a/flowlayout_test.go b/flowlayout_test.go new file mode 100644 index 0000000..47b0a26 --- /dev/null +++ b/flowlayout_test.go @@ -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) +} diff --git a/go.mod b/go.mod index 1c830a0..49e1a7c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d87edee..84daf77 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/go.work.sum b/go.work.sum index 806de33..589623f 100644 --- a/go.work.sum +++ b/go.work.sum @@ -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=