性能优化

This commit is contained in:
zhongluofeng
2026-08-06 10:33:16 +08:00
parent c7578a2e6b
commit e66c53e66d
105 changed files with 7273 additions and 5002 deletions
+51 -17
View File
@@ -6,8 +6,8 @@
//! - 任务栏覆盖检测:轮询 GetForegroundWindow,检测系统 UI 出现时暂时取消置顶
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use std::thread;
use std::sync::{Arc, Mutex, OnceLock};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use tauri::{AppHandle, Emitter};
@@ -15,6 +15,9 @@ use tauri::{AppHandle, Emitter};
static DRAG_STOP: OnceLock<Arc<AtomicBool>> = OnceLock::new();
/// 任务栏覆盖监视线程停止标志
static TOPMOST_STOP: OnceLock<Arc<AtomicBool>> = OnceLock::new();
/// 监视线程句柄(用于停止时 join,避免 sleep 猜测式等待 + 线程泄漏)
static DRAG_HANDLE: Mutex<Option<JoinHandle<()>>> = Mutex::new(None);
static TOPMOST_HANDLE: Mutex<Option<JoinHandle<()>>> = Mutex::new(None);
fn drag_stop() -> &'static Arc<AtomicBool> {
DRAG_STOP.get_or_init(|| Arc::new(AtomicBool::new(true)))
@@ -24,6 +27,30 @@ fn topmost_stop() -> &'static Arc<AtomicBool> {
TOPMOST_STOP.get_or_init(|| Arc::new(AtomicBool::new(true)))
}
/// 停止右键拖动监视线程并等待其退出(标志置位后线程最迟一个轮询周期退出)
fn stop_drag_thread() {
drag_stop().store(true, Ordering::SeqCst);
if let Some(h) = DRAG_HANDLE
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
{
let _ = h.join();
}
}
/// 停止任务栏覆盖监视线程并等待其退出
fn stop_topmost_thread() {
topmost_stop().store(true, Ordering::SeqCst);
if let Some(h) = TOPMOST_HANDLE
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
{
let _ = h.join();
}
}
#[cfg(windows)]
mod win_api {
use tauri::{AppHandle, Manager};
@@ -186,7 +213,7 @@ pub fn osd_apply_overlay_style(label: String, app: AppHandle) -> Result<(), Stri
let hwnd = win_api::get_hwnd(&label, &app)
.ok_or_else(|| format!("窗口 {} 不存在", label))?;
win_api::apply_no_activate(hwnd);
eprintln!("[osd] 已应用 NoActivate 样式到窗口 {}", label);
crate::logger::log_info("osd", &format!("已应用 NoActivate 样式到窗口 {}", label));
}
Ok(())
}
@@ -198,15 +225,14 @@ pub fn osd_apply_overlay_style(label: String, app: AppHandle) -> Result<(), Stri
/// 右键释放后发出 `osd-end-drag` 事件。
#[tauri::command]
pub fn osd_start_drag_watch(label: String, app: AppHandle) -> Result<(), String> {
// 停止旧线程,等待退出
drag_stop().store(true, Ordering::SeqCst);
thread::sleep(Duration::from_millis(50));
// 停止旧线程,等待退出后再启动新线程(避免新旧线程并存)
stop_drag_thread();
let stop_flag = drag_stop().clone();
stop_flag.store(false, Ordering::SeqCst);
let app_handle = app.clone();
thread::spawn(move || {
let handle = thread::spawn(move || {
let mut rbutton_was_down = false;
let mut press_start: Option<Instant> = None;
let mut drag_emitted = false;
@@ -234,7 +260,7 @@ pub fn osd_start_drag_watch(label: String, app: AppHandle) -> Result<(), String>
(win_api::get_cursor_pos(), win_api::get_window_rect(hwnd))
{
if win_api::point_in_rect(pt, rect) {
let _ = app_handle.emit("osd-start-drag", ());
let _ = app_handle.emit(crate::constants::events::OSD_START_DRAG, ());
drag_emitted = true;
}
}
@@ -245,7 +271,7 @@ pub fn osd_start_drag_watch(label: String, app: AppHandle) -> Result<(), String>
} else if !rbutton_down && rbutton_was_down {
// 右键释放
if drag_emitted {
let _ = app_handle.emit("osd-end-drag", ());
let _ = app_handle.emit(crate::constants::events::OSD_END_DRAG, ());
}
press_start = None;
drag_emitted = false;
@@ -258,6 +284,10 @@ pub fn osd_start_drag_watch(label: String, app: AppHandle) -> Result<(), String>
}
});
if let Ok(mut guard) = DRAG_HANDLE.lock() {
*guard = Some(handle);
}
Ok(())
}
@@ -268,14 +298,14 @@ pub fn osd_start_drag_watch(label: String, app: AppHandle) -> Result<(), String>
/// 系统 UI 关闭后发出 `osd-system-ui-inactive` 事件恢复置顶。
#[tauri::command]
pub fn osd_start_topmost_watch(app: AppHandle) -> Result<(), String> {
topmost_stop().store(true, Ordering::SeqCst);
thread::sleep(Duration::from_millis(50));
// 停止旧线程,等待其退出后再启动新线程
stop_topmost_thread();
let stop_flag = topmost_stop().clone();
stop_flag.store(false, Ordering::SeqCst);
let app_handle = app.clone();
thread::spawn(move || {
let handle = thread::spawn(move || {
let mut last_foreground: isize = 0;
let mut system_ui_active = false;
@@ -293,15 +323,15 @@ pub fn osd_start_topmost_watch(app: AppHandle) -> Result<(), String> {
if win_api::is_system_ui_class(&class) {
if !system_ui_active {
system_ui_active = true;
let _ = app_handle.emit("osd-system-ui-active", ());
let _ = app_handle.emit(crate::constants::events::OSD_SYSTEM_UI_ACTIVE, ());
}
} else if system_ui_active {
system_ui_active = false;
let _ = app_handle.emit("osd-system-ui-inactive", ());
let _ = app_handle.emit(crate::constants::events::OSD_SYSTEM_UI_INACTIVE, ());
}
} else if system_ui_active {
system_ui_active = false;
let _ = app_handle.emit("osd-system-ui-inactive", ());
let _ = app_handle.emit(crate::constants::events::OSD_SYSTEM_UI_INACTIVE, ());
}
}
}
@@ -310,14 +340,18 @@ pub fn osd_start_topmost_watch(app: AppHandle) -> Result<(), String> {
}
});
if let Ok(mut guard) = TOPMOST_HANDLE.lock() {
*guard = Some(handle);
}
Ok(())
}
/// 停止所有 OSD 监视线程
#[tauri::command]
pub fn osd_stop_watch() {
drag_stop().store(true, Ordering::SeqCst);
topmost_stop().store(true, Ordering::SeqCst);
stop_drag_thread();
stop_topmost_thread();
}
/// 设置点击穿透(Rust 侧原生 WS_EX_TRANSPARENT,比 JS setIgnoreCursorEvents 更可靠)