Skip to content
Tauri

Window State

Save window positions and sizes and restore them when the app is reopened.

Supported Platforms

  • Windows
  • Linux
  • macOS

Setup

This plugin requires a Rust version of at least 1.75

Install the window-state plugin to get started.

Use your project’s package manager to add the dependency:

npm run tauri add window-state

Usage

After adding the all windows will remember their state when the app is being closed and will restore to their previous state on the next launch.

You can also access the window-state plugin in both JavaScript and Rust.

JavaScript

You can use saveWindowState to manually save the window state:

import { saveWindowState, StateFlags } from '@tauri-apps/plugin-window-state';
saveWindowState(StateFlags.ALL);

Similarly you can manually restore a window’s state from disk:

import {
restoreStateCurrent,
StateFlags,
} from '@tauri-apps/plugin-window-state';
restoreStateCurrent(StateFlags.ALL);

Rust

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 method
app.save_window_state(StateFlags::all()); // will save the state of all open windows to disk

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

Permissions

By default all plugin commands are blocked and cannot be accessed. You must define a list of permissions in your capabilities configuration.

See Permissions Overview for more information.

src-tauri/capabilities/main.json
{
"permissions": [
...,
"window-state:allow-restore-state",
"window-state:allow-save-window-state",
]
}
PermissionDescription
window-state:allow-restore-stateEnables the restore_state command without any pre-configured scope.
window-state:deny-restore-stateDenies the restore_state command without any pre-configured scope.
window-state:allow-save-window-stateEnables the save_window_state command without any pre-configured scope.
window-state:deny-save-window-stateDenies the save_window_state command without any pre-configured scope.

© 2024 Tauri Contributors. CC-BY / MIT