31 Window State
原文链接: https://tauri.app/plugin/window-state/
保存并恢复窗口位置与尺寸。
支持的平台
| 平台 | 支持程度 | 说明 |
|---|
| Windows | 完整支持 | |
| Linux | 完整支持 | |
| macOS | 完整支持 | |
| Android | 不支持 | |
| iOS | 不支持 | |
设置
自动
使用你的项目包管理器添加依赖:
1
| npm run tauri add window-state
|
1
| yarn run tauri add window-state
|
1
| pnpm tauri add window-state
|
1
| deno task tauri add window-state
|
1
| bun tauri add window-state
|
1
| cargo tauri add window-state
|
手动
在 src-tauri 文件夹中运行以下命令,把插件加入 Cargo.toml 里的项目依赖:
1
| cargo add tauri-plugin-window-state
|
修改 lib.rs 初始化插件:
1
2
3
4
5
6
7
| #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_window_state::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
|
用你偏好的 JavaScript 包管理器安装 JavaScript 端绑定:
1
| npm install @tauri-apps/plugin-window-state
|
1
| yarn add @tauri-apps/plugin-window-state
|
1
| pnpm add @tauri-apps/plugin-window-state
|
1
| deno add npm:@tauri-apps/plugin-window-state
|
1
| bun add @tauri-apps/plugin-window-state
|
用法
加入 window-state 插件后,所有窗口都会在应用关闭时记住自己的状态,并在下次启动时恢复到之前的状态。
你也可以在 JavaScript 和 Rust 中使用 window-state 插件。
提示
状态恢复发生在窗口创建之后。因此为防止窗口闪烁,你可以在创建窗口时把 visible 设为 false,插件会在恢复状态后显示该窗口。
JavaScript
你可以用 saveWindowState 手动保存窗口状态:
1
2
3
4
5
| import { saveWindowState, StateFlags } from '@tauri-apps/plugin-window-state';
// 使用 `"withGlobalTauri": true` 时,你可以这样写
// const { saveWindowState, StateFlags } = window.__TAURI__.windowState;
saveWindowState(StateFlags.ALL);
|
同样,你也可以手动从磁盘恢复窗口状态:
1
2
3
4
5
6
7
8
| import {
restoreStateCurrent,
StateFlags,
} from '@tauri-apps/plugin-window-state';
// 使用 `"withGlobalTauri": true` 时,你可以这样写
// const { restoreStateCurrent, StateFlags } = window.__TAURI__.windowState;
restoreStateCurrent(StateFlags.ALL);
|
Rust
你可以使用 AppHandleExt trait 暴露的 save_window_state() 方法:
1
2
3
4
| use tauri_plugin_window_state::{AppHandleExt, StateFlags};
// `tauri::AppHandle` 现在多了以下方法
app.save_window_state(StateFlags::all()); // 会把所有打开窗口的状态保存到磁盘
|
同样,你也可以用 WindowExt trait 暴露的 restore_state() 方法手动从磁盘恢复窗口状态:
1
2
3
4
| use tauri_plugin_window_state::{WindowExt, StateFlags};
// 所有 `Window` 类型现在多了以下方法
window.restore_state(StateFlags::all()); // 会从磁盘恢复窗口状态
|
权限
默认情况下,所有有潜在危险的插件命令和作用域都被阻止,无法访问。你必须在 capabilities 配置中修改权限才能启用它们。
更多信息请参阅能力概述,以及使用插件权限的分步指南。
1
2
3
4
5
6
| {
"permissions": [
...,
"window-state:default"
]
}
|