From 8050e24bb228a788e4f6fa8359853e6f41c0e2cb Mon Sep 17 00:00:00 2001 From: csturiale Date: Tue, 17 Dec 2024 17:07:31 +0100 Subject: [PATCH 1/4] add OnScroll event to list --- widget/list.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/widget/list.go b/widget/list.go index 872e8b4022..11e9d13c6a 100644 --- a/widget/list.go +++ b/widget/list.go @@ -37,6 +37,7 @@ type List struct { UpdateItem func(id ListItemID, item fyne.CanvasObject) `json:"-"` OnSelected func(id ListItemID) `json:"-"` OnUnselected func(id ListItemID) `json:"-"` + OnScroll func(id ListItemID) `json:"-"` // HideSeparators hides the separators between list rows // @@ -186,6 +187,11 @@ func (l *List) scrollTo(id ListItemID) { l.scroller.Offset.Y = y + lastItemHeight - l.scroller.Size().Height } l.offsetUpdated(l.scroller.Offset) + defer func() { + if f := l.OnScroll; f != nil { + f(id) + } + }() } // Resize is called when this list should change size. We refresh to ensure invisible items are drawn. From 666ff119b0c6bb3ff475cad4368aeabf7970d762 Mon Sep 17 00:00:00 2001 From: csturiale Date: Tue, 17 Dec 2024 18:24:17 +0100 Subject: [PATCH 2/4] add OnFocus event to list --- widget/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/widget/list.go b/widget/list.go index 11e9d13c6a..2fe4b2d4ef 100644 --- a/widget/list.go +++ b/widget/list.go @@ -37,7 +37,7 @@ type List struct { UpdateItem func(id ListItemID, item fyne.CanvasObject) `json:"-"` OnSelected func(id ListItemID) `json:"-"` OnUnselected func(id ListItemID) `json:"-"` - OnScroll func(id ListItemID) `json:"-"` + OnFocus func(id ListItemID) `json:"-"` // HideSeparators hides the separators between list rows // @@ -188,7 +188,7 @@ func (l *List) scrollTo(id ListItemID) { } l.offsetUpdated(l.scroller.Offset) defer func() { - if f := l.OnScroll; f != nil { + if f := l.OnFocus; f != nil { f(id) } }() From e66ed12ff1f05109f84260f88cfee3873005e2ef Mon Sep 17 00:00:00 2001 From: csturiale Date: Tue, 17 Dec 2024 20:31:51 +0100 Subject: [PATCH 3/4] add OnFocused event to list --- widget/list.go | 21 +++++++++++++++------ widget/list_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/widget/list.go b/widget/list.go index 2fe4b2d4ef..daf34038c4 100644 --- a/widget/list.go +++ b/widget/list.go @@ -37,7 +37,11 @@ type List struct { UpdateItem func(id ListItemID, item fyne.CanvasObject) `json:"-"` OnSelected func(id ListItemID) `json:"-"` OnUnselected func(id ListItemID) `json:"-"` - OnFocus 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 // @@ -108,6 +112,7 @@ 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. @@ -115,6 +120,7 @@ func (l *List) FocusGained() { // Implements: fyne.Focusable func (l *List) FocusLost() { l.focused = false + l.onFocused(-1) l.RefreshItem(l.currentFocus) } @@ -187,11 +193,6 @@ func (l *List) scrollTo(id ListItemID) { l.scroller.Offset.Y = y + lastItemHeight - l.scroller.Size().Height } l.offsetUpdated(l.scroller.Offset) - defer func() { - if f := l.OnFocus; f != nil { - f(id) - } - }() } // Resize is called when this list should change size. We refresh to ensure invisible items are drawn. @@ -311,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: @@ -319,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) } @@ -865,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) + } +} diff --git a/widget/list_test.go b/widget/list_test.go index 45cd0edbce..2d81793804 100644 --- a/widget/list_test.go +++ b/widget/list_test.go @@ -75,6 +75,30 @@ func TestList_Resize(t *testing.T) { assert.True(t, resizeCountAllGreaterOrEqual) } +func TestList_OnFocused(t *testing.T) { + l, _, rows := setupList(t) + var selectedId widget.ListItemID + 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 { + l.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown}) + assert.Equal(t, selectedId, index+1) + } + for index, _ := range rows { + 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) @@ -101,6 +125,7 @@ type resizeRefreshCountingLabel struct { widget.Label resizeCount int refreshCount int + focusedCount int } func newResizeRefreshCountingLabel(text string) *resizeRefreshCountingLabel { @@ -119,3 +144,7 @@ func (r *resizeRefreshCountingLabel) Resize(s fyne.Size) { r.resizeCount++ r.Label.Resize(s) } + +func (r *resizeRefreshCountingLabel) OnFocused() { + r.focusedCount++ +} From 740fc36044da24d7b01c79b5daee3cc93be60eaa Mon Sep 17 00:00:00 2001 From: csturiale Date: Tue, 17 Dec 2024 20:33:43 +0100 Subject: [PATCH 4/4] removed unused field in list_test --- widget/list_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/widget/list_test.go b/widget/list_test.go index 2d81793804..27e43cc822 100644 --- a/widget/list_test.go +++ b/widget/list_test.go @@ -125,7 +125,6 @@ type resizeRefreshCountingLabel struct { widget.Label resizeCount int refreshCount int - focusedCount int } func newResizeRefreshCountingLabel(text string) *resizeRefreshCountingLabel { @@ -144,7 +143,3 @@ func (r *resizeRefreshCountingLabel) Resize(s fyne.Size) { r.resizeCount++ r.Label.Resize(s) } - -func (r *resizeRefreshCountingLabel) OnFocused() { - r.focusedCount++ -}