From 92d9c94e87b06b0b041fce3728c92d321b6c4452 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sat, 30 Aug 2025 20:04:21 +0800 Subject: [PATCH] fix: resolve crashes when exiting lightweight mode by ensuring async window operations --- UPDATELOG.md | 1 + src-tauri/src/core/tray/mod.rs | 6 +++--- src-tauri/src/feat/window.rs | 8 ++++---- src-tauri/src/lib.rs | 9 ++++++--- src-tauri/src/utils/window_manager.rs | 14 ++++++-------- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/UPDATELOG.md b/UPDATELOG.md index b1dccf37..5e6189fa 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -27,6 +27,7 @@ - 修复卸载服务后的 tun 开关状态问题 - 修复页面快速切换订阅时导致崩溃 - 修复丢失工作目录时无法恢复环境 +- 修复从轻量模式恢复导致崩溃 ### 👙 界面样式 diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index a7f308ea..64445b4c 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -524,7 +524,7 @@ impl Tray { log::info!(target: "app", "当前在轻量模式,正在退出轻量模式"); crate::module::lightweight::exit_lightweight_mode().await; } - let result = WindowManager::show_main_window(); + let result = WindowManager::show_main_window().await; log::info!(target: "app", "窗口显示结果: {result:?}"); }), _ => Box::pin(async move {}), @@ -822,7 +822,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { log::info!(target: "app", "当前在轻量模式,正在退出"); crate::module::lightweight::exit_lightweight_mode().await; // Await async function } - let result = WindowManager::show_main_window(); // Remove .await as it's not async + let result = WindowManager::show_main_window().await; // Await async function log::info!(target: "app", "窗口显示结果: {result:?}"); } "system_proxy" => { @@ -852,7 +852,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { if was_lightweight { crate::module::lightweight::exit_lightweight_mode().await; // Await async function use crate::utils::window_manager::WindowManager; - let result = WindowManager::show_main_window(); // Remove .await as it's not async + let result = WindowManager::show_main_window().await; // Await async function log::info!(target: "app", "退出轻量模式后显示主窗口: {result:?}"); } else { crate::module::lightweight::entry_lightweight_mode().await; // Remove .await as it's not async diff --git a/src-tauri/src/feat/window.rs b/src-tauri/src/feat/window.rs index b80b1b8a..e4391aa8 100644 --- a/src-tauri/src/feat/window.rs +++ b/src-tauri/src/feat/window.rs @@ -34,12 +34,12 @@ async fn open_or_close_dashboard_internal(bypass_debounce: bool) { log::info!(target: "app", "Currently in lightweight mode, exiting lightweight mode"); crate::module::lightweight::exit_lightweight_mode().await; log::info!(target: "app", "Creating new window after exiting lightweight mode"); - let result = WindowManager::show_main_window(); + let result = WindowManager::show_main_window().await; log::info!(target: "app", "Window operation result: {result:?}"); return; } - let result = WindowManager::toggle_main_window(); + let result = WindowManager::toggle_main_window().await; log::info!(target: "app", "Window toggle result: {result:?}"); return; } @@ -47,12 +47,12 @@ async fn open_or_close_dashboard_internal(bypass_debounce: bool) { log::info!(target: "app", "Currently in lightweight mode, exiting lightweight mode"); crate::module::lightweight::exit_lightweight_mode().await; log::info!(target: "app", "Creating new window after exiting lightweight mode"); - let result = WindowManager::show_main_window(); + let result = WindowManager::show_main_window().await; log::info!(target: "app", "Window operation result: {result:?}"); return; } - let result = WindowManager::toggle_main_window(); + let result = WindowManager::toggle_main_window().await; log::info!(target: "app", "Window toggle result: {result:?}"); } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index ab6f6484..65d95de7 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -362,7 +362,7 @@ pub fn run() { /// Handle application reopen events (macOS) #[cfg(target_os = "macos")] - pub fn handle_reopen(app_handle: &AppHandle, has_visible_windows: bool) { + pub async fn handle_reopen(app_handle: &AppHandle, has_visible_windows: bool) { logging!( info, Type::System, @@ -379,7 +379,7 @@ pub fn run() { logging!(info, Type::System, true, "没有可见窗口,尝试显示主窗口"); - let result = WindowManager::show_main_window(); + let result = WindowManager::show_main_window().await; logging!( info, Type::System, @@ -538,7 +538,10 @@ pub fn run() { has_visible_windows, .. } => { - event_handlers::handle_reopen(app_handle, has_visible_windows); + let app_handle = app_handle.clone(); + AsyncHandler::spawn(move || async move { + event_handlers::handle_reopen(&app_handle, has_visible_windows).await; + }); } tauri::RunEvent::ExitRequested { api, code, .. } => { if code.is_none() { diff --git a/src-tauri/src/utils/window_manager.rs b/src-tauri/src/utils/window_manager.rs index 97b5984a..5d7b1cbd 100644 --- a/src-tauri/src/utils/window_manager.rs +++ b/src-tauri/src/utils/window_manager.rs @@ -115,7 +115,7 @@ impl WindowManager { } /// 智能显示主窗口 - pub fn show_main_window() -> WindowOperationResult { + pub async fn show_main_window() -> WindowOperationResult { // 防抖检查 if !should_handle_window_operation() { return WindowOperationResult::NoAction; @@ -138,7 +138,7 @@ impl WindowManager { match current_state { WindowState::NotExist => { logging!(info, Type::Window, true, "窗口不存在,创建新窗口"); - if Self::create_new_window() { + if Self::create_new_window().await { logging!(info, Type::Window, true, "窗口创建成功"); std::thread::sleep(std::time::Duration::from_millis(100)); WindowOperationResult::Created @@ -172,7 +172,7 @@ impl WindowManager { } /// 切换主窗口显示状态(显示/隐藏) - pub fn toggle_main_window() -> WindowOperationResult { + pub async fn toggle_main_window() -> WindowOperationResult { // 防抖检查 if !should_handle_window_operation() { return WindowOperationResult::NoAction; @@ -198,7 +198,7 @@ impl WindowManager { // 窗口不存在,创建新窗口 logging!(info, Type::Window, true, "窗口不存在,将创建新窗口"); // 由于已经有防抖保护,直接调用内部方法 - if Self::create_new_window() { + if Self::create_new_window().await { WindowOperationResult::Created } else { WindowOperationResult::Failed @@ -360,12 +360,10 @@ impl WindowManager { } /// 创建新窗口,防抖避免重复调用 - fn create_new_window() -> bool { - use crate::process::AsyncHandler; + async fn create_new_window() -> bool { use crate::utils::resolve; - // 使用 tokio runtime 阻塞调用 async 函数 - AsyncHandler::block_on(resolve::window::create_window(true)) + resolve::window::create_window(true).await } /// 获取详细的窗口状态信息