fix: improve window close and focus listener management

This commit is contained in:
Tunglies 2026-03-18 10:58:30 +08:00
parent 75af05860e
commit 8b99bb5150
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8

View File

@ -153,6 +153,11 @@ pub async fn add_light_weight_timer() {
fn setup_window_close_listener() {
if let Some(window) = WindowManager::get_main_window() {
let previous_handler_id = WINDOW_CLOSE_HANDLER_ID.swap(0, Ordering::AcqRel);
if previous_handler_id != 0 {
window.unlisten(previous_handler_id);
logging!(debug, Type::Lightweight, "覆盖旧的窗口关闭监听");
}
let handler_id = window.listen("tauri://close-requested", move |_event| {
std::mem::drop(AsyncHandler::spawn(|| async {
if let Err(e) = setup_light_weight_timer().await {
@ -170,17 +175,22 @@ fn setup_window_close_listener() {
}
fn cancel_window_close_listener() {
if let Some(window) = WindowManager::get_main_window() {
let id = WINDOW_CLOSE_HANDLER_ID.swap(0, Ordering::AcqRel);
if id != 0 {
let id = WINDOW_CLOSE_HANDLER_ID.swap(0, Ordering::AcqRel);
if id != 0 {
if let Some(window) = WindowManager::get_main_window() {
window.unlisten(id);
logging!(debug, Type::Lightweight, "取消了窗口关闭监听");
}
logging!(debug, Type::Lightweight, "取消了窗口关闭监听");
}
}
fn setup_webview_focus_listener() {
if let Some(window) = WindowManager::get_main_window() {
let previous_handler_id = WEBVIEW_FOCUS_HANDLER_ID.swap(0, Ordering::AcqRel);
if previous_handler_id != 0 {
window.unlisten(previous_handler_id);
logging!(debug, Type::Lightweight, "覆盖旧的窗口焦点监听");
}
let handler_id = window.listen("tauri://focus", move |_event| {
logging_error!(Type::Lightweight, cancel_light_weight_timer());
logging!(debug, Type::Lightweight, "监听到窗口获得焦点,取消轻量模式计时");
@ -190,12 +200,12 @@ fn setup_webview_focus_listener() {
}
fn cancel_webview_focus_listener() {
if let Some(window) = WindowManager::get_main_window() {
let id = WEBVIEW_FOCUS_HANDLER_ID.swap(0, Ordering::AcqRel);
if id != 0 {
let id = WEBVIEW_FOCUS_HANDLER_ID.swap(0, Ordering::AcqRel);
if id != 0 {
if let Some(window) = WindowManager::get_main_window() {
window.unlisten(id);
logging!(debug, Type::Lightweight, "取消了窗口焦点监听");
}
logging!(debug, Type::Lightweight, "取消了窗口焦点监听");
}
}