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

Fix issue with Focus call crashing #1903

Merged
merged 4 commits into from Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable feature, but where is the test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, This could be a different PR :).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added

}
}

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(e)
c := w.Canvas().(*glCanvas)
c.Focus(e)
assert.Equal(t, e, c.Focused())

w.SetContent(e)
andydotxyz marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, e, c.Focused())
}

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