Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,21 @@ func (l *listLayout) updateList(newOnly bool) {
fyne.LogError("Missing UpdateCell callback required for List", nil)
}

if l.list.currentFocus >= length {
l.list.focused = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should look like it lost focus - the actual focus won't have changed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this check, even though we removed the selection when the list shrunk, when the list grows again the previously selected row shows as focused, which is incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree what you describe is not correct - but I'm not sure your solution is either.

If you're editing an Entry and SetText is called to shorter the cursor would move to accommodate but it would not unfocus the entry...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying if there is an entry inside the list row? at least with this code, the entry is properly unfocused as well:

package main

import (
        "fyne.io/fyne/v2"
        "fyne.io/fyne/v2/app"
        "fyne.io/fyne/v2/container"
        "fyne.io/fyne/v2/widget"
)

func main() {
        a := app.New()
        w := a.NewWindow("Scroll Test")
        w.Resize(fyne.NewSize(800, 800))

        items := []string{
                "Item1",
                "Item2",
                "Item3",
        }
        populateList := true
        button := widget.NewButton("Click to toggle list", func() {})

        list := widget.NewList(
                func() int {
                        if populateList {
                                return len(items)
                        }
                        return 0
                },
                func() fyne.CanvasObject {
                        return widget.NewEntry()
                },
                func(lii widget.ListItemID, co fyne.CanvasObject) {
                        co.(*widget.Entry).OnChanged = func(s string) {
                                items[lii] = s
                        }
                        co.(*widget.Entry).SetText(items[lii])
                },
        )

        button.OnTapped = func() {
                populateList = !populateList
                list.Refresh()
        }

        w.SetContent(container.NewBorder(button, nil, nil, nil, list))

        w.ShowAndRun()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying if there is an entry inside the list row?

No.
I was using Entry as an illustration of the focus model. Unless the user focuses another widget the list remains focus and so we shouldn't hide the focus state...

}

if l.list.selected != nil {
index := 0
for _, selected := range l.list.selected {
if selected < length {
l.list.selected[index] = selected
index++
}
}
l.list.selected = l.list.selected[:index]
}

// l.wasVisible now represents the currently visible items, while
// l.visible will be updated to represent what is visible *after* the update
l.wasVisible = append(l.wasVisible, l.visible...)
Expand Down
29 changes: 29 additions & 0 deletions widget/list_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,35 @@ func TestList_ScrollToLargeItem(t *testing.T) {
assert.Equal(t, list.scroller.Content.MinSize().Height-list.Size().Height, list.scroller.Offset.Y)
}

func TestList_SelectionReset(t *testing.T) {
data := []string{
"Test1",
"Test2",
"Test3",
}
list := NewList(
func() int { return len(data) },
func() fyne.CanvasObject { return NewLabel("Templ") },
func(id ListItemID, item fyne.CanvasObject) { item.(*Label).SetText(data[id]) },
)

list.Select(2)
list.Refresh()

assert.Equal(t, len(list.selected), 1)

data = make([]string, 0)
list.Refresh()
assert.Equal(t, len(list.selected), 0)

data = []string{
"Test1",
"Test2",
}
list.Refresh()
assert.Equal(t, len(list.selected), 0)
}

var minSize fyne.Size

func BenchmarkContentMinSize(b *testing.B) {
Expand Down
Loading