From 9af0803e9b36f6ee2e552515bd02fdb325a581f6 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Thu, 22 May 2025 09:54:25 +0800 Subject: [PATCH] fix: ensure app state is saved on exit and window events for better state management --- src-tauri/src/lib.rs | 30 +++++++++++++++++++++++++++++- src-tauri/tauri.conf.json | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index b1f31593..3588e036 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -19,7 +19,7 @@ use tauri::AppHandle; use tauri::Manager; use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_deep_link::DeepLinkExt; -use tauri_plugin_window_state::{StateFlags, WindowExt}; +use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt}; use tokio::time::{timeout, Duration}; use utils::logging::Type; @@ -350,12 +350,26 @@ pub fn run() { if code.is_none() { api.prevent_exit(); } + if let Some(app_handle) = core::handle::Handle::global().app_handle() { + logging_error!( + Type::Window, + true, + app_handle.save_window_state(StateFlags::all()) + ); + } } tauri::RunEvent::Exit => { // avoid duplicate cleanup if core::handle::Handle::global().is_exiting() { return; } + if let Some(app_handle) = core::handle::Handle::global().app_handle() { + logging_error!( + Type::Window, + true, + app_handle.save_window_state(StateFlags::all()) + ); + } feat::clean(); } tauri::RunEvent::WindowEvent { label, event, .. } => { @@ -374,6 +388,13 @@ pub fn run() { } else { logging!(warn, Type::Window, true, "尝试隐藏窗口但窗口不存在"); } + if let Some(app_handle) = core::handle::Handle::global().app_handle() { + logging_error!( + Type::Window, + true, + app_handle.save_window_state(StateFlags::all()) + ); + } } tauri::WindowEvent::Focused(true) => { #[cfg(target_os = "macos")] @@ -437,6 +458,13 @@ pub fn run() { hotkey::Hotkey::global().unregister("CMD+W") ); } + if let Some(app_handle) = core::handle::Handle::global().app_handle() { + logging_error!( + Type::Window, + true, + app_handle.save_window_state(StateFlags::all()) + ); + } } _ => {} } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 149aad88..3a94a215 100755 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -74,4 +74,4 @@ "csp": null } } -} \ No newline at end of file +}