コンテンツにスキップ
Tauri

Window State(ウィンドウ状態)

《訳注》

Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。

ウィンドウの位置とサイズを保存し、アプリを再度開いたときに復元します。

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

はじめに、「window-state」プラグインをインストールしてください。

自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:

npm run tauri add window-state

「window-state」プラグインを追加すると、アプリが閉じられるときにすべてのウィンドウはその状態を記憶し、次回の起動時にその状態が復元されます。

「window-state」プラグインは、JavaScript と Rust の双方からもアクセスできます。

ウィンドウの状態を手動で保存するには、saveWindowState を使用します。

import { saveWindowState, StateFlags } from '@tauri-apps/plugin-window-state';
// `"withGlobalTauri": true` を使用する場合は
// const { saveWindowState, StateFlags } = window.__TAURI__.windowState; を使用できます
saveWindowState(StateFlags.ALL);

同様に、ディスクからウィンドウの状態を手動で復元することもできます:

import {
restoreStateCurrent,
StateFlags,
} from '@tauri-apps/plugin-window-state';
// `"withGlobalTauri": true` 使用する場合は
// const { restoreStateCurrent, StateFlags } = window.__TAURI__.windowState; を使用できます
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 設定でアクセス権限を変更する必要があります。

詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。

src-tauri/capabilities/default.json
{
"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-filename
  • allow-restore-state
  • allow-save-window-state

Permission Table

Identifier Description

window-state:allow-filename

Enables the filename command without any pre-configured scope.

window-state:deny-filename

Denies the filename command without any pre-configured scope.

window-state:allow-restore-state

Enables the restore_state command without any pre-configured scope.

window-state:deny-restore-state

Denies the restore_state command without any pre-configured scope.

window-state:allow-save-window-state

Enables the save_window_state command without any pre-configured scope.

window-state:deny-save-window-state

Denies the save_window_state command without any pre-configured scope.

【※ この日本語版は、「Apr 11, 2025 英語版」に基づいています】


© 2025 Tauri Contributors. CC-BY / MIT