Hide stale off-screen windows of limited-AX apps
Some checks failed
CI / Build and test (ad-hoc signing) (push) Has been cancelled
Some checks failed
CI / Build and test (ad-hoc signing) (push) Has been cancelled
Some apps (for example a Java GUI toolkit) leave windows in the window list that claim the current Space but are not on screen. These stale buffer windows appeared as extra tabs. Add kCGWindowIsOnscreen and kCGWindowAlpha to the CGWindowList read and to the window dump. New filter rule: hide a no-AX window that is on the current Space and not on screen, unless it is minimized. A real window on the current Space is on screen. A real window on another Space is not on the current Space, so the rule keeps it (decision D4). Add WindowFilterTests for the stale, on-screen, and other-Space cases. All 34 tests pass with xcodebuild test.
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user