Skip to content

List: Reset selection state when length decreases #5875

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

redawl
Copy link
Contributor

@redawl redawl commented Aug 2, 2025

Description:

Fixes #5852

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

@coveralls
Copy link

Coverage Status

coverage: 62.404% (+0.02%) from 62.388%
when pulling 778372b on redawl:list_selection_fix
into 3ee02e8 on fyne-io:develop.

@@ -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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants