fix: ensure app state is saved on exit and window events for better state management

This commit is contained in:
Tunglies
2025-05-22 09:54:25 +08:00
Unverified
parent ebecabc0cc
commit 9af0803e9b
2 changed files with 30 additions and 2 deletions

View File

@@ -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())
);
}
}
_ => {}
}

View File

@@ -74,4 +74,4 @@
"csp": null
}
}
}
}