From 24f4b62cff8786d5cc1c929c63f4c29999d36cd4 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 15 Jan 2025 05:53:00 -0800 Subject: [PATCH] Fix window state preservation --- src-tauri/src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 7bb13bab..08ea4a3a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -34,6 +34,7 @@ use tauri_plugin_clipboard_manager::ClipboardExt; use tauri_plugin_log::fern::colors::ColoredLevelConfig; use tauri_plugin_log::{Builder, Target, TargetKind}; use tauri_plugin_opener::OpenerExt; +use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt}; use tokio::fs::read_to_string; use tokio::sync::Mutex; use tokio::task::block_in_place; @@ -1950,6 +1951,24 @@ pub fn run() { } _ => {} }; + match event { + RunEvent::WindowEvent { + label, + event: WindowEvent::CloseRequested { .. }, + .. + } => { + // Save window state on close because, for some reason, it doesn't + if !label.starts_with(OTHER_WINDOW_PREFIX) { + let handle = app_handle.clone(); + if let Err(e) = handle.save_window_state(StateFlags::all()) { + warn!("Failed to save window state {e:?}"); + } else { + debug!("Saved window state"); + }; + } + } + _ => {} + }; }); } @@ -2080,6 +2099,12 @@ fn create_window(handle: &AppHandle, config: CreateWindowConfig) -> WebviewWindo } }); + if let Err(e) = win.restore_state(StateFlags::all()) { + warn!("Failed to restore window state {e:?}"); + } else { + debug!("Restored window state"); + } + win }