窗口状态
保存窗口位置和大小,并在重新打开应用时恢复它们。
¥Save window positions and sizes and restore them when the app is reopened.
支持的平台
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
设置
¥Setup
安装 window-state 插件即可开始使用。
¥Install the window-state plugin to get started.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
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
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-window-state --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))' -
Modify
lib.rs
to initialize the plugin: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");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
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
使用
¥Usage
添加窗口状态插件后,所有窗口都会记住应用关闭时的状态,并在下次启动时恢复到之前的状态。
¥After adding the window-state plugin, all windows will remember their state when the app is being closed and will restore to their previous state on the next launch.
你还可以在 JavaScript 和 Rust 中访问窗口状态插件。
¥You can also access the window-state plugin in both JavaScript and Rust.
JavaScript
你可以使用 saveWindowState
手动保存窗口状态:
¥You can use saveWindowState
to manually save the window state:
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);
同样,你可以手动从磁盘恢复窗口的状态:
¥Similarly you can manually restore a window’s state from disk:
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);
Rust
你可以使用 AppHandleExt
特性公开的 save_window_state()
方法:
¥You can use the save_window_state()
method exposed by the AppHandleExt
trait:
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
特性公开的 restore_state()
方法从磁盘手动恢复窗口的状态:
¥Similarly you can manually restore a window’s state from disk using the restore_state()
method exposed by the WindowExt
trait:
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
权限
¥Permissions
默认情况下,所有潜在危险的插件命令和范围都会被阻止,无法访问。你必须修改 capabilities
配置中的权限才能启用这些权限。
¥By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities
configuration to enable these.
有关更详细的说明,请参阅 功能概述。
¥See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "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.
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. |
Tauri 中文网 - 粤ICP备13048890号