Skip to content

Commit

Permalink
Merge pull request #1903 from fyne-io/fix/1893
Browse files Browse the repository at this point in the history
Fix issue with Focus call crashing
  • Loading branch information
andydotxyz committed Feb 17, 2021
2 parents 1ac1d1d + 30d6fc0 commit 25bc694
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/driver/glfw/canvas.go
Expand Up @@ -84,12 +84,16 @@ func (c *glCanvas) Focus(obj fyne.Focusable) {
c.RUnlock()

for _, mgr := range focusMgrs {
if mgr == nil {
continue
}
if focusMgr != mgr {
if mgr.Focus(obj) {
return
}
}
}

fyne.LogError("Failed to focus object which is not part of the canvas’ content, menu or overlays.", nil)
}

Expand Down Expand Up @@ -452,7 +456,14 @@ func (c *glCanvas) paint(size fyne.Size) {
func (c *glCanvas) setContent(content fyne.CanvasObject) {
c.content = content
c.contentTree = &renderCacheTree{root: &renderCacheNode{obj: c.content}}
var focused fyne.Focusable
if c.contentFocusMgr != nil {
focused = c.contentFocusMgr.Focused() // keep old focus if possible
}
c.contentFocusMgr = app.NewFocusManager(c.content)
if focused != nil {
c.contentFocusMgr.Focus(focused)
}
}

func (c *glCanvas) setDirty(dirty bool) {
Expand Down
21 changes: 21 additions & 0 deletions internal/driver/glfw/canvas_test.go
Expand Up @@ -204,6 +204,27 @@ func TestGlCanvas_Focus(t *testing.T) {
assert.True(t, o2e.focused)
}

func TestGlCanvas_Focus_BeforeVisible(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
e := widget.NewEntry()
c := w.Canvas().(*glCanvas)
c.Focus(e) // this crashed in the past
}

func TestGlCanvas_Focus_SetContent(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
e := widget.NewEntry()
w.SetContent(container.NewHBox(e))
c := w.Canvas().(*glCanvas)
c.Focus(e)
assert.Equal(t, e, c.Focused())

w.SetContent(container.NewVBox(e))
assert.Equal(t, e, c.Focused())
}

func TestGlCanvas_FocusHandlingWhenAddingAndRemovingOverlays(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
Expand Down

0 comments on commit 25bc694

Please sign in to comment.