diff --git a/Sources/Tracking/TrackingController.swift b/Sources/Tracking/TrackingController.swift index d55bcbf..c7e638b 100644 --- a/Sources/Tracking/TrackingController.swift +++ b/Sources/Tracking/TrackingController.swift @@ -169,7 +169,7 @@ final class TrackingController { for r in result.debug { let b = r.bounds let boundsText = "(\(b.origin.x), \(b.origin.y), \(b.width), \(b.height))" - lines.append("DUMP id=\(r.windowID) title=\(r.title) subrole=\(r.subrole ?? "nil") layer=\(r.layer) bounds=\(boundsText) spaces=\(r.spaceIDs) min=\(r.isMinimized) full=\(r.isFullScreen) shown=\(r.shown) reason=\(r.reason)") + lines.append("DUMP id=\(r.windowID) title=\(r.title) subrole=\(r.subrole ?? "nil") layer=\(r.layer) bounds=\(boundsText) spaces=\(r.spaceIDs) onscreen=\(r.isOnscreen) alpha=\(r.alpha) min=\(r.isMinimized) full=\(r.isFullScreen) shown=\(r.shown) reason=\(r.reason)") } sections[index] = lines } diff --git a/Sources/Tracking/WindowEnumerator.swift b/Sources/Tracking/WindowEnumerator.swift index e383f8c..f98e0ad 100644 --- a/Sources/Tracking/WindowEnumerator.swift +++ b/Sources/Tracking/WindowEnumerator.swift @@ -33,6 +33,8 @@ struct WindowEnumerator { let title: String? let layer: Int let bounds: CGRect + let isOnscreen: Bool + let alpha: Double } // WindowDebugRecord holds one line for the debug dump (spec section 7.14). @@ -45,6 +47,8 @@ struct WindowEnumerator { let spaceIDs: [UInt64] let isMinimized: Bool let isFullScreen: Bool + let isOnscreen: Bool + let alpha: Double let shown: Bool let reason: String } @@ -128,10 +132,16 @@ struct WindowEnumerator { bounds = rect } } + // kCGWindowIsOnscreen is present only for a window that is on + // screen. kCGWindowAlpha is the window opacity (0 = transparent). + let isOnscreen = (info[kCGWindowIsOnscreen as String] as? NSNumber)?.boolValue ?? false + let alpha = (info[kCGWindowAlpha as String] as? NSNumber)?.doubleValue ?? 1.0 result.append(CGRawWindow(windowID: windowID, title: title, layer: layer, - bounds: bounds)) + bounds: bounds, + isOnscreen: isOnscreen, + alpha: alpha)) } return result } @@ -167,6 +177,17 @@ struct WindowEnumerator { ids.formUnion(axByID.keys) ids.formUnion(cgByID.keys) + // The current Space is where the on-screen windows are. Derive it from + // the on-screen windows and the passed current Space IDs. This does not + // depend on one private API call. A no-AX window that claims the current + // Space but is not on screen is a stale buffer window. + var currentSpaces = Set(currentSpaceIDs) + for (id, window) in cgByID where window.isOnscreen { + for space in spaceIDsByWindow[id] ?? [] { + currentSpaces.insert(space) + } + } + var windows: [WindowInfo] = [] var elements: [(CGWindowID, AXUIElement)] = [] var debug: [WindowDebugRecord] = [] @@ -178,6 +199,12 @@ struct WindowEnumerator { let spaceIDs = spaceIDsByWindow[id] ?? [] let onCurrentSpace = isOnCurrentSpace(windowSpaceIDs: spaceIDs, currentSpaceIDs: currentSpaceIDs) + // onCurrentSpacePositive needs a real Space match. An empty Space + // list does not count (unlike onCurrentSpace above). + let onCurrentSpacePositive = spaceIDs.isEmpty == false + && currentSpaces.isEmpty == false + && spaceIDs.contains(where: { currentSpaces.contains($0) }) + let isOnscreen = cgWindow?.isOnscreen ?? false var title = appName if let axTitle = axWindow?.title, axTitle.isEmpty == false { @@ -202,6 +229,8 @@ struct WindowEnumerator { title: title, appName: appName, isMinimized: isMinimized, + isOnscreen: isOnscreen, + onCurrentSpace: onCurrentSpacePositive, screenBoundsCG: screenBoundsCG, hasCG: cgWindow != nil) @@ -213,6 +242,8 @@ struct WindowEnumerator { spaceIDs: spaceIDs, isMinimized: isMinimized, isFullScreen: isFullScreen, + isOnscreen: cgWindow?.isOnscreen ?? false, + alpha: cgWindow?.alpha ?? 1.0, shown: decision.show, reason: decision.reason)) @@ -248,6 +279,8 @@ struct WindowEnumerator { title: String, appName: String, isMinimized: Bool, + isOnscreen: Bool, + onCurrentSpace: Bool, screenBoundsCG: CGRect, hasCG: Bool) -> (show: Bool, reason: String) { if ownerPID == ownPID { @@ -277,6 +310,14 @@ struct WindowEnumerator { if title == appName { return (false, "no AX and title is the app name") } + // Stale buffer window: some applications (for example a Java GUI + // toolkit) leave windows in the list that claim the current Space + // but are not on screen. A real window on the current Space is on + // screen. A real window on another Space is not on the current + // Space, so this rule keeps it. A minimized window keeps its tab. + if isMinimized == false, onCurrentSpace, isOnscreen == false { + return (false, "no AX, on current Space, not on screen") + } if bounds.width < 50 || bounds.height < 50 { return (false, "no AX and bounds < 50 x 50") } diff --git a/Tests/WindowFilterTests.swift b/Tests/WindowFilterTests.swift index 7cb73db..4863db0 100644 --- a/Tests/WindowFilterTests.swift +++ b/Tests/WindowFilterTests.swift @@ -24,7 +24,8 @@ final class WindowFilterTests: XCTestCase { } private func cgWindow(id: CGWindowID, title: String?, bounds: CGRect) -> WindowEnumerator.CGRawWindow { - return WindowEnumerator.CGRawWindow(windowID: id, title: title, layer: 0, bounds: bounds) + return WindowEnumerator.CGRawWindow(windowID: id, title: title, layer: 0, bounds: bounds, + isOnscreen: true, alpha: 1.0) } private func shownIDs(ax: [WindowEnumerator.AXRawWindow], @@ -98,4 +99,72 @@ final class WindowFilterTests: XCTestCase { let shown = shownIDs(ax: ax, axIDs: [555], cg: cg) XCTAssertTrue(shown.contains(555)) } + + // The stale buffer window case: a no-AX window that claims the current + // Space but is not on screen. Some Java GUI toolkits leave these windows. + func testHidesStaleCurrentSpaceOffscreenWindow() { + let cg = [WindowEnumerator.CGRawWindow(windowID: 500, + title: "Report 1 - Overview", + layer: 0, + bounds: CGRect(x: 0, y: 0, width: 1624, height: 1247), + isOnscreen: false, + alpha: 1.0)] + let result = enumerator.buildWindows(pid: pid, + bundleID: "com.example.javaapp", + appName: "JavaApp", + ax: [], + axWindowIDs: [], + cg: cg, + spaceIDsByWindow: [500: [4]], + currentSpaceIDs: [4], + ownPID: 9999, + screenBoundsCG: CGRect(x: 0, y: 0, width: 2560, height: 1440), + now: Date()) + XCTAssertFalse(result.windows.contains { $0.id == 500 }) + } + + // A no-AX window on the current Space that IS on screen is a real window. + func testKeepsOnscreenCurrentSpaceNoAXWindow() { + let cg = [WindowEnumerator.CGRawWindow(windowID: 501, + title: "Report 2 - Details", + layer: 0, + bounds: CGRect(x: 0, y: 0, width: 1624, height: 1247), + isOnscreen: true, + alpha: 1.0)] + let result = enumerator.buildWindows(pid: pid, + bundleID: "com.example.javaapp", + appName: "JavaApp", + ax: [], + axWindowIDs: [], + cg: cg, + spaceIDsByWindow: [501: [4]], + currentSpaceIDs: [4], + ownPID: 9999, + screenBoundsCG: CGRect(x: 0, y: 0, width: 2560, height: 1440), + now: Date()) + XCTAssertTrue(result.windows.contains { $0.id == 501 }) + } + + // A no-AX window with no Space id is on another Space. The rule keeps it, + // even when it is not on screen (decision D4). + func testKeepsOtherSpaceOffscreenNoAXWindow() { + let cg = [WindowEnumerator.CGRawWindow(windowID: 502, + title: "Document 3", + layer: 0, + bounds: CGRect(x: 0, y: 0, width: 1200, height: 800), + isOnscreen: false, + alpha: 1.0)] + let result = enumerator.buildWindows(pid: pid, + bundleID: "com.example.app", + appName: "ExampleApp", + ax: [], + axWindowIDs: [], + cg: cg, + spaceIDsByWindow: [:], + currentSpaceIDs: [4], + ownPID: 9999, + screenBoundsCG: CGRect(x: 0, y: 0, width: 2560, height: 1440), + now: Date()) + XCTAssertTrue(result.windows.contains { $0.id == 502 }) + } }