Can't get List.FocusGained to work... #5995
-
|
Hello, package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
type MyList struct {
widget.List
}
func newMyList(length func() int, createItem func() fyne.CanvasObject, updateItem func(id widget.ListItemID, item fyne.CanvasObject)) *MyList {
l := &MyList{}
l.Length = length
l.CreateItem = createItem
l.UpdateItem = updateItem
l.ExtendBaseWidget(l)
return l
}
func (l *MyList) FocusGained() {
l.List.FocusGained()
fmt.Println("FocusGained")
}
func main() {
app := app.New()
win := app.NewWindow("FOCUS")
desk, ok := app.Driver().(desktop.Driver)
if !ok {
panic("could not get desktop-driver for app!")
}
data := []string{"one", "two", "three"}
l := newMyList(
func() int {
return len(data)
},
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(id widget.ListItemID, o fyne.CanvasObject) {
o.(*widget.Label).SetText(data[id])
},
)
l.OnSelected = func(id widget.ListItemID) {
modifiers := desk.CurrentKeyModifiers()
fmt.Println("Selected ", data[id], modifiers)
}
c := container.NewBorder(nil, nil, nil, nil,l,)
win.SetContent(c)
win.CenterOnScreen()
win.Resize(fyne.NewSize(1024, 760))
win.ShowAndRun()
}Currently the list "works"; it does the println in OnSelelected() but the println in FocusGained() never gets called... I think I am missing something fundamental here :-).. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
|
How are you focusing the list to determine whether or not the focus callbacks are working? |
Beta Was this translation helpful? Give feedback.
-
|
It seems to relate to the extended list somehow - it works on a basic list but when extended the tap won't focus it, but then tapping "Tab" will (not the expected behaviour) |
Beta Was this translation helpful? Give feedback.
-
|
Yes, you have found a bug in I think this fixes it (we will need to check other collection widgets too) --- a/widget/list.go
+++ b/widget/list.go
@@ -697,9 +697,9 @@ func (l *listLayout) setupListItem(li *listItem, id ListItemID, focus bool) {
}
li.onTapped = func() {
if !fyne.CurrentDevice().IsMobile() {
- canvas := fyne.CurrentApp().Driver().CanvasForObject(l.list)
+ canvas := fyne.CurrentApp().Driver().CanvasForObject(l.list.super())
if canvas != nil {
- canvas.Focus(l.list.impl.(fyne.Focusable))
+ canvas.Focus(l.list.super().(fyne.Focusable))
} |
Beta Was this translation helpful? Give feedback.
-
|
Moved to issue #5997 |
Beta Was this translation helpful? Give feedback.
I guess you are looking for the recent
List.OnHighlightedcallback #5961 - you can use this by upgrading to versiondevelopor using the SHA in the version instead.