bug修复调整
This commit is contained in:
+150
-4
@@ -15,9 +15,12 @@ use tauri::{AppHandle, Emitter};
|
||||
static DRAG_STOP: OnceLock<Arc<AtomicBool>> = OnceLock::new();
|
||||
/// 任务栏覆盖监视线程停止标志
|
||||
static TOPMOST_STOP: OnceLock<Arc<AtomicBool>> = OnceLock::new();
|
||||
/// 游戏全屏监视线程停止标志
|
||||
static GAME_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);
|
||||
static GAME_HANDLE: Mutex<Option<JoinHandle<()>>> = Mutex::new(None);
|
||||
|
||||
fn drag_stop() -> &'static Arc<AtomicBool> {
|
||||
DRAG_STOP.get_or_init(|| Arc::new(AtomicBool::new(true)))
|
||||
@@ -27,6 +30,10 @@ fn topmost_stop() -> &'static Arc<AtomicBool> {
|
||||
TOPMOST_STOP.get_or_init(|| Arc::new(AtomicBool::new(true)))
|
||||
}
|
||||
|
||||
fn game_stop() -> &'static Arc<AtomicBool> {
|
||||
GAME_STOP.get_or_init(|| Arc::new(AtomicBool::new(true)))
|
||||
}
|
||||
|
||||
/// 停止右键拖动监视线程并等待其退出(标志置位后线程最迟一个轮询周期退出)
|
||||
fn stop_drag_thread() {
|
||||
drag_stop().store(true, Ordering::SeqCst);
|
||||
@@ -51,18 +58,35 @@ fn stop_topmost_thread() {
|
||||
}
|
||||
}
|
||||
|
||||
/// 停止游戏全屏监视线程并等待其退出
|
||||
fn stop_game_thread() {
|
||||
game_stop().store(true, Ordering::SeqCst);
|
||||
if let Some(h) = GAME_HANDLE
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.take()
|
||||
{
|
||||
let _ = h.join();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod win_api {
|
||||
use tauri::{AppHandle, Manager};
|
||||
use windows_sys::Win32::Foundation::{POINT, RECT};
|
||||
use windows_sys::Win32::Graphics::Gdi::{
|
||||
GetMonitorInfoW, MonitorFromWindow, MONITORINFO, MONITOR_DEFAULTTONEAREST,
|
||||
};
|
||||
use windows_sys::Win32::UI::Input::KeyboardAndMouse::{GetAsyncKeyState, VK_RBUTTON};
|
||||
use windows_sys::Win32::UI::WindowsAndMessaging::{
|
||||
GetClassNameW, GetCursorPos, GetForegroundWindow, GetWindowLongPtrW,
|
||||
GetWindowRect, SendMessageW, SetWindowLongPtrW, SetWindowPos,
|
||||
GWL_EXSTYLE, HTCAPTION, HWND_NOTOPMOST, HWND_TOPMOST, SWP_NOACTIVATE, SWP_NOMOVE,
|
||||
SWP_NOSIZE, SWP_NOZORDER, WM_NCLBUTTONDOWN, WS_EX_NOACTIVATE,
|
||||
GetClassNameW, GetCursorPos, GetForegroundWindow, GetWindowLongPtrW, GetWindowLongW,
|
||||
GetWindowRect, GetWindowThreadProcessId, SendMessageW, SetWindowLongPtrW, SetWindowPos,
|
||||
GWL_EXSTYLE, GWL_STYLE, HTCAPTION, HWND_NOTOPMOST, HWND_TOPMOST, SWP_NOACTIVATE,
|
||||
SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER, WM_NCLBUTTONDOWN, WS_EX_NOACTIVATE,
|
||||
WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT,
|
||||
};
|
||||
/// 供模块外全屏判定使用的窗口样式常量(pub re-export)
|
||||
pub use windows_sys::Win32::UI::WindowsAndMessaging::WS_CAPTION;
|
||||
|
||||
/// windows-sys 的 HWND 类型别名(isize)
|
||||
pub type Hwnd = isize;
|
||||
@@ -225,6 +249,41 @@ mod win_api {
|
||||
| "Windows.UI.Shell.ShellFlyoutWindow" // Win11 Shell 弹出
|
||||
)
|
||||
}
|
||||
|
||||
/// 获取窗口样式(GWL_STYLE)
|
||||
pub fn get_window_style(hwnd: Hwnd) -> isize {
|
||||
unsafe { GetWindowLongW(hwnd, GWL_STYLE) as isize }
|
||||
}
|
||||
|
||||
/// 判断窗口是否属于本进程(Thing 自身窗口不参与全屏判定)
|
||||
pub fn is_own_process(hwnd: Hwnd) -> bool {
|
||||
let mut pid: u32 = 0;
|
||||
unsafe {
|
||||
GetWindowThreadProcessId(hwnd, &mut pid);
|
||||
}
|
||||
pid != 0 && pid == std::process::id()
|
||||
}
|
||||
|
||||
/// 获取窗口所在显示器(最近匹配)的矩形(物理像素)
|
||||
pub fn get_monitor_rect(hwnd: Hwnd) -> Option<RECT> {
|
||||
let monitor = unsafe { MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST) };
|
||||
if monitor == 0 {
|
||||
return None;
|
||||
}
|
||||
let mut info = MONITORINFO {
|
||||
cbSize: std::mem::size_of::<MONITORINFO>() as u32,
|
||||
rcMonitor: RECT { left: 0, top: 0, right: 0, bottom: 0 },
|
||||
rcWork: RECT { left: 0, top: 0, right: 0, bottom: 0 },
|
||||
dwFlags: 0,
|
||||
};
|
||||
unsafe {
|
||||
if GetMonitorInfoW(monitor, &mut info) != 0 {
|
||||
Some(info.rcMonitor)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 应用 OSD 悬浮窗的原生样式(NoActivate + ToolWindow)
|
||||
@@ -369,11 +428,98 @@ pub fn osd_start_topmost_watch(app: AppHandle) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 判断窗口是否为全屏应用(无边框/独占全屏游戏)
|
||||
///
|
||||
/// 判定条件(全部满足):
|
||||
/// 1. 无 WS_CAPTION 样式 —— 排除普通应用的"最大化"(即使系统任务栏设为自动隐藏,
|
||||
/// 最大化窗口覆盖率也接近 100%,但它们带标题栏,靠样式即可区分)
|
||||
/// 2. 非本进程窗口(Thing 主窗口/悬浮窗自身)
|
||||
/// 3. 窗口矩形与所在显示器矩形的交集覆盖率 ≥ 95%(兼容缩放/1px 误差)
|
||||
#[cfg(windows)]
|
||||
fn is_fullscreen_game_window(hwnd: isize) -> bool {
|
||||
if win_api::get_window_style(hwnd) & (win_api::WS_CAPTION as isize) != 0 {
|
||||
return false;
|
||||
}
|
||||
if win_api::is_own_process(hwnd) {
|
||||
return false;
|
||||
}
|
||||
let (Some(win_rect), Some(mon_rect)) = (
|
||||
win_api::get_window_rect(hwnd),
|
||||
win_api::get_monitor_rect(hwnd),
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
let iw = (win_rect.right.min(mon_rect.right) - win_rect.left.max(mon_rect.left)).max(0) as i64;
|
||||
let ih = (win_rect.bottom.min(mon_rect.bottom) - win_rect.top.max(mon_rect.top)).max(0) as i64;
|
||||
let mw = (mon_rect.right - mon_rect.left).max(0) as i64;
|
||||
let mh = (mon_rect.bottom - mon_rect.top).max(0) as i64;
|
||||
let mon_area = (mw * mh).max(1);
|
||||
iw * ih * 100 >= mon_area * 95
|
||||
}
|
||||
|
||||
/// 启动游戏全屏监视
|
||||
///
|
||||
/// 轮询检测前景窗口是否为全屏应用(无边框/独占全屏游戏),
|
||||
/// 状态变化时发出 `osd-game-active` / `osd-game-inactive` 事件。
|
||||
/// 前端据此隐藏/恢复 OSD:透明置顶 WebView 悬浮窗会占用 DWM 合成路径,
|
||||
/// 禁用游戏的独立翻转(MPO),是游戏中掉帧的根源;隐藏悬浮窗即可排除影响。
|
||||
#[tauri::command]
|
||||
pub fn osd_start_game_watch(app: AppHandle) -> Result<(), String> {
|
||||
// 停止旧线程,等待其退出后再启动新线程
|
||||
stop_game_thread();
|
||||
let stop_flag = game_stop().clone();
|
||||
stop_flag.store(false, Ordering::SeqCst);
|
||||
|
||||
let app_handle = app.clone();
|
||||
|
||||
let handle = thread::spawn(move || {
|
||||
let mut fullscreen_active = false;
|
||||
|
||||
loop {
|
||||
if stop_flag.load(Ordering::SeqCst) {
|
||||
break;
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let fg = win_api::get_foreground_window();
|
||||
let fullscreen = fg != 0
|
||||
&& win_api::get_class_name(fg)
|
||||
.map_or(false, |c| !win_api::is_system_ui_class(&c))
|
||||
&& is_fullscreen_game_window(fg);
|
||||
|
||||
if fullscreen != fullscreen_active {
|
||||
fullscreen_active = fullscreen;
|
||||
let event = if fullscreen {
|
||||
crate::constants::events::OSD_GAME_ACTIVE
|
||||
} else {
|
||||
crate::constants::events::OSD_GAME_INACTIVE
|
||||
};
|
||||
let _ = app_handle.emit(event, ());
|
||||
crate::logger::log_info(
|
||||
"osd",
|
||||
&format!("全屏应用前台: {}", if fullscreen { "是 → 隐藏 OSD" } else { "否 → 恢复 OSD" }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
});
|
||||
|
||||
if let Ok(mut guard) = GAME_HANDLE.lock() {
|
||||
*guard = Some(handle);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 停止所有 OSD 监视线程
|
||||
#[tauri::command]
|
||||
pub fn osd_stop_watch() {
|
||||
stop_drag_thread();
|
||||
stop_topmost_thread();
|
||||
stop_game_thread();
|
||||
}
|
||||
|
||||
/// 设置点击穿透(Rust 侧原生 WS_EX_TRANSPARENT,比 JS setIgnoreCursorEvents 更可靠)
|
||||
|
||||
Reference in New Issue
Block a user