Skip to content

[feat] Prevent exit when all windows are closed #13511

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

Open
lars-berger opened this issue May 26, 2025 · 3 comments
Open

[feat] Prevent exit when all windows are closed #13511

lars-berger opened this issue May 26, 2025 · 3 comments

Comments

@lars-berger
Copy link
Contributor

Describe the problem

By default, Tauri exits when all windows have been destroyed.

AFAIK this is preventable in 2 ways:

  1. Call prevent_exit() on RunEvent::ExitRequested events.
    • Downside is that it prevents the process from being killed normally. Users have to force kill the app in task manager/activity monitor.
  2. Prevent window close via prevent_close and hide windows instead on WindowEvent::CloseRequested.
    • Hiding windows comes with side effects and can't be used in all use cases.

Since both approaches come with downsides, it'd be good if there was an intuitive way to keep the process running when all windows are closed.


Source for RunEvent::ExitRequested being called when all windows are closed appears to be here.

Describe the solution you'd like

Add an additional param on RunEvent::ExitRequested event indicating whether it's due to all windows being closed. Currently it's not possible to discern whether the exit request originated from Tauri itself on window close or legitimate user interaction.

Alternatives considered

It's definitely hacky but the best solution I've got for now is to run a empty, hidden window in the background to prevent exit. Would love to have a better way of solving this!

Additional context

No response

@GuoJikun
Copy link

It can be written like this.

tauri::Builder::default()
.build(tauri::generate_context!())
        .expect("error while running tauri application")
        .run(|_app_handle, event| {
            match event {
                tauri::RunEvent::ExitRequested { api, code, .. } => {
                    if code.is_none() {
                        api.prevent_exit();
                    } else {
                        info!("exit code: {:?}", code);
                    }
                }
                _ => {
                    println!("event: {:?}", event);
                }

            }
        });

@lars-berger
Copy link
Contributor Author

It can be written like this.

tauri::Builder::default()
.build(tauri::generate_context!())
        .expect("error while running tauri application")
        .run(|_app_handle, event| {
            match event {
                tauri::RunEvent::ExitRequested { api, code, .. } => {
                    if code.is_none() {
                        api.prevent_exit();
                    } else {
                        info!("exit code: {:?}", code);
                    }
                }
                _ => {
                    println!("event: {:?}", event);
                }

            }
        });

This is actually exactly what we used to have but it interferes with how the process can be killed. On both MacOS and Windows, it prevents the process from being killed normally and you have to "force kill" it to shut it down.

@GuoJikun
Copy link

This is actually exactly what we used to have but it interferes with how the process can be killed. On both MacOS and Windows, it prevents the process from being killed normally and you have to "force kill" it to shut it down.

You're right. I added an "Exit" option to the right-click menu of the tray.

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

No branches or pull requests

2 participants