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

Add a method for fetching the main window when it might not exist #874

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Frojdholm
Copy link
Contributor

@Frojdholm Frojdholm commented Jul 9, 2022

I was trying to work with code similar to the draw_captur_hi_res.rs example that rendered to a texture, but my code was crashing when I was closing the nannou window using the Windows "X" button. It turns out that it was panicing in the exit function trying to fetch the main window which seems to have closed before the exit function had a chance to run.

I'm not sure if this is the correct solution. Ideally I would change the API of either App::window_id or App::main_window to return an Option. Changing main_window would likely not be great since it would add a lot of boilerplate to the more common case of accessing it outside of the exit function. Changing window_id would require us to check whether the focused_window still existed in windows. Both of these would be breaking API changes, which is why I choose to implement new methods.

Also in my particular case it would be enough to check if window_count was 0, though that feels a bit brittle in the general case. Additionally, a real workaround would be to check if App::window_id exist in App::window_ids. That would work since it looks like App::focused_window is set the first time a window is created. In that case I think the docs of App::window_id should be updated to reflect that this is something that is guaranteed.

Previously, if the main window had been closed, there was no way of
trying to access the main window without panicing. There was also no
safe way to check if the main window had been closed since that would
require fetching the focused window id which might not exist, thus
panicing.

With these new methods it's possible to easily handle the case when
there is no main window or the main window has been closed, using the
App::try_main_window method. App::try_window_id is added for
completeness, but is not strictly necessary since if there has been at
least one window the focused window should be set.
If the main window was closed using the X, the window might not exist in
the exit function, causing a panic. Use the new method which returns an
Option to handle that case.
@Frojdholm
Copy link
Contributor Author

Seem to be a little bit connected to #743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant