fix: windows cannot save window state(2)

This commit is contained in:
huzibaca 2024-11-20 00:27:53 +08:00
parent 94259f9515
commit 4e2d9d6acd
No known key found for this signature in database
GPG Key ID: D4364EE4851DC302
3 changed files with 16 additions and 0 deletions

View File

@ -9,6 +9,7 @@ use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
#[derive(Debug, Default, Clone)]
pub struct Handle {
pub app_handle: Arc<RwLock<Option<AppHandle>>>,
pub is_exiting: Arc<RwLock<bool>>,
}
impl Handle {
@ -17,6 +18,7 @@ impl Handle {
HANDLE.get_or_init(|| Handle {
app_handle: Arc::new(RwLock::new(None)),
is_exiting: Arc::new(RwLock::new(false)),
})
}
@ -68,4 +70,13 @@ impl Handle {
Tray::update_part()?;
Ok(())
}
pub fn set_is_exiting(&self) {
let mut is_exiting = self.is_exiting.write();
*is_exiting = true;
}
pub fn is_exiting(&self) -> bool {
*self.is_exiting.read()
}
}

View File

@ -118,12 +118,14 @@ pub fn toggle_tun_mode() {
pub fn quit(code: Option<i32>) {
let app_handle = handle::Handle::global().app_handle().unwrap();
handle::Handle::global().set_is_exiting();
resolve::resolve_reset();
#[cfg(target_os = "macos")]
tauri::async_runtime::block_on(async {
resolve::restore_public_dns().await;
});
log_err!(handle::Handle::global().get_window().unwrap().close());
log_err!(app_handle.save_window_state(StateFlags::default()));
app_handle.exit(code.unwrap_or(0));
}

View File

@ -151,6 +151,9 @@ pub fn run() {
if label == "main" {
match event {
tauri::WindowEvent::CloseRequested { api, .. } => {
if core::handle::Handle::global().is_exiting() {
return;
}
println!("closing window...");
api.prevent_close();
let window = core::handle::Handle::global().get_window().unwrap();