Merge pull request #376 from adamantike/misc/port-latest-layout-changes

misc: Port latest Fyne layout changes
This commit is contained in:
Drew Weymouth
2024-04-27 11:16:06 -07:00
committed by GitHub
4 changed files with 93 additions and 45 deletions
+19 -12
View File
@@ -86,17 +86,18 @@ func (g *gridLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
rows := g.countRows(objects) rows := g.countRows(objects)
padding := theme.Padding() + g.Padding padding := theme.Padding() + g.Padding
padWidth := float32(g.Cols-1) * padding
padHeight := float32(rows-1) * padding
cellWidth := float64(size.Width-padWidth) / float64(g.Cols)
cellHeight := float64(size.Height-padHeight) / float64(rows)
if !g.horizontal() { primaryObjects := rows
padWidth, padHeight = padHeight, padWidth secondaryObjects := g.Cols
cellWidth = float64(size.Width-padWidth) / float64(rows) if g.horizontal() {
cellHeight = float64(size.Height-padHeight) / float64(g.Cols) primaryObjects, secondaryObjects = secondaryObjects, primaryObjects
} }
padWidth := float32(primaryObjects-1) * padding
padHeight := float32(secondaryObjects-1) * padding
cellWidth := float64(size.Width-padWidth) / float64(primaryObjects)
cellHeight := float64(size.Height-padHeight) / float64(secondaryObjects)
row, col := 0, 0 row, col := 0, 0
i := 0 i := 0
for _, child := range objects { for _, child := range objects {
@@ -147,11 +148,17 @@ func (g *gridLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
} }
padding := theme.Padding() + g.Padding padding := theme.Padding() + g.Padding
primaryObjects := rows
secondaryObjects := g.Cols
if g.horizontal() { if g.horizontal() {
minContentSize := fyne.NewSize(minSize.Width*float32(g.Cols), minSize.Height*float32(rows)) primaryObjects, secondaryObjects = secondaryObjects, primaryObjects
return minContentSize.Add(fyne.NewSize(padding*fyne.Max(float32(g.Cols-1), 0), padding*fyne.Max(float32(rows-1), 0)))
} }
minContentSize := fyne.NewSize(minSize.Width*float32(rows), minSize.Height*float32(g.Cols)) width := minSize.Width * float32(primaryObjects)
return minContentSize.Add(fyne.NewSize(padding*fyne.Max(float32(rows-1), 0), padding*fyne.Max(float32(g.Cols-1), 0))) height := minSize.Height * float32(secondaryObjects)
xpad := padding * fyne.Max(float32(primaryObjects-1), 0)
ypad := padding * fyne.Max(float32(secondaryObjects-1), 0)
return fyne.NewSize(width+xpad, height+ypad)
} }
+27 -22
View File
@@ -6,7 +6,7 @@ import (
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
) )
var _ fyne.Layout = (*VboxCustomPadding)(nil) var _ fyne.Layout = (*HboxCustomPadding)(nil)
type HboxCustomPadding struct { type HboxCustomPadding struct {
ExtraPad float32 ExtraPad float32
@@ -14,14 +14,8 @@ type HboxCustomPadding struct {
} }
func (*HboxCustomPadding) isSpacer(obj fyne.CanvasObject) bool { func (*HboxCustomPadding) isSpacer(obj fyne.CanvasObject) bool {
if !obj.Visible() { spacer, ok := obj.(layout.SpacerObject)
return false return ok && spacer.ExpandHorizontal()
}
if spacer, ok := obj.(layout.SpacerObject); ok {
return spacer.ExpandHorizontal()
}
return false
} }
func (h *HboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size { func (h *HboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size {
@@ -29,15 +23,13 @@ func (h *HboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size {
addPadding := false addPadding := false
padding := h.themePad() + h.ExtraPad padding := h.themePad() + h.ExtraPad
for _, child := range objects { for _, child := range objects {
if !child.Visible() { if !child.Visible() || h.isSpacer(child) {
continue
}
if h.isSpacer(child) {
continue continue
} }
minSize.Height = fyne.Max(child.MinSize().Height, minSize.Height) childMin := child.MinSize()
minSize.Width += child.MinSize().Width minSize.Height = fyne.Max(childMin.Height, minSize.Height)
minSize.Width += childMin.Width
if addPadding { if addPadding {
minSize.Width += padding minSize.Width += padding
} }
@@ -48,37 +40,50 @@ func (h *HboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size {
func (h *HboxCustomPadding) Layout(objects []fyne.CanvasObject, size fyne.Size) { func (h *HboxCustomPadding) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0 spacers := 0
visibleObjects := 0
// Size taken up by visible objects
total := float32(0) total := float32(0)
for _, child := range objects { for _, child := range objects {
if !child.Visible() { if !child.Visible() {
continue continue
} }
if h.isSpacer(child) { if h.isSpacer(child) {
spacers++ spacers++
continue continue
} }
visibleObjects++
total += child.MinSize().Width total += child.MinSize().Width
} }
x, y := float32(0), float32(0)
padding := h.themePad() + h.ExtraPad padding := h.themePad() + h.ExtraPad
extra := size.Width - total - (padding * float32(len(objects)-spacers-1))
extraCell := float32(0) // Amount of space not taken up by visible objects and inter-object padding
extra := size.Width - total - (padding * float32(visibleObjects-1))
// Spacers split extra space equally
spacerSize := float32(0)
if spacers > 0 { if spacers > 0 {
extraCell = extra / float32(spacers) spacerSize = extra / float32(spacers)
} }
x, y := float32(0), float32(0)
for _, child := range objects { for _, child := range objects {
if !child.Visible() { if !child.Visible() {
continue continue
} }
if h.isSpacer(child) { if h.isSpacer(child) {
x += extraCell x += spacerSize
continue
} }
width := child.MinSize().Width
child.Move(fyne.NewPos(x, y)) child.Move(fyne.NewPos(x, y))
child.Resize(fyne.NewSize(width, size.Height))
width := child.MinSize().Width
x += padding + width x += padding + width
child.Resize(fyne.NewSize(width, size.Height))
} }
} }
+1 -1
View File
@@ -2,7 +2,7 @@ package layouts
import "fyne.io/fyne/v2" import "fyne.io/fyne/v2"
var _ fyne.Layout = (*CenterPadLayout)(nil) var _ fyne.Layout = (*MaxPadLayout)(nil)
type MaxPadLayout struct { type MaxPadLayout struct {
PadLeft float32 PadLeft float32
+46 -10
View File
@@ -2,6 +2,7 @@ package layouts
import ( import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
) )
@@ -11,41 +12,76 @@ type VboxCustomPadding struct {
ExtraPad float32 ExtraPad float32
} }
func (*VboxCustomPadding) isSpacer(obj fyne.CanvasObject) bool {
spacer, ok := obj.(layout.SpacerObject)
return ok && spacer.ExpandVertical()
}
func (v *VboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size { func (v *VboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0) minSize := fyne.NewSize(0, 0)
addPadding := false
padding := theme.Padding() + v.ExtraPad
for _, child := range objects { for _, child := range objects {
if !child.Visible() { if !child.Visible() || v.isSpacer(child) {
continue continue
} }
minSize.Width = fyne.Max(child.MinSize().Width, minSize.Width) childMin := child.MinSize()
minSize.Height += child.MinSize().Height minSize.Width = fyne.Max(childMin.Width, minSize.Width)
minSize.Height += childMin.Height
if addPadding {
minSize.Height += padding
}
addPadding = true
} }
minSize.Height += (theme.Padding() + v.ExtraPad) * float32(len(objects)-1)
return minSize return minSize
} }
func (v *VboxCustomPadding) Layout(objects []fyne.CanvasObject, size fyne.Size) { func (v *VboxCustomPadding) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0
visibleObjects := 0
// Size taken up by visible objects
total := float32(0) total := float32(0)
for _, child := range objects { for _, child := range objects {
if !child.Visible() { if !child.Visible() {
continue continue
} }
if v.isSpacer(child) {
spacers++
continue
}
visibleObjects++
total += child.MinSize().Height total += child.MinSize().Height
} }
x, y := float32(0), float32(0)
padding := theme.Padding() + v.ExtraPad padding := theme.Padding() + v.ExtraPad
extra := float32(0)
// Amount of space not taken up by visible objects and inter-object padding
extra := size.Height - total - (padding * float32(visibleObjects-1))
// Spacers split extra space equally
spacerSize := float32(0)
if spacers > 0 {
spacerSize = extra / float32(spacers)
}
x, y := float32(0), float32(0)
for _, child := range objects { for _, child := range objects {
if !child.Visible() { if !child.Visible() {
continue continue
} }
if v.isSpacer(child) {
y += spacerSize
continue
}
child.Move(fyne.NewPos(x, y))
height := child.MinSize().Height height := child.MinSize().Height
child.Move(fyne.NewPos(x, y+extra)) y += padding + height
y += height
child.Resize(fyne.NewSize(size.Width, height)) child.Resize(fyne.NewSize(size.Width, height))
extra += padding
} }
} }