nannou icon indicating copy to clipboard operation
nannou copied to clipboard

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

Open Frojdholm opened this issue 2 years ago • 1 comments

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.

Frojdholm avatar Jul 09 '22 21:07 Frojdholm

Seem to be a little bit connected to #743

Frojdholm avatar Jul 09 '22 21:07 Frojdholm