From 207d3ecf7ddd63cfac700d44e8403849280989b7 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Sat, 27 Apr 2024 14:54:38 -0300 Subject: [PATCH] misc: Port latest Fyne layout changes Specific commits ported, to update the layouts and reduce differences with upstream Fyne code: * https://github.com/fyne-io/fyne/commit/a9a66f60d2167d7945bcf7718a781335541317c0 * https://github.com/fyne-io/fyne/commit/2cde868904092382ca5f645802ee9d444d8ae6e8 * https://github.com/fyne-io/fyne/commit/99fa7e3a7dc83dd273ad366cc7d336a1e1cee5f6 * https://github.com/fyne-io/fyne/commit/4ea8a5762f1744401ede9f7bd35b3b75890f925e --- ui/layouts/gridlayoutcustompadding.go | 31 +++++++++------ ui/layouts/hboxcustompadding.go | 49 ++++++++++++----------- ui/layouts/maxpadlayout.go | 2 +- ui/layouts/vboxcustompadding.go | 56 ++++++++++++++++++++++----- 4 files changed, 93 insertions(+), 45 deletions(-) diff --git a/ui/layouts/gridlayoutcustompadding.go b/ui/layouts/gridlayoutcustompadding.go index 19f13e0..754b6b4 100644 --- a/ui/layouts/gridlayoutcustompadding.go +++ b/ui/layouts/gridlayoutcustompadding.go @@ -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) } diff --git a/ui/layouts/hboxcustompadding.go b/ui/layouts/hboxcustompadding.go index 6005d35..8500002 100644 --- a/ui/layouts/hboxcustompadding.go +++ b/ui/layouts/hboxcustompadding.go @@ -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)) } } diff --git a/ui/layouts/maxpadlayout.go b/ui/layouts/maxpadlayout.go index 7af0d37..314bddf 100644 --- a/ui/layouts/maxpadlayout.go +++ b/ui/layouts/maxpadlayout.go @@ -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 diff --git a/ui/layouts/vboxcustompadding.go b/ui/layouts/vboxcustompadding.go index 4d22088..fe25125 100644 --- a/ui/layouts/vboxcustompadding.go +++ b/ui/layouts/vboxcustompadding.go @@ -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 } }