Skip to content

Commit

Permalink
Refactor away the need for counting rows beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed May 2, 2024
1 parent 095007f commit e5d6ccd
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions layout/formlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,39 @@ const formLayoutCols = 2
var _ fyne.Layout = (*formLayout)(nil)

// formLayout is two column grid where each row has a label and a widget.
type formLayout struct {
}

func (f *formLayout) countRows(objects []fyne.CanvasObject) int {
count := 0
type formLayout struct{}

for i := 0; i < len(objects); i += formLayoutCols {
if !objects[i].Visible() && !objects[i+1].Visible() {
continue
}
count++
}

return count
}

// calculateTableSizes calculates the size of all of the cells in the table.
// It returns the width of the left column (labels), the width of the right column (content)
// as well as a slice with the height of each row. The height of each row will be set as the max
// size of the label and content cell heights for that row. The width of the label column will
// be set as the max width of all the label cells. The width of the content column will be set as
// the max width of all the content cells or the remaining space of the bounding containerWidth,
// if it is larger.
// calculateTableSizes calculates the izes of the table.
// This includes the width of the label column (maximum width of all labels),
// the width of the content column (maximum width of all content cells and remaining space in container)
// and the total minimum height of the form.
func (f *formLayout) calculateTableSizes(objects []fyne.CanvasObject, containerWidth float32) (labelWidth float32, contentWidth float32, height float32) {
if (len(objects))%formLayoutCols != 0 {
if len(objects)%formLayoutCols != 0 {
return 0, 0, 0
}

rows := 0
innerPadding := theme.InnerPadding()
lowBound := 0
highBound := 2
rows := f.countRows(objects)
for row := 0; row < rows; {
currentRow := objects[lowBound:highBound]
lowBound = highBound
highBound += formLayoutCols
if !currentRow[0].Visible() && !currentRow[1].Visible() {
for i := 0; i < len(objects); i += formLayoutCols {
labelCell, contentCell := objects[i], objects[i+1]
if !labelCell.Visible() && !contentCell.Visible() {
continue
}

labelCell := currentRow[0].MinSize()
if _, ok := currentRow[0].(*canvas.Text); ok {
labelCell.Width += innerPadding * 2
// Label column width is the maximum of all labels.
labelSize := labelCell.MinSize()
if _, ok := labelCell.(*canvas.Text); ok {
labelSize.Width += innerPadding * 2
}
labelWidth = fyne.Max(labelWidth, labelCell.Width)
labelWidth = fyne.Max(labelWidth, labelSize.Width)

contentCell := currentRow[1].MinSize()
contentWidth = fyne.Max(contentWidth, contentCell.Width)
// Content column width is the maximum of all content items.
contentSize := contentCell.MinSize()
contentWidth = fyne.Max(contentWidth, contentSize.Width)

rowHeight := fyne.Max(labelCell.Height, contentCell.Height)
rowHeight := fyne.Max(labelSize.Height, contentSize.Height)
height += rowHeight
row++
rows++
}

padding := theme.Padding()
Expand Down

0 comments on commit e5d6ccd

Please sign in to comment.