窗口状态
保存窗口位置和大小,并在应用程序重新打开时恢复它们。
支持的平台
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
yarn run tauri add window-state
pnpm tauri add window-state
deno task tauri add window-state
bun tauri add window-state
cargo tauri add window-state
- 通过将以下内容添加到
Cargo.toml
的文件中来安装插件。
[dependencies]tauri-plugin-window-state = "2.0.0"# 或者使用 Git:tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
- 修改
lib.rs
来初始化插件。
fn main() { tauri::Builder::default() .plugin(tauri_plugin_window_state::Builder::default().build()) .run(tauri::generate_context!()) .expect("error while running tauri application");}
- 使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定。
npm install @tauri-apps/plugin-window-state
yarn add @tauri-apps/plugin-window-state
pnpm add @tauri-apps/plugin-window-state
deno add npm:@tauri-apps/plugin-window-state
bun add @tauri-apps/plugin-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
文件夹中的配置来启用它们。
参见能力概览以获取更多信息,以及插件的分步导览来调整插件权限。
{ "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 |
---|---|
|
Enables the filename command without any pre-configured scope. |
|
Denies the filename command without any pre-configured scope. |
|
Enables the restore_state command without any pre-configured scope. |
|
Denies the restore_state command without any pre-configured scope. |
|
Enables the save_window_state command without any pre-configured scope. |
|
Denies the save_window_state command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT