視窗狀態
儲存視窗位置和大小,並在重新開啟應用程式時還原。
支援平台
此外掛程式需要 Rust 版本至少 1.77.2
平台 | 層級 | 備註 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
設定
安裝 window-state 外掛程式以開始使用。
使用專案的套件管理器新增依賴項
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
-
在
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
用法
新增 window-state 外掛程式後,所有視窗都會在應用程式關閉時記住其狀態,並在下次啟動時還原到先前的狀態。
您也可以在 JavaScript 和 Rust 中存取 window-state 外掛程式。
JavaScript
您可以使用 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);
Rust
您可以使用 AppHandleExt
特徵公開的 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
特徵公開的 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-filename
allow-restore-state
allow-save-window-state
權限表
識別碼 | 描述 |
---|---|
|
啟用 filename 命令,無需任何預先設定的作用域。 |
|
拒絕 filename 命令,無需任何預先設定的作用域。 |
|
啟用 restore_state 命令,無需任何預先設定的作用域。 |
|
拒絕 restore_state 命令,無需任何預先設定的作用域。 |
|
啟用 save_window_state 命令,無需任何預先設定的作用域。 |
|
拒絕 save_window_state 命令,無需任何預先設定的作用域。 |
© 2025 Tauri Contributors. CC-BY / MIT