fix: resolve crashes when exiting lightweight mode by ensuring async window operations

This commit is contained in:
Tunglies
2025-08-30 20:04:21 +08:00
parent c09066c0a3
commit 92d9c94e87
5 changed files with 20 additions and 18 deletions

View File

@@ -27,6 +27,7 @@
- 修复卸载服务后的 tun 开关状态问题 - 修复卸载服务后的 tun 开关状态问题
- 修复页面快速切换订阅时导致崩溃 - 修复页面快速切换订阅时导致崩溃
- 修复丢失工作目录时无法恢复环境 - 修复丢失工作目录时无法恢复环境
- 修复从轻量模式恢复导致崩溃
### 👙 界面样式 ### 👙 界面样式

View File

@@ -524,7 +524,7 @@ impl Tray {
log::info!(target: "app", "当前在轻量模式,正在退出轻量模式"); log::info!(target: "app", "当前在轻量模式,正在退出轻量模式");
crate::module::lightweight::exit_lightweight_mode().await; 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:?}"); log::info!(target: "app", "窗口显示结果: {result:?}");
}), }),
_ => Box::pin(async move {}), _ => Box::pin(async move {}),
@@ -822,7 +822,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
log::info!(target: "app", "当前在轻量模式,正在退出"); log::info!(target: "app", "当前在轻量模式,正在退出");
crate::module::lightweight::exit_lightweight_mode().await; // Await async function 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:?}"); log::info!(target: "app", "窗口显示结果: {result:?}");
} }
"system_proxy" => { "system_proxy" => {
@@ -852,7 +852,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
if was_lightweight { if was_lightweight {
crate::module::lightweight::exit_lightweight_mode().await; // Await async function crate::module::lightweight::exit_lightweight_mode().await; // Await async function
use crate::utils::window_manager::WindowManager; 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:?}"); log::info!(target: "app", "退出轻量模式后显示主窗口: {result:?}");
} else { } else {
crate::module::lightweight::entry_lightweight_mode().await; // Remove .await as it's not async crate::module::lightweight::entry_lightweight_mode().await; // Remove .await as it's not async

View File

@@ -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"); log::info!(target: "app", "Currently in lightweight mode, exiting lightweight mode");
crate::module::lightweight::exit_lightweight_mode().await; crate::module::lightweight::exit_lightweight_mode().await;
log::info!(target: "app", "Creating new window after exiting lightweight mode"); 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:?}"); log::info!(target: "app", "Window operation result: {result:?}");
return; return;
} }
let result = WindowManager::toggle_main_window(); let result = WindowManager::toggle_main_window().await;
log::info!(target: "app", "Window toggle result: {result:?}"); log::info!(target: "app", "Window toggle result: {result:?}");
return; 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"); log::info!(target: "app", "Currently in lightweight mode, exiting lightweight mode");
crate::module::lightweight::exit_lightweight_mode().await; crate::module::lightweight::exit_lightweight_mode().await;
log::info!(target: "app", "Creating new window after exiting lightweight mode"); 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:?}"); log::info!(target: "app", "Window operation result: {result:?}");
return; return;
} }
let result = WindowManager::toggle_main_window(); let result = WindowManager::toggle_main_window().await;
log::info!(target: "app", "Window toggle result: {result:?}"); log::info!(target: "app", "Window toggle result: {result:?}");
} }

View File

@@ -362,7 +362,7 @@ pub fn run() {
/// Handle application reopen events (macOS) /// Handle application reopen events (macOS)
#[cfg(target_os = "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!( logging!(
info, info,
Type::System, Type::System,
@@ -379,7 +379,7 @@ pub fn run() {
logging!(info, Type::System, true, "没有可见窗口,尝试显示主窗口"); logging!(info, Type::System, true, "没有可见窗口,尝试显示主窗口");
let result = WindowManager::show_main_window(); let result = WindowManager::show_main_window().await;
logging!( logging!(
info, info,
Type::System, Type::System,
@@ -538,7 +538,10 @@ pub fn run() {
has_visible_windows, 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, .. } => { tauri::RunEvent::ExitRequested { api, code, .. } => {
if code.is_none() { if code.is_none() {

View File

@@ -115,7 +115,7 @@ impl WindowManager {
} }
/// 智能显示主窗口 /// 智能显示主窗口
pub fn show_main_window() -> WindowOperationResult { pub async fn show_main_window() -> WindowOperationResult {
// 防抖检查 // 防抖检查
if !should_handle_window_operation() { if !should_handle_window_operation() {
return WindowOperationResult::NoAction; return WindowOperationResult::NoAction;
@@ -138,7 +138,7 @@ impl WindowManager {
match current_state { match current_state {
WindowState::NotExist => { WindowState::NotExist => {
logging!(info, Type::Window, true, "窗口不存在,创建新窗口"); logging!(info, Type::Window, true, "窗口不存在,创建新窗口");
if Self::create_new_window() { if Self::create_new_window().await {
logging!(info, Type::Window, true, "窗口创建成功"); logging!(info, Type::Window, true, "窗口创建成功");
std::thread::sleep(std::time::Duration::from_millis(100)); std::thread::sleep(std::time::Duration::from_millis(100));
WindowOperationResult::Created 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() { if !should_handle_window_operation() {
return WindowOperationResult::NoAction; return WindowOperationResult::NoAction;
@@ -198,7 +198,7 @@ impl WindowManager {
// 窗口不存在,创建新窗口 // 窗口不存在,创建新窗口
logging!(info, Type::Window, true, "窗口不存在,将创建新窗口"); logging!(info, Type::Window, true, "窗口不存在,将创建新窗口");
// 由于已经有防抖保护,直接调用内部方法 // 由于已经有防抖保护,直接调用内部方法
if Self::create_new_window() { if Self::create_new_window().await {
WindowOperationResult::Created WindowOperationResult::Created
} else { } else {
WindowOperationResult::Failed WindowOperationResult::Failed
@@ -360,12 +360,10 @@ impl WindowManager {
} }
/// 创建新窗口,防抖避免重复调用 /// 创建新窗口,防抖避免重复调用
fn create_new_window() -> bool { async fn create_new_window() -> bool {
use crate::process::AsyncHandler;
use crate::utils::resolve; use crate::utils::resolve;
// 使用 tokio runtime 阻塞调用 async 函数 resolve::window::create_window(true).await
AsyncHandler::block_on(resolve::window::create_window(true))
} }
/// 获取详细的窗口状态信息 /// 获取详细的窗口状态信息