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

Canvas.Focus() before window visible causes application to crash #1893

Closed
ababo opened this issue Feb 2, 2021 · 10 comments
Closed

Canvas.Focus() before window visible causes application to crash #1893

ababo opened this issue Feb 2, 2021 · 10 comments
Assignees
Labels
blocker Items that would block a forthcoming release bug Something isn't working

Comments

@ababo
Copy link

ababo commented Feb 2, 2021

Here is a part of the related stack trace:

2021/02/02 11:36:42 panic was raised: runtime error: invalid memory address or nil pointer dereference, stack: goroutine 27 [running]:
panic(0x46fac20, 0x536b050)
	/usr/local/Cellar/go/1.14.1/libexec/src/runtime/panic.go:967 +0x166
fyne.io/fyne/v2/internal/app.(*FocusManager).Focus(0x0, 0x4c62cc0, 0xc0007f2900, 0x0)
	/Users/ababo/go/pkg/mod/fyne.io/fyne/v2@v2.0.0/internal/app/focus_manager.go:25 +0x4f
fyne.io/fyne/v2/internal/driver/glfw.(*glCanvas).Focus(0xc0002a8100, 0x4c62cc0, 0xc0007f2900)
	/Users/ababo/go/pkg/mod/fyne.io/fyne/v2@v2.0.0/internal/driver/glfw/canvas.go:88 +0x1a8

This happens when trying to focus on widget.Entry or a custom control based on widget.List.

Platform: OS X 11.1
Go version: go1.14.1 darwin/amd64
Fyne version: v2.0.0

@ababo ababo added the bug Something isn't working label Feb 2, 2021
@ababo ababo changed the title Canvas.Focus() cause application to crash Canvas.Focus() causes application to crash Feb 2, 2021
@andydotxyz
Copy link
Member

It looks like the FocusManager is nil somehow.
Please can you share the code as there is not enough information here.

@andydotxyz andydotxyz added the question A question has been asked label Feb 2, 2021
@ababo
Copy link
Author

ababo commented Feb 2, 2021

func createUserpassLoginPageContent(cg *util.CancelGroup,
	data *pageData, finished chan<- *pageError) fyne.CanvasObject {
	...	
	username := widget.NewEntry()
	...
	form := fyne.NewContainerWithLayout(layout.NewGridWrapLayout(formSize),
		widget.NewForm(
			widget.NewFormItem("Username", username),
			widget.NewFormItem("Password", password),
		),
	)

	control := fynecont.NewHBox(
		fynex.NewHorizontalMargin(loginPageSubmitMargin),
		fynecont.NewVBox(
			submit,
			progress.CanvasObject(),
		),
	)

	data.container.Canvas().Focus(username) <--- This line causes the crash.
	username.DoubleTapped(nil)

	return fynecont.NewVBox(
		newPageTitle(loginTitle),
		newRichText(loginText),
		fynex.NewVerticalMargin(loginPageFormMargin),
		fynecont.NewHBox(
			fynex.NewHorizontalMargin(loginPageFormMargin),
			fynecont.NewVBox(
				form,
				control,
			),
		),
		loggedInIndicator,
	)
}

@andydotxyz
Copy link
Member

Ah. I see. I think this is caused because you are focusing an element that is not in the canvas (yet).
You raise an interesting point though, how do we handle the case where developers want to focus something they just created.

@ababo
Copy link
Author

ababo commented Feb 2, 2021

This worked before v2.0.0.

@AlbinoGeek
Copy link
Contributor

I thought this was just me, haha.

@andydotxyz
Copy link
Member

This worked before v2.0.0.

Ah, this would have been useful information for the initial bug report, but thanks.
If the crash is new, did it actually do what was expected in 1.4? I can't quite figure out how it could have worked so keen for more info.

@andydotxyz andydotxyz changed the title Canvas.Focus() causes application to crash Canvas.Focus() before window visible causes application to crash Feb 4, 2021
andydotxyz added a commit that referenced this issue Feb 4, 2021
@andydotxyz andydotxyz added the blocker Items that would block a forthcoming release label Feb 4, 2021
@andydotxyz andydotxyz self-assigned this Feb 9, 2021
@andydotxyz andydotxyz removed the question A question has been asked label Feb 12, 2021
@andydotxyz
Copy link
Member

We are fixing the crash but returning to the 2.0 behaviour whereby an object must be in the tree before you can focus it.

@ababo
Copy link
Author

ababo commented Feb 13, 2021

The problem starts when you develop code that return a complex canvas object that presumes focusing. The returned object isn't aware when it will be attached to the canvas tree. This constraint will frequently force developers to add deferred focusing which will lead to fragile code.

@andydotxyz
Copy link
Member

I agree that there are some areas that are going to need work.
Perhaps we can open a feature request for "request future focus" - this way the element could get focus when it is added.
That will be safe whereas doing it the way I had in the original PR is unsafe.

No object should assume it has focus - it will be informed when it has gained the window focus.

andydotxyz added a commit that referenced this issue Feb 20, 2021
@andydotxyz
Copy link
Member

On develop and will be in 2.0.1 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker Items that would block a forthcoming release bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants