diff --git a/widget/gridwrap_test.go b/widget/gridwrap_test.go index 8a536957f3..2dab860f3e 100644 --- a/widget/gridwrap_test.go +++ b/widget/gridwrap_test.go @@ -3,11 +3,61 @@ package widget import ( "testing" + "github.com/stretchr/testify/assert" + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/test" "fyne.io/fyne/v2/theme" - "github.com/stretchr/testify/assert" ) +func TestGridWrap_Focus(t *testing.T) { + defer test.NewApp() + list := createGridWrap(100) + window := test.NewWindow(list) + defer window.Close() + window.Resize(list.MinSize().Max(fyne.NewSize(150, 200))) + + canvas := window.Canvas().(test.WindowlessCanvas) + assert.Nil(t, canvas.Focused()) + + canvas.FocusNext() + assert.NotNil(t, canvas.Focused()) + assert.Equal(t, 0, canvas.Focused().(*GridWrap).currentFocus) + + children := list.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout).children + assert.True(t, children[0].(*gridWrapItem).hovered) + assert.False(t, children[1].(*gridWrapItem).hovered) + assert.False(t, children[6].(*gridWrapItem).hovered) + assert.False(t, children[7].(*gridWrapItem).hovered) + + list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown}) + assert.False(t, children[0].(*gridWrapItem).hovered) + assert.False(t, children[1].(*gridWrapItem).hovered) + assert.True(t, children[6].(*gridWrapItem).hovered) + assert.False(t, children[7].(*gridWrapItem).hovered) + + list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyRight}) + assert.False(t, children[0].(*gridWrapItem).hovered) + assert.False(t, children[1].(*gridWrapItem).hovered) + assert.False(t, children[6].(*gridWrapItem).hovered) + assert.True(t, children[7].(*gridWrapItem).hovered) + + list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyLeft}) + assert.False(t, children[0].(*gridWrapItem).hovered) + assert.False(t, children[1].(*gridWrapItem).hovered) + assert.True(t, children[6].(*gridWrapItem).hovered) + assert.False(t, children[7].(*gridWrapItem).hovered) + + list.TypedKey(&fyne.KeyEvent{Name: fyne.KeyUp}) + assert.True(t, children[0].(*gridWrapItem).hovered) + assert.False(t, children[1].(*gridWrapItem).hovered) + assert.False(t, children[6].(*gridWrapItem).hovered) + assert.False(t, children[7].(*gridWrapItem).hovered) + + canvas.Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace}) + assert.True(t, children[0].(*gridWrapItem).selected) +} + func TestGridWrap_New(t *testing.T) { g := createGridWrap(1000) template := NewIcon(theme.AccountIcon()) diff --git a/widget/tree.go b/widget/tree.go index 71ff63e065..d8b8b0adbc 100644 --- a/widget/tree.go +++ b/widget/tree.go @@ -134,6 +134,14 @@ func (t *Tree) IsBranchOpen(uid TreeNodeID) bool { // // Implements: fyne.Focusable func (t *Tree) FocusGained() { + if t.currentFocus == "" { + if childUIDs := t.ChildUIDs; childUIDs != nil { + if ids := childUIDs(""); len(ids) > 0 { + t.currentFocus = ids[0] + } + } + } + t.focused = true t.ScrollTo(t.currentFocus) t.Refresh() // TODO RefreshItem(t.currentFocus) diff --git a/widget/tree_internal_test.go b/widget/tree_internal_test.go index 35eea7fd42..fb98b9b6cd 100644 --- a/widget/tree_internal_test.go +++ b/widget/tree_internal_test.go @@ -161,6 +161,39 @@ func TestTree(t *testing.T) { }) } +func TestTree_Focus(t *testing.T) { + var treeData = map[string][]string{ + "": {"foo", "bar"}, + "foo": {"foobar", "barbar"}, + } + tree := NewTreeWithStrings(treeData) + window := test.NewWindow(tree) + defer window.Close() + window.Resize(tree.MinSize().Max(fyne.NewSize(150, 200))) + + canvas := window.Canvas().(test.WindowlessCanvas) + assert.Nil(t, canvas.Focused()) + + canvas.FocusNext() + assert.NotNil(t, canvas.Focused()) + assert.Equal(t, "foo", canvas.Focused().(*Tree).currentFocus) + + tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyDown}) + assert.Equal(t, "bar", canvas.Focused().(*Tree).currentFocus) + + tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyUp}) + assert.Equal(t, "foo", canvas.Focused().(*Tree).currentFocus) + + tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyRight}) + assert.Equal(t, "foobar", canvas.Focused().(*Tree).currentFocus) + + tree.TypedKey(&fyne.KeyEvent{Name: fyne.KeyLeft}) + assert.Equal(t, "foo", canvas.Focused().(*Tree).currentFocus) + + canvas.Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace}) + assert.Equal(t, "foo", tree.selected[0]) +} + func TestTree_Indentation(t *testing.T) { data := make(map[string][]string) tree := NewTreeWithStrings(data)