diff --git a/internal/platform/windows/fullscreen.go b/internal/platform/windows/fullscreen.go index 2671186..4bb81a0 100644 --- a/internal/platform/windows/fullscreen.go +++ b/internal/platform/windows/fullscreen.go @@ -3,6 +3,7 @@ package windows import ( "sync" "sync/atomic" + "syscall" "time" "unsafe" ) @@ -16,9 +17,45 @@ const FullscreenCoverageThreshold = 0.95 var ( procGetForegroundWindow = user32Hook.NewProc("GetForegroundWindow") procGetWindowRect = user32Hook.NewProc("GetWindowRect") + procGetClassNameW = user32Hook.NewProc("GetClassNameW") + procGetDesktopWindow = user32Hook.NewProc("GetDesktopWindow") procIsZoomed = user32Hook.NewProc("IsZoomed") ) +// isDesktopClass 报告窗口类名是否为 Shell 桌面窗口类。 +// +// 空桌面(任务栏无程序)时前台窗口是这些窗口之一: +// - Progman Shell 桌面主窗口(explorer 创建,桌面图标/壁纸宿主) +// - WorkerW Progman 的壁纸子窗口(Win11 常作为前台焦点窗口) +// - #32769 系统桌面窗口(GetDesktopWindow,所有窗口的根) +// - SHELLDLL_DefView 桌面图标宿主(用户点击桌面图标时获得焦点) +// - SysListView32 桌面图标列表视图(同为点击桌面图标场景) +// +// 它们的矩形覆盖整个屏幕且没有 WS_MAXIMIZE 样式,仅靠覆盖率阈值会被误判 +// 为全屏,从而错误抑制角落/边缘规则(症状:空虚拟桌面上切桌面/调音量全部 +// 失效)。桌面窗口不算全屏。 +func isDesktopClass(cls string) bool { + switch cls { + case "Progman", "WorkerW", "#32769", "SHELLDLL_DefView", "SysListView32": + return true + } + return false +} + +// isDesktopWindow 判断窗口是否为桌面窗口(Progman 桌面或其壁纸层 WorkerW)。 +// +// 空桌面(任务栏无任何程序)时前台窗口就是桌面窗口:其矩形覆盖整个屏幕且 +// 没有 WS_MAXIMIZE 样式,仅靠覆盖率阈值会被误判为全屏,从而错误抑制角落/ +// 边缘规则(症状:空虚拟桌面上切桌面/调音量全部失效)。桌面不算全屏。 +func isDesktopWindow(hwnd uintptr) bool { + var buf [64]uint16 + n, _, _ := procGetClassNameW.Call(hwnd, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf))) + if n == 0 { + return false + } + return isDesktopClass(syscall.UTF16ToString(buf[:n])) +} + // IsFullscreenRect 判定窗口矩形是否覆盖屏幕矩形达到阈值(纯函数,可测)。 // coverage = 交集面积 / 屏幕面积。 func IsFullscreenRect(win, screen MonitorRect, threshold float64) bool { @@ -71,6 +108,12 @@ func foregroundFullscreen() bool { if hwnd == 0 { return false } + // 空桌面(任务栏无程序)时前台窗口是桌面 Progman/WorkerW:矩形覆盖 + // 全屏且无 WS_MAXIMIZE,仅靠覆盖率会被误判为全屏,导致角落/边缘规则 + // 被错误抑制。桌面窗口不算全屏。 + if isDesktopWindow(hwnd) { + return false + } // 最大化窗口不是全屏:任务栏可见时最大化窗口覆盖工作区 ≈96%, // 仅靠覆盖率阈值(95%)会把浏览器等普通最大化窗口误判为全屏, // 导致规则被错误抑制(角落/边缘功能失效)。用 IsZoomed 排除 diff --git a/internal/platform/windows/fullscreen_test.go b/internal/platform/windows/fullscreen_test.go index fc547a3..8477a00 100644 --- a/internal/platform/windows/fullscreen_test.go +++ b/internal/platform/windows/fullscreen_test.go @@ -37,6 +37,41 @@ func TestIsFullscreenRectMultiMonitor(t *testing.T) { } } +// TestIsDesktopClass 验证 Shell 桌面窗口类名识别(空桌面场景回归)。 +func TestIsDesktopClass(t *testing.T) { + cases := []struct { + cls string + want bool + }{ + {"Progman", true}, // Shell 桌面主窗口 + {"WorkerW", true}, // 壁纸层(Win11 前台焦点) + {"#32769", true}, // 系统桌面窗口(GetDesktopWindow) + {"SHELLDLL_DefView", true}, // 桌面图标宿主 + {"SysListView32", true}, // 桌面图标列表 + {"Chrome_WidgetWin_1", false}, + {"ConsoleWindowClass", false}, + {"CabinetWClass", false}, // 资源管理器(普通窗口,不算桌面) + {"", false}, + } + for _, c := range cases { + if got := isDesktopClass(c.cls); got != c.want { + t.Errorf("isDesktopClass(%q) = %v, want %v", c.cls, got, c.want) + } + } +} + +// TestIsDesktopWindowReal 集成验证:GetDesktopWindow 返回的系统桌面窗口 +// (类名 #32769)必须判定为桌面(防止空桌面被误判全屏导致规则抑制)。 +func TestIsDesktopWindowReal(t *testing.T) { + hwnd, _, _ := procGetDesktopWindow.Call() + if hwnd == 0 { + t.Skip("GetDesktopWindow returned 0") + } + if !isDesktopWindow(hwnd) { + t.Fatal("desktop window (#32769) should be detected as desktop") + } +} + // TestFullscreenWatcherLifecycle 验证轮询器 Start/Stop 幂等与状态可读。 func TestFullscreenWatcherLifecycle(t *testing.T) { w := NewFullscreenWatcher()