C.W.K.
Stream
Lesson 06 of 06 · published

Persisting Window State

~11 min · tauri, windows, plugin, persistence

Level 0Web Tourist
0 XP0/56 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete
"A desktop app that forgets where you put its window every launch feels broken — even when nothing is wrong."

The Smallest Polish That Matters Most

Users expect an app to reopen where they left it — same position, same size. Implementing that by hand (listen for move/resize, debounce, write to disk, read on startup, apply before show) is fiddly and easy to get subtly wrong. Tauri ships an official plugin, tauri-plugin-window-state, that does exactly this: it saves each window's geometry on exit and restores it on next launch, automatically.

Register and Forget

The beauty of this plugin is that it needs essentially zero application code beyond registration. You add it to the Builder chain and it hooks the window lifecycle for you. No commands to write, no events to wire — restore-on-open and save-on-exit just happen. It's the canonical example of a plugin doing one job well so you don't reinvent it.

A First Taste of the Three-Surface Rule

Adding this plugin touches three places, and noticing the pattern now will pay off in the plugins track: the Cargo.toml dependency, the builder registration in lib.rs, and (for plugins that expose commands to the frontend) a capabilities permission. window-state is mostly automatic, but it still appears in capabilities as window-state:default. Cinder registers this exact plugin so its window reopens in place — three surfaces, one behavior.

Code

Surface 2: register in the builder·rust
// lib.rs — register the plugin; restore/save happen automatically.
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_window_state::Builder::default().build())
        // ...other plugins, commands...
        .run(tauri::generate_context!())
        .expect("error while running app");
}
Surface 1: the Cargo dependency·toml
# Cargo.toml — Surface 1: the dependency.
[dependencies]
tauri-plugin-window-state = "2"

External links

Exercise

Add tauri-plugin-window-state to your app (dependency + builder line). Run it, move and resize the window, quit, and relaunch — confirm it reopens exactly where you left it. Then note: how many lines of YOUR code did persistence take? That near-zero number is the point of plugins.
Hint
Add the crate to Cargo.toml and .plugin(tauri_plugin_window_state::Builder::default().build()) to the chain. If the plugin's frontend commands are needed you'd add window-state:default to capabilities — but basic save/restore works from registration alone.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.