From e5d6ccd3c4f827d695650706ffd47eb9dff2226e Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 2 May 2024 16:57:22 +0200 Subject: [PATCH] Refactor away the need for counting rows beforehand --- layout/formlayout.go | 59 +++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/layout/formlayout.go b/layout/formlayout.go index 96e44ebcc7..e02587f1c3 100644 --- a/layout/formlayout.go +++ b/layout/formlayout.go @@ -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()