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-stateyarn run tauri add window-statepnpm tauri add window-statedeno task tauri add window-statebun tauri add window-statecargo 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」プラグインを追加すると、アプリが閉じられるときにすべてのウィンドウはその状態を記憶し、次回の起動時にその状態が復元されます。
「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」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "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-filenameallow-restore-stateallow-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. |
【※ この日本語版は、「Apr 11, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT