Skip to content

Commit

Permalink
Fix bug in Box layout causing extra padding with hidden objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Oct 17, 2023
1 parent 0635d3e commit a9a66f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
22 changes: 15 additions & 7 deletions layout/boxlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ func (g *boxLayout) isSpacer(obj fyne.CanvasObject) bool {
// Any spacers added will pad the view, sharing the space if there are two or more.
func (g *boxLayout) 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 g.isSpacer(child) {
spacers++
continue
}

visibleObjects++
if g.horizontal {
total += child.MinSize().Width
} else {
Expand All @@ -69,15 +73,19 @@ func (g *boxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
}

padding := theme.Padding()

// Amount of space not taken up by visible objects and inter-object padding
var extra float32
if g.horizontal {
extra = size.Width - total - (padding * float32(len(objects)-spacers-1))
extra = size.Width - total - (padding * float32(visibleObjects-1))
} else {
extra = size.Height - total - (padding * float32(len(objects)-spacers-1))
extra = size.Height - total - (padding * float32(visibleObjects-1))
}
extraCell := float32(0)

// 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)
Expand All @@ -88,9 +96,9 @@ func (g *boxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {

if g.isSpacer(child) {
if g.horizontal {
x += extraCell
x += spacerSize
} else {
y += extraCell
y += spacerSize
}
continue
}
Expand Down
17 changes: 17 additions & 0 deletions layout/boxlayout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,20 @@ func TestVBoxLayout_MiddleSpacer(t *testing.T) {
cell3Pos := fyne.NewPos(0, 250)
assert.Equal(t, cell3Pos, obj3.Position())
}

// Test for issue #4259 - spacer in HBox with hidden item causing add'l trailing padding
func TestHBoxLayout_MiddleSpacerHiddenItem(t *testing.T) {
cellSize := fyne.NewSize(100, 50)

obj1 := NewMinSizeRect(cellSize)
obj2 := NewMinSizeRect(cellSize)
obj3 := NewMinSizeRect(cellSize)

container := container.NewHBox(obj1, obj2, layout.NewSpacer(), obj3)
container.Resize(fyne.NewSize(400, 100))
assert.Equal(t, fyne.NewPos(300, 0), obj3.Position())

obj2.Hide()
container.Refresh()
assert.Equal(t, fyne.NewPos(300, 0), obj3.Position())
}

0 comments on commit a9a66f6

Please sign in to comment.