diff --git a/widget/form.go b/widget/form.go index 2aa43ed9ac..8b9d73e5be 100644 --- a/widget/form.go +++ b/widget/form.go @@ -256,27 +256,36 @@ func (f *Form) checkValidation(err error) { } func (f *Form) ensureRenderItems() { - done := len(f.itemGrid.Objects) / 2 - if done >= len(f.Items) { - f.itemGrid.Objects = f.itemGrid.Objects[0 : len(f.Items)*2] - return + // Calculate the required capacity based on the number of items in the form + requiredCapacity := len(f.Items) * 2 + + // Pre-allocate capacity if necessary + if cap(f.itemGrid.Objects) < requiredCapacity { + newObjects := make([]fyne.CanvasObject, len(f.itemGrid.Objects), requiredCapacity) + copy(newObjects, f.itemGrid.Objects) + f.itemGrid.Objects = newObjects } - adding := len(f.Items) - done - objects := make([]fyne.CanvasObject, adding*2) - off := 0 + // Adjust the length to match the number of items (each with label and widget) + f.itemGrid.Objects = f.itemGrid.Objects[:requiredCapacity] + for i, item := range f.Items { - if i < done { - continue + labelIndex := i * 2 + widgetIndex := labelIndex + 1 + + // Update or create label for the item + if labelIndex < len(f.itemGrid.Objects) && f.itemGrid.Objects[labelIndex] != nil { + f.itemGrid.Objects[labelIndex] = f.createLabel(item.Text) + } else { + f.itemGrid.Objects[labelIndex] = f.createLabel(item.Text) } - objects[off] = f.createLabel(item.Text) - off++ f.setUpValidation(item.Widget, i) - objects[off] = f.createInput(item) - off++ + f.itemGrid.Objects[widgetIndex] = f.createInput(item) } - f.itemGrid.Objects = append(f.itemGrid.Objects, objects...) + + // Refresh the grid to apply changes + f.itemGrid.Refresh() } func (f *Form) isVertical() bool { diff --git a/widget/form_test.go b/widget/form_test.go index c79742ba6c..99ccfdc9c7 100644 --- a/widget/form_test.go +++ b/widget/form_test.go @@ -436,3 +436,28 @@ func TestForm_RefreshFromStructInit(t *testing.T) { }) } + +func TestEnsureRenderItemsCapacity(t *testing.T) { + form := &Form{ + Items: []*FormItem{ + {Text: "Label1", Widget: NewEntry()}, + {Text: "Label2", Widget: NewCheck("Check", nil)}, + }, + itemGrid: &fyne.Container{ + Objects: []fyne.CanvasObject{}, + }, + } + + // Call ensureRenderItems + form.ensureRenderItems() + + // Check that the capacity is sufficient + if cap(form.itemGrid.Objects) < len(form.Items)*2 { + t.Errorf("Expected capacity >= %d, got %d", len(form.Items)*2, cap(form.itemGrid.Objects)) + } + + // Check that objects are updated correctly + if len(form.itemGrid.Objects) != len(form.Items)*2 { + t.Errorf("Expected %d objects, got %d", len(form.Items)*2, len(form.itemGrid.Objects)) + } +}