Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changes to widget.Form.Items not being reflected on refresh #5308

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
coder-in-go marked this conversation as resolved.
Show resolved Hide resolved
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] = item.Widget
coder-in-go marked this conversation as resolved.
Show resolved Hide resolved
}
f.itemGrid.Objects = append(f.itemGrid.Objects, objects...)

// Refresh the grid to apply changes
f.itemGrid.Refresh()
}

func (f *Form) isVertical() bool {
Expand Down
25 changes: 25 additions & 0 deletions widget/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Loading