Add a method for fetching the main window when it might not exist #874
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
orApp::main_window
to return anOption
. Changingmain_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. Changingwindow_id
would require us to check whether thefocused_window
still existed inwindows
. 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 ifApp::window_id
exist inApp::window_ids
. That would work since it looks likeApp::focused_window
is set the first time a window is created. In that case I think the docs ofApp::window_id
should be updated to reflect that this is something that is guaranteed.