跳到主要內容
Tauri

視窗狀態

儲存視窗位置和大小,並在重新開啟應用程式時還原。

支援平台

此外掛程式需要 Rust 版本至少 1.77.2

平台 層級 備註
windows
linux
macos
android
ios

設定

安裝 window-state 外掛程式以開始使用。

使用專案的套件管理器新增依賴項

npm run tauri add 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 method
app.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 method
window.restore_state(StateFlags::all()); // will restore the window's state from disk

權限

預設情況下,所有潛在危險的外掛程式命令和作用域都會被封鎖且無法存取。您必須修改 capabilities 組態中的權限才能啟用這些。

請參閱功能總覽以取得更多資訊,以及逐步指南以使用外掛程式權限。

src-tauri/capabilities/default.json
{
"permissions": [
...,
"window-state:default",
]
}

預設權限

此權限集設定可從視窗狀態外掛程式取得哪些類型的操作。

已授與的權限

預設會啟用所有操作。

  • allow-filename
  • allow-restore-state
  • allow-save-window-state

權限表

識別碼 描述

window-state:allow-filename

啟用 filename 命令,無需任何預先設定的作用域。

window-state:deny-filename

拒絕 filename 命令,無需任何預先設定的作用域。

window-state:allow-restore-state

啟用 restore_state 命令,無需任何預先設定的作用域。

window-state:deny-restore-state

拒絕 restore_state 命令,無需任何預先設定的作用域。

window-state:allow-save-window-state

啟用 save_window_state 命令,無需任何預先設定的作用域。

window-state:deny-save-window-state

拒絕 save_window_state 命令,無需任何預先設定的作用域。


© 2025 Tauri Contributors. CC-BY / MIT