Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/process-exit-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri': 'minor:feat'
'@tauri-apps/api': 'minor:feat'
---

Expose app exit as a JS process API backed by the core app exit command.
1 change: 1 addition & 0 deletions crates/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
("identifier", true),
("app_show", false),
("app_hide", false),
("exit", false),
("fetch_data_store_identifiers", false),
("remove_data_store", false),
("default_window_icon", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import android.app.Activity
import android.webkit.WebView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import app.tauri.annotation.Command
import app.tauri.annotation.TauriPlugin
import app.tauri.plugin.Plugin
import app.tauri.plugin.Invoke
import app.tauri.plugin.JSObject

@TauriPlugin
Expand Down Expand Up @@ -46,9 +44,4 @@ class AppPlugin(private val activity: Activity): Plugin(activity) {
(activity as AppCompatActivity).onBackPressedDispatcher.addCallback(activity, callback)
}

@Command
fun exit(invoke: Invoke) {
invoke.resolve()
activity.finish()
}
}
26 changes: 26 additions & 0 deletions crates/tauri/permissions/app/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@ Denies the default_window_icon command without any pre-configured scope.
<tr>
<td>

`core:app:allow-exit`

</td>
<td>

Enables the exit command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`core:app:deny-exit`

</td>
<td>

Denies the exit command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`core:app:allow-fetch-data-store-identifiers`

</td>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions crates/tauri/src/app/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ pub fn app_hide<R: Runtime>(app: AppHandle<R>) -> crate::Result<()> {
Ok(())
}

#[command(root = "crate")]
pub fn exit<R: Runtime>(app: AppHandle<R>, exit_code: Option<i32>) {
app.exit(exit_code.unwrap_or(0));
}

#[command(root = "crate")]
#[allow(unused_variables)]
pub async fn fetch_data_store_identifiers<R: Runtime>(
Expand Down Expand Up @@ -125,6 +130,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
identifier,
app_show,
app_hide,
exit,
fetch_data_store_identifiers,
remove_data_store,
default_window_icon,
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import * as image from './image'
import * as menu from './menu'
import * as mocks from './mocks'
import * as path from './path'
import * as process from './process'
import * as tray from './tray'
import * as webview from './webview'
import * as webviewWindow from './webviewWindow'
Expand All @@ -45,6 +46,7 @@ export {
menu,
mocks,
path,
process,
tray,
webview,
webviewWindow,
Expand Down
31 changes: 31 additions & 0 deletions packages/api/src/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

import { invoke } from './core'

/**
* Process-related APIs.
*
* @module
*/

/**
* Exits the app.
*
* @param exitCode - The exit code to use. Defaults to `0`.
*
* @example
* ```typescript
* import { exit } from '@tauri-apps/api/process';
* await exit();
* ```
*
* @since 2.10.0
*/
async function exit(exitCode?: number): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to app (packages/api/src/app.ts)

const payload = exitCode === undefined ? {} : { exitCode }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could put the default value in the js side? (e.g. { exitCode: exitCode ?? 0 })

return invoke('plugin:app|exit', payload)
}

export { exit }