跳转到内容
Tauri

窗口状态

保存窗口位置和大小,并在应用程序重新打开时恢复它们。

支持的平台

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

设置

请安装窗口状态插件。

使用项目的包管理器来添加依赖。

npm run tauri add window-state

用法

添加窗口状态插件后,应用程序关闭时所有窗口的状态都会被记住,并且在下次启动时会恢复到之前的状态。

你也可以在 JavaScript 和 Rust 中访问窗口状态插件。

JavaScript

你可以使用 saveWindowState 手动保存窗口状态:

import { saveWindowState, StateFlags } from '@tauri-apps/plugin-window-state';
saveWindowState(StateFlags.ALL);

同样,你可以手动从磁盘恢复窗口的状态:

import {
restoreStateCurrent,
StateFlags,
} from '@tauri-apps/plugin-window-state';
restoreStateCurrent(StateFlags.ALL);

Rust

你可以使用由 AppHandleExt 特征暴露的 save_window_state() 方法:

use tauri_plugin_window_state::{AppHandleExt, StateFlags};
// `tauri::AppHandle` 现在有了以下额外的方法
app.save_window_state(StateFlags::all()); // 将所有打开窗口的状态保存到磁盘

同样,你可以使用 WindowExt 特征暴露的 restore_state() 方法从磁盘手动恢复窗口的状态:

use tauri_plugin_window_state::{WindowExt, StateFlags};
// 所有的 `Window` 类型现在都有以下额外的方法
window.restore_state(StateFlags::all()); // 将从磁盘恢复窗口的状态

权限

默认情况下,所有具有潜在危险的插件命令和范围都会被阻止且无法访问。您必须修改 capabilities 文件夹中的配置来启用它们。

参见能力概览以获取更多信息,以及插件的分步导览来调整插件权限。

src-tauri/capabilities/default.json
{
"permissions": [
...,
"window-state:default",
]
}

Default Permission

This permission set configures what kind of operations are available from the window state plugin.

Granted Permissions

All operations are enabled by default.

This default permission set includes the following:

  • allow-filename
  • allow-restore-state
  • allow-save-window-state

Permission Table

Identifier Description

window-state:allow-filename

Enables the filename command without any pre-configured scope.

window-state:deny-filename

Denies the filename command without any pre-configured scope.

window-state:allow-restore-state

Enables the restore_state command without any pre-configured scope.

window-state:deny-restore-state

Denies the restore_state command without any pre-configured scope.

window-state:allow-save-window-state

Enables the save_window_state command without any pre-configured scope.

window-state:deny-save-window-state

Denies the save_window_state command without any pre-configured scope.


© 2025 Tauri Contributors. CC-BY / MIT