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

add OnScroll event to list #5329

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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 @@ -37,6 +37,11 @@ type List struct {
UpdateItem func(id ListItemID, item fyne.CanvasObject) `json:"-"`
OnSelected func(id ListItemID) `json:"-"`
OnUnselected func(id ListItemID) `json:"-"`
// OnFocused fire an event when a listItem is focused
// Whenever the list loses focus ListItemId will be equals to -1
//
// Since: 2.6
OnFocused func(id ListItemID) `json:"-"`

// HideSeparators hides the separators between list rows
//
Expand Down Expand Up @@ -107,13 +112,15 @@ func (l *List) FocusGained() {
l.focused = true
l.scrollTo(l.currentFocus)
l.RefreshItem(l.currentFocus)
l.onFocused(l.currentFocus)
}

// FocusLost is called after this List has lost focus.
//
// Implements: fyne.Focusable
func (l *List) FocusLost() {
l.focused = false
l.onFocused(-1)
l.RefreshItem(l.currentFocus)
}

Expand Down Expand Up @@ -305,6 +312,7 @@ func (l *List) TypedKey(event *fyne.KeyEvent) {
}
l.RefreshItem(l.currentFocus)
l.currentFocus++
l.onFocused(l.currentFocus)
l.scrollTo(l.currentFocus)
l.RefreshItem(l.currentFocus)
case fyne.KeyUp:
Expand All @@ -313,6 +321,7 @@ func (l *List) TypedKey(event *fyne.KeyEvent) {
}
l.RefreshItem(l.currentFocus)
l.currentFocus--
l.onFocused(l.currentFocus)
l.scrollTo(l.currentFocus)
l.RefreshItem(l.currentFocus)
}
Expand Down Expand Up @@ -859,3 +868,9 @@ func createItemAndApplyThemeScope(f func() fyne.CanvasObject, scope fyne.Widget)
item.Refresh()
return item
}

func (l *List) onFocused(id ListItemID) {
if f := l.OnFocused; f != nil {
f(id)
}
}
24 changes: 24 additions & 0 deletions widget/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@
assert.True(t, resizeCountAllGreaterOrEqual)
}

func TestList_OnFocused(t *testing.T) {
l, _, rows := setupList(t)
var selectedId widget.ListItemID

Check failure on line 80 in widget/list_test.go

View workflow job for this annotation

GitHub Actions / static_analysis

var selectedId should be selectedID (ST1003)
l.OnFocused = func(id widget.ListItemID) {
selectedId = id
}
l.FocusGained()
assert.Equal(t, selectedId, 0)
l.FocusLost()
assert.Equal(t, selectedId, -1)
l.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown})
assert.Equal(t, selectedId, 1)
l.TypedKey(&fyne.KeyEvent{Name: fyne.KeyUp})
assert.Equal(t, selectedId, 0)
for index, _ := range rows {

Check failure on line 92 in widget/list_test.go

View workflow job for this annotation

GitHub Actions / static_analysis

unnecessary assignment to the blank identifier (S1005)
l.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown})
assert.Equal(t, selectedId, index+1)
}
for index, _ := range rows {

Check failure on line 96 in widget/list_test.go

View workflow job for this annotation

GitHub Actions / static_analysis

unnecessary assignment to the blank identifier (S1005)
l.TypedKey(&fyne.KeyEvent{Name: fyne.KeyUp})
assert.Equal(t, selectedId, len(rows)-(index+1))
}
}

func setupList(t *testing.T) (*widget.List, fyne.Window, []*resizeRefreshCountingLabel) {
var rows []*resizeRefreshCountingLabel
test.NewTempApp(t)
Expand Down
Loading