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

[bug] Main window onCloseRequested is not triggered when the window is closed via APIs #5288

Open
CrendKing opened this issue Sep 28, 2022 · 2 comments
Labels
scope: core Core packages of Tauri status: backlog Issue is ready and we can work on it type: bug

Comments

@CrendKing
Copy link

CrendKing commented Sep 28, 2022

Describe the bug

It seems the main window's onCloseRequested is correctly triggered when user closes the app with top right X button or Alt+F4, but not triggered via APIs such as appWindow.close() or exit().

Reproduction

  1. Create a Svelte component or anything equivalent:
<script lang="ts">
    appWindow.onCloseRequested(async () => {
        await invoke('test')
    })
</script>

<button on:click={() => appWindow.close()}>Test</button>
  1. Click the test button
  2. Confirm that the "test" command is not triggered.
  3. However, close the app with Alt+F4. Observe "test" command is triggered.

Expected behavior

Closing the app or main window via API should also trigger onCloseRequested.

Platform and versions

Environment
  › OS: Windows 10.0.22621 X64
  › Webview2: 105.0.1343.42
  › MSVC: 
      - Visual Studio Community 2022
  › Node.js: 16.17.1
  › npm: 8.15.0
  › pnpm: 7.12.2
  › yarn: Not installed!
  › rustup: 1.25.1
  › rustc: 1.64.0
  › cargo: 1.64.0
  › Rust toolchain: stable-x86_64-pc-windows-msvc 

Packages
  › @tauri-apps/cli [NPM]: 1.1.1
  › @tauri-apps/api [NPM]: 1.1.0
  › tauri [RUST]: 1.1.1,
  › tauri-build [RUST]: 1.1.1,
  › tao [RUST]: 0.14.0,
  › wry [RUST]: 0.21.1,

App
  › build-type: build
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:5173/
  › framework: Svelte

App directory structure
  ├─ .git
  ├─ dist
  ├─ node_modules
  ├─ src
  └─ src-tauri

Stack trace

No response

Additional context

This is needed so that the app can do necessary clean up and save data to file regardless of how user initiates the app termination. Otherwise, we have to duplicate the clean-up logic to all the possible routes.

@CrendKing CrendKing added status: needs triage This issue needs to triage, applied to new issues type: bug labels Sep 28, 2022
@amrbashir
Copy link
Member

For exit(), it is a desired behavior since it is meant to close the process as soon as possible. If you want to run code before that, you can just run the code before calling it.

For window.close() This is a known behavior for v1 but is planned to be changed in v2 where there will be window.close() that triggers the event and window.destroy() that will bypass it.

@amrbashir amrbashir added scope: core Core packages of Tauri status: backlog Issue is ready and we can work on it status: needs triage This issue needs to triage, applied to new issues and removed status: needs triage This issue needs to triage, applied to new issues labels Sep 28, 2022
@CrendKing
Copy link
Author

Thanks for clarification. If we can explain this (especially exit() at the moment) in doc that will help clear up the confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: core Core packages of Tauri status: backlog Issue is ready and we can work on it type: bug
Projects
Status: 📬Proposal
Development

No branches or pull requests

2 participants