nannou-org / nannou

A Creative Coding Framework for Rust.
https://nannou.cc/
6.05k stars 306 forks source link

Nannou application exits immediately on cmd-Q without invoking custom exit handlers #932

Open jukkae opened 1 year ago

jukkae commented 1 year ago

Issue Description When using nannou for developing applications on macOS, I've observed that pressing cmd-Q causes the application to exit immediately. It appears that the window close request event might be handled before nannou's own event loop gets the chance, thus bypassing any custom exit handlers I've set up.

Steps to Reproduce:

  1. Set up a basic nannou application.
  2. Implement a custom exit handler by implementing Drop for Model.
  3. Run the application on macOS.
  4. Sanity check: press esc to verify that the exit handler does run when exiting with esc.
  5. Press cmd-Q to quit the application. The exit handler does not run in this situation.

Here’s a minimal example:

use nannou::prelude::*;

fn main() {
    nannou::app(model)
        .update(update)
        .simple_window(view)
        .run();
}

struct Model {}

impl Drop for Model {
    fn drop(&mut self) {
        println!("saving some important state!");
    }
}

fn model(_app: &App) -> Model {
    Model {}
}

fn update(_app: &App, _model: &mut Model, _update: Update) {
}

fn view(_app: &App, _model: &Model, frame: Frame){
    frame.clear(PURPLE);
}

Expected Behavior: The application should run the custom exit handler before closing, printing “saving some important state!” to console. Actual Behavior: The application exits immediately without running the custom exit handler.

Potential Impact: For applications that need to perform critical cleanup tasks or save state before exiting, this behavior could lead to data loss or other unintended side effects. I’d like to use nannou to build a kind of an editor application that auto-saves the project on exit, but not being able to override cmd-Q behavior is a major problem for my usecase.

Suggested Solution: It would be beneficial if nannou could expose or forward the window close request event to the application before actually closing it, allowing developers to decide how to handle this situation.