Skip to content

Commit

Permalink
Refactor code to handle uneven item last
Browse files Browse the repository at this point in the history
Avoids lots of if i + 1 checks.
  • Loading branch information
Jacalz committed May 2, 2024
1 parent 87262e1 commit 5ab30a0
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions layout/formlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,50 +56,50 @@ func (f *formLayout) calculateTableSizes(objects []fyne.CanvasObject, containerW
func (f *formLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
labelWidth, contentWidth, _ := f.calculateTableSizes(objects, size.Width)

y := float32(0)
padding := theme.Padding()
innerPadding := theme.InnerPadding()

y := float32(0)
for i := 0; i < len(objects); i += formLayoutCols {
if !objects[i].Visible() && (i+1 < len(objects) && !objects[i+1].Visible()) {
continue
// Calculate size and position of object. Position and size is returned (instead of calling Move() and Resize()) to make inlineable.
objectLayout := func(obj fyne.CanvasObject, offset, width, rowHeight, itemHeight float32) (fyne.Position, fyne.Size) {
pos := fyne.NewPos(offset, y)
size := fyne.NewSize(width, rowHeight)
if _, ok := obj.(*canvas.Text); ok {
pos = pos.AddXY(innerPadding, innerPadding)
size.Width -= innerPadding * 2
size.Height = itemHeight
}

labelMin := objects[i].MinSize()
contentMin := fyne.Size{}
return pos, size
}

rowHeight := labelMin.Height
if i+1 < len(objects) {
contentMin = objects[i+1].MinSize()
rowHeight = fyne.Max(rowHeight, contentMin.Height)
remainer := len(objects) % formLayoutCols
for i := 0; i < len(objects)-remainer; i += formLayoutCols {
if !objects[i].Visible() && !objects[i+1].Visible() {
continue
}

pos := fyne.NewPos(0, y)
size := fyne.NewSize(labelWidth, rowHeight)
if _, ok := objects[i].(*canvas.Text); ok {
pos = pos.AddXY(innerPadding, innerPadding)
size.Width -= innerPadding * 2
size.Height = labelMin.Height
}
labelMin := objects[i].MinSize()
contentMin := objects[i+1].MinSize()
rowHeight := fyne.Max(labelMin.Height, contentMin.Height)

pos, size := objectLayout(objects[i], 0, labelWidth, rowHeight, labelMin.Height)
objects[i].Move(pos)
objects[i].Resize(size)

if i+1 < len(objects) {
pos = fyne.NewPos(padding+labelWidth, y)
size = fyne.NewSize(contentWidth, rowHeight)
if _, ok := objects[i+1].(*canvas.Text); ok {
pos = pos.AddXY(innerPadding, innerPadding)
size.Width -= innerPadding * 2
size.Height = contentMin.Height
}

objects[i+1].Move(pos)
objects[i+1].Resize(size)
}
pos, size = objectLayout(objects[i+1], labelWidth+padding, contentWidth, rowHeight, contentMin.Height)
objects[i+1].Move(pos)
objects[i+1].Resize(size)

y += rowHeight + padding
}

// Handle remaining item in the case of uneven number of objects:
if remainer == 1 {
lastCell := objects[len(objects)-1]
lastMin := lastCell.MinSize()
objectLayout(lastCell, 0, labelWidth, lastMin.Height, lastMin.Height)
}
}

// MinSize finds the smallest size that satisfies all the child objects.
Expand Down

0 comments on commit 5ab30a0

Please sign in to comment.