窗口状态
保存窗口位置和大小,并在应用重新打开时恢复它们。
此插件需要 Rust 版本至少为 **1.77.2**
| 平台 | 级别 | 备注 |
|---|---|---|
| Windows | ||
| Linux | ||
| macOS | ||
| Android | | |
| iOS | |
安装 window-state 插件以开始使用。
使用你的项目包管理器添加依赖项
npm run tauri add window-stateyarn run tauri add window-statepnpm tauri add window-statedeno task tauri add window-statebun tauri add window-statecargo tauri add window-state-
在
src-tauri文件夹中运行以下命令,将插件添加到项目的Cargo.toml依赖项中cargo add tauri-plugin-window-state --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))' -
修改
lib.rs以初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_window_state::Builder::default().build());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您首选的 JavaScript 包管理器安装 JavaScript 访客绑定
npm install @tauri-apps/plugin-window-stateyarn add @tauri-apps/plugin-window-statepnpm add @tauri-apps/plugin-window-statedeno add npm:@tauri-apps/plugin-window-statebun add @tauri-apps/plugin-window-state
添加窗口状态插件后,所有窗口在应用关闭时都会记住其状态,并在下次启动时恢复到以前的状态。
您也可以在 JavaScript 和 Rust 中访问窗口状态插件。
您可以使用 saveWindowState 手动保存窗口状态
import { saveWindowState, StateFlags } from '@tauri-apps/plugin-window-state';// when using `"withGlobalTauri": true`, you may use// const { saveWindowState, StateFlags } = window.__TAURI__.windowState;
saveWindowState(StateFlags.ALL);同样,您也可以从磁盘手动恢复窗口状态
import { restoreStateCurrent, StateFlags,} from '@tauri-apps/plugin-window-state';// when using `"withGlobalTauri": true`, you may use// const { restoreStateCurrent, StateFlags } = window.__TAURI__.windowState;
restoreStateCurrent(StateFlags.ALL);您可以使用 AppHandleExt trait 暴露的 save_window_state() 方法
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
// `tauri::AppHandle` now has the following additional methodapp.save_window_state(StateFlags::all()); // will save the state of all open windows to disk同样,您可以使用 WindowExt trait 暴露的 restore_state() 方法从磁盘手动恢复窗口的状态
use tauri_plugin_window_state::{WindowExt, StateFlags};
// all `Window` types now have the following additional methodwindow.restore_state(StateFlags::all()); // will restore the window's state from disk默认情况下,所有潜在危险的插件命令和范围都被阻止,无法访问。您必须修改 capabilities 配置中的权限才能启用这些功能。
有关更多信息,请参阅功能概述,并参阅分步指南以使用插件权限。
{ "permissions": [ ..., "window-state:default", ]}默认权限
此权限集配置了窗口状态插件可用的操作类型。
已授予权限
默认情况下,所有操作均已启用。
此默认权限集包括以下内容
allow-filenameallow-restore-stateallow-save-window-state
权限表
| 标识符 | 描述 |
|---|---|
|
|
启用 filename 命令,不带任何预配置范围。 |
|
|
禁用 filename 命令,不带任何预配置范围。 |
|
|
启用 restore_state 命令,不带任何预配置范围。 |
|
|
禁用 restore_state 命令,不带任何预配置范围。 |
|
|
启用 save_window_state 命令,不带任何预配置范围。 |
|
|
禁用 save_window_state 命令,不带任何预配置范围。 |
© 2025 Tauri 贡献者。CC-BY / MIT