窗口状态
保存窗口位置和大小,并在应用程序重新打开时恢复它们。
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-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 Guest 绑定。
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';
saveWindowState(StateFlags.ALL);同样,你可以手动从磁盘恢复窗口的状态:
import { restoreStateCurrent, StateFlags,} from '@tauri-apps/plugin-window-state';
restoreStateCurrent(StateFlags.ALL);你可以使用由 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-filenameallow-restore-stateallow-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