fix(popup): 云审收口(clamp saturating_sub 防溢出 + 右缘溢出单测 + 清理不可达 hwnd guard)

This commit is contained in:
吕柏青 committed 2026-06-07 13:59:54 +08:00
1 parent fab965162b
commit 482408071e
2 files changed
+15 -2

No files matched your search

@@ -515,6 +515,9 @@ pub(crate) fn show_capsule_window_no_activate<R: tauri::Runtime>(
}
}
}
// 走到这里 hwnd 必为 Some:上面循环要么在 Win32 分支 break(hwnd 已赋值),
// 要么提前 return false。此 else 仅是 HANDLE_RETRY_ATTEMPTS == 0(循环体一次都不跑)
// 时的防御性兜底,当前常量为 5 时不可达,保留以防后续把次数改 0。
let Some(hwnd) = hwnd else {
return false;
};
+12 -2
View File
@@ -1228,8 +1228,9 @@ fn clamp_to_monitor(
area_bottom: i32,
) -> (i32, i32) {
// 右/下边界 = area 右下角减去窗口自身尺寸,确保整窗可见。
let max_x = (area_right - w).max(area_left);
let max_y = (area_bottom - h).max(area_top);
// 用 saturating_sub 防 area_right/area_bottom 为极小(含 i32::MIN 近邻)时减法溢出。
let max_x = area_right.saturating_sub(w).max(area_left);
let max_y = area_bottom.saturating_sub(h).max(area_top);
let clamped_x = x.clamp(area_left, max_x);
let clamped_y = y.clamp(area_top, max_y);
(clamped_x, clamped_y)
@@ -1969,6 +1970,15 @@ mod tests {
assert!(y + 126 <= 1040);
}
#[test]
fn clamp_to_monitor_pulls_back_when_right_edge_overflows_inside_area() {
// 左上角 x=1800 本在 area 内,但 x+w=2064 越过右边界 1920 →
// 应被左移到「右边界 - 窗口宽」,整窗右缘恰好贴住 area_right。
let (x, _y) = clamp_to_monitor(1800, 900, 264, 126, 0, 0, 1920, 1040);
assert_eq!(x, 1920 - 264);
assert!(x + 264 <= 1920);
}
#[test]
fn clamp_to_monitor_pushes_into_negative_origin_left_monitor() {
// 副屏在主屏左侧(负 X 原点),落点算到了副屏左外侧 → 夹回 area_left。