Michael Manganiello
2024-04-27 14:54:38 -03:00
parent e196475a0a
commit 207d3ecf7d
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)
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() {
padWidth, padHeight = padHeight, padWidth
cellWidth = float64(size.Width-padWidth) / float64(rows)
cellHeight = float64(size.Height-padHeight) / float64(g.Cols)
primaryObjects := rows
secondaryObjects := g.Cols
if g.horizontal() {
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
i := 0
for _, child := range objects {
@@ -147,11 +148,17 @@ func (g *gridLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
}
padding := theme.Padding() + g.Padding
primaryObjects := rows
secondaryObjects := g.Cols
if g.horizontal() {
minContentSize := fyne.NewSize(minSize.Width*float32(g.Cols), minSize.Height*float32(rows))
return minContentSize.Add(fyne.NewSize(padding*fyne.Max(float32(g.Cols-1), 0), padding*fyne.Max(float32(rows-1), 0)))
primaryObjects, secondaryObjects = secondaryObjects, primaryObjects
}
minContentSize := fyne.NewSize(minSize.Width*float32(rows), minSize.Height*float32(g.Cols))
return minContentSize.Add(fyne.NewSize(padding*fyne.Max(float32(rows-1), 0), padding*fyne.Max(float32(g.Cols-1), 0)))
width := minSize.Width * float32(primaryObjects)
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"
)
var _ fyne.Layout = (*VboxCustomPadding)(nil)
var _ fyne.Layout = (*HboxCustomPadding)(nil)
type HboxCustomPadding struct {
ExtraPad float32
@@ -14,14 +14,8 @@ type HboxCustomPadding struct {
}
func (*HboxCustomPadding) isSpacer(obj fyne.CanvasObject) bool {
if !obj.Visible() {
return false
}
if spacer, ok := obj.(layout.SpacerObject); ok {
return spacer.ExpandHorizontal()
}
return false
spacer, ok := obj.(layout.SpacerObject)
return ok && spacer.ExpandHorizontal()
}
func (h *HboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size {
@@ -29,15 +23,13 @@ func (h *HboxCustomPadding) MinSize(objects []fyne.CanvasObject) fyne.Size {
addPadding := false
padding := h.themePad() + h.ExtraPad
for _, child := range objects {
if !child.Visible() {
continue
}
if h.isSpacer(child) {
if !child.Visible() || h.isSpacer(child) {
continue
}
minSize.Height = fyne.Max(child.MinSize().Height, minSize.Height)
minSize.Width += child.MinSize().Width
childMin := child.MinSize()
minSize.Height = fyne.Max(childMin.Height, minSize.Height)
minSize.Width += childMin.Width
if addPadding {
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) {
spacers := 0
visibleObjects := 0
// Size taken up by visible objects
total := float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if h.isSpacer(child) {
spacers++
continue
}
visibleObjects++
total += child.MinSize().Width
}
x, y := float32(0), float32(0)
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 {
extraCell = extra / float32(spacers)
spacerSize = extra / float32(spacers)
}
x, y := float32(0), float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if h.isSpacer(child) {
x += extraCell
x += spacerSize
continue
}
width := child.MinSize().Width
child.Move(fyne.NewPos(x, y))
child.Resize(fyne.NewSize(width, size.Height))
width := child.MinSize().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"
var _ fyne.Layout = (*CenterPadLayout)(nil)
var _ fyne.Layout = (*MaxPadLayout)(nil)
type MaxPadLayout struct {
PadLeft float32
+46 -10
View File
@@ -2,6 +2,7 @@ package layouts
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
)
@@ -11,41 +12,76 @@ type VboxCustomPadding struct {
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 {
minSize := fyne.NewSize(0, 0)
addPadding := false
padding := theme.Padding() + v.ExtraPad
for _, child := range objects {
if !child.Visible() {
if !child.Visible() || v.isSpacer(child) {
continue
}
minSize.Width = fyne.Max(child.MinSize().Width, minSize.Width)
minSize.Height += child.MinSize().Height
childMin := child.MinSize()
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
}
func (v *VboxCustomPadding) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0
visibleObjects := 0
// Size taken up by visible objects
total := float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if v.isSpacer(child) {
spacers++
continue
}
visibleObjects++
total += child.MinSize().Height
}
x, y := float32(0), float32(0)
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 {
if !child.Visible() {
continue
}
if v.isSpacer(child) {
y += spacerSize
continue
}
child.Move(fyne.NewPos(x, y))
height := child.MinSize().Height
child.Move(fyne.NewPos(x, y+extra))
y += height
y += padding + height
child.Resize(fyne.NewSize(size.Width, height))
extra += padding
}
}