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.
367 lines
16 KiB
Swift
367 lines
16 KiB
Swift
import Foundation
|
|
import AppKit
|
|
import ApplicationServices
|
|
import CoreGraphics
|
|
|
|
// WindowEnumerator reads the windows of one application. The enumerator uses
|
|
// two sources and merges them by CGWindowID (spec section 7.3).
|
|
// - AX source: the current Space and the minimized windows.
|
|
// - CGWindowList source: all Spaces.
|
|
//
|
|
// The enumerator does the blocking AX attribute reads on a background queue. A
|
|
// frozen application must not freeze the caller (spec section 7.15). The caller
|
|
// runs readAXWindows and readCGWindows on a serial queue.
|
|
//
|
|
// The enumerator does NOT map an AX element to a CGWindowID. The private
|
|
// function for that map lives in PrivateAPI, and PrivateAPI is main-actor
|
|
// isolated in the foundation. The caller resolves the window IDs on the main
|
|
// actor and passes them to buildWindows. See the phase report for the reason.
|
|
struct WindowEnumerator {
|
|
|
|
// AXRawWindow holds the raw AX data of one window.
|
|
struct AXRawWindow {
|
|
let element: AXUIElement
|
|
let title: String?
|
|
let isMinimized: Bool
|
|
let subrole: String?
|
|
let isFullScreen: Bool
|
|
}
|
|
|
|
// CGRawWindow holds the raw CGWindowList data of one window.
|
|
struct CGRawWindow {
|
|
let windowID: CGWindowID
|
|
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).
|
|
struct WindowDebugRecord {
|
|
let windowID: CGWindowID
|
|
let title: String
|
|
let subrole: String?
|
|
let layer: Int
|
|
let bounds: CGRect
|
|
let spaceIDs: [UInt64]
|
|
let isMinimized: Bool
|
|
let isFullScreen: Bool
|
|
let isOnscreen: Bool
|
|
let alpha: Double
|
|
let shown: Bool
|
|
let reason: String
|
|
}
|
|
|
|
// EnumerationResult holds the result of buildWindows.
|
|
struct EnumerationResult {
|
|
let windows: [WindowInfo]
|
|
let elements: [(CGWindowID, AXUIElement)]
|
|
let debug: [WindowDebugRecord]
|
|
}
|
|
|
|
private let standardSubrole = kAXStandardWindowSubrole as String
|
|
|
|
// readAXWindows reads the AX windows of the application. The function sets
|
|
// the AX messaging timeout to 0.25 s. Run this function on a serial queue.
|
|
func readAXWindows(pid: pid_t) -> [AXRawWindow] {
|
|
let app = AXUIElementCreateApplication(pid)
|
|
AXUIElementSetMessagingTimeout(app, 0.25)
|
|
|
|
let elements = Self.copyElements(app, kAXWindowsAttribute as String)
|
|
var result: [AXRawWindow] = []
|
|
result.reserveCapacity(elements.count)
|
|
for element in elements {
|
|
let title = Self.copyString(element, kAXTitleAttribute as String)
|
|
let minimized = Self.copyBool(element, kAXMinimizedAttribute as String)
|
|
let subrole = Self.copyString(element, kAXSubroleAttribute as String)
|
|
let fullScreen = Self.copyBool(element, "AXFullScreen")
|
|
result.append(AXRawWindow(element: element,
|
|
title: title,
|
|
isMinimized: minimized,
|
|
subrole: subrole,
|
|
isFullScreen: fullScreen))
|
|
}
|
|
return result
|
|
}
|
|
|
|
// readFocusedFullScreen returns true if the focused window of the
|
|
// application is full screen. Run this function on a serial queue.
|
|
func readFocusedFullScreen(pid: pid_t) -> Bool {
|
|
let app = AXUIElementCreateApplication(pid)
|
|
AXUIElementSetMessagingTimeout(app, 0.25)
|
|
var value: CFTypeRef?
|
|
let error = AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute as CFString, &value)
|
|
guard error == .success, let value = value else { return false }
|
|
let focused = value as! AXUIElement
|
|
return Self.copyBool(focused, "AXFullScreen")
|
|
}
|
|
|
|
// readFocusedWindowElement returns the focused window element of the
|
|
// application. The caller resolves the CGWindowID on the main actor. Run
|
|
// this function on a serial queue.
|
|
func readFocusedWindowElement(pid: pid_t) -> AXUIElement? {
|
|
let app = AXUIElementCreateApplication(pid)
|
|
AXUIElementSetMessagingTimeout(app, 0.25)
|
|
var value: CFTypeRef?
|
|
let error = AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute as CFString, &value)
|
|
guard error == .success, let value = value else { return nil }
|
|
return (value as! AXUIElement)
|
|
}
|
|
|
|
// readCGWindows reads the CGWindowList windows of the application. The
|
|
// function keeps only the windows of the given PID. Run this function on a
|
|
// serial queue.
|
|
func readCGWindows(pid: pid_t) -> [CGRawWindow] {
|
|
let options: CGWindowListOption = [.optionAll, .excludeDesktopElements]
|
|
guard let list = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as? [[String: Any]] else {
|
|
return []
|
|
}
|
|
var result: [CGRawWindow] = []
|
|
for info in list {
|
|
guard let owner = (info[kCGWindowOwnerPID as String] as? NSNumber)?.int32Value,
|
|
owner == pid else { continue }
|
|
let layer = (info[kCGWindowLayer as String] as? NSNumber)?.intValue ?? -1
|
|
let windowID = (info[kCGWindowNumber as String] as? NSNumber)?.uint32Value ?? 0
|
|
let title = info[kCGWindowName as String] as? String
|
|
var bounds = CGRect.zero
|
|
if let boundsValue = info[kCGWindowBounds as String] {
|
|
let dict = boundsValue as! CFDictionary
|
|
var rect = CGRect.zero
|
|
if CGRectMakeWithDictionaryRepresentation(dict, &rect) {
|
|
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,
|
|
isOnscreen: isOnscreen,
|
|
alpha: alpha))
|
|
}
|
|
return result
|
|
}
|
|
|
|
// buildWindows merges the AX source and the CGWindowList source. The
|
|
// function applies the filter rules (spec section 7.3). The function is
|
|
// pure. The caller resolves axWindowIDs on the main actor. axWindowIDs is
|
|
// parallel to ax.
|
|
func buildWindows(pid: pid_t,
|
|
bundleID: String?,
|
|
appName: String,
|
|
ax: [AXRawWindow],
|
|
axWindowIDs: [CGWindowID?],
|
|
cg: [CGRawWindow],
|
|
spaceIDsByWindow: [CGWindowID: [UInt64]],
|
|
currentSpaceIDs: [UInt64],
|
|
ownPID: pid_t,
|
|
screenBoundsCG: CGRect = .null,
|
|
now: Date = Date()) -> EnumerationResult {
|
|
|
|
// Index the AX windows by CGWindowID.
|
|
var axByID: [CGWindowID: AXRawWindow] = [:]
|
|
for (index, window) in ax.enumerated() {
|
|
guard index < axWindowIDs.count, let id = axWindowIDs[index], id != 0 else { continue }
|
|
axByID[id] = window
|
|
}
|
|
var cgByID: [CGWindowID: CGRawWindow] = [:]
|
|
for window in cg where window.windowID != 0 {
|
|
cgByID[window.windowID] = window
|
|
}
|
|
|
|
var ids = Set<CGWindowID>()
|
|
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] = []
|
|
|
|
for id in ids {
|
|
let axWindow = axByID[id]
|
|
let cgWindow = cgByID[id]
|
|
|
|
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 {
|
|
title = axTitle
|
|
} else if let cgTitle = cgWindow?.title, cgTitle.isEmpty == false {
|
|
title = cgTitle
|
|
}
|
|
|
|
let subrole = axWindow?.subrole
|
|
let layer = cgWindow?.layer ?? 0
|
|
let bounds = cgWindow?.bounds ?? .zero
|
|
let isMinimized = axWindow?.isMinimized ?? false
|
|
let isFullScreen = axWindow?.isFullScreen ?? false
|
|
let hasAX = axWindow != nil
|
|
|
|
let decision = shouldShow(ownerPID: pid,
|
|
ownPID: ownPID,
|
|
layer: layer,
|
|
subrole: subrole,
|
|
hasAX: hasAX,
|
|
bounds: bounds,
|
|
title: title,
|
|
appName: appName,
|
|
isMinimized: isMinimized,
|
|
isOnscreen: isOnscreen,
|
|
onCurrentSpace: onCurrentSpacePositive,
|
|
screenBoundsCG: screenBoundsCG,
|
|
hasCG: cgWindow != nil)
|
|
|
|
debug.append(WindowDebugRecord(windowID: id,
|
|
title: title,
|
|
subrole: subrole,
|
|
layer: layer,
|
|
bounds: bounds,
|
|
spaceIDs: spaceIDs,
|
|
isMinimized: isMinimized,
|
|
isFullScreen: isFullScreen,
|
|
isOnscreen: cgWindow?.isOnscreen ?? false,
|
|
alpha: cgWindow?.alpha ?? 1.0,
|
|
shown: decision.show,
|
|
reason: decision.reason))
|
|
|
|
guard decision.show else { continue }
|
|
|
|
let info = WindowInfo(id: id,
|
|
pid: pid,
|
|
bundleID: bundleID,
|
|
title: title,
|
|
spaceIDs: spaceIDs,
|
|
isOnCurrentSpace: onCurrentSpace,
|
|
isMinimized: isMinimized,
|
|
isFullScreen: isFullScreen,
|
|
firstSeen: now)
|
|
windows.append(info)
|
|
|
|
if let element = axWindow?.element {
|
|
elements.append((id, element))
|
|
}
|
|
}
|
|
|
|
return EnumerationResult(windows: windows, elements: elements, debug: debug)
|
|
}
|
|
|
|
// shouldShow applies the filter rules of spec section 7.3. A window is
|
|
// shown only if all rules are true.
|
|
private func shouldShow(ownerPID: pid_t,
|
|
ownPID: pid_t,
|
|
layer: Int,
|
|
subrole: String?,
|
|
hasAX: Bool,
|
|
bounds: CGRect,
|
|
title: String,
|
|
appName: String,
|
|
isMinimized: Bool,
|
|
isOnscreen: Bool,
|
|
onCurrentSpace: Bool,
|
|
screenBoundsCG: CGRect,
|
|
hasCG: Bool) -> (show: Bool, reason: String) {
|
|
if ownerPID == ownPID {
|
|
return (false, "owned by EPOD Window Bar")
|
|
}
|
|
if hasCG, layer != 0 {
|
|
return (false, "layer != 0 (\(layer))")
|
|
}
|
|
// Off-screen check: hide a window that lies entirely outside all
|
|
// screens (for example the Eclipse "limbo" shell at y=10000). A
|
|
// minimized window keeps its tab. The user restores it with a click.
|
|
if isMinimized == false,
|
|
screenBoundsCG.isNull == false, screenBoundsCG.isEmpty == false,
|
|
bounds.isEmpty == false, bounds.intersects(screenBoundsCG) == false {
|
|
return (false, "off-screen")
|
|
}
|
|
if hasAX {
|
|
if subrole != standardSubrole {
|
|
return (false, "subrole \(subrole ?? "nil") is not standard")
|
|
}
|
|
} else {
|
|
// The window has no AX element. The window is on another Space, or
|
|
// the window is a hidden placeholder. A genuine window has a
|
|
// distinct title. A placeholder has the application name as the
|
|
// title. The title is the application name because the real title
|
|
// is empty, or because kCGWindowName is set to the application name.
|
|
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")
|
|
}
|
|
}
|
|
return (true, "shown")
|
|
}
|
|
|
|
// isOnCurrentSpace returns true if the window shares a Space with a current
|
|
// Space. If the Space data is empty, the function returns true.
|
|
private func isOnCurrentSpace(windowSpaceIDs: [UInt64], currentSpaceIDs: [UInt64]) -> Bool {
|
|
if windowSpaceIDs.isEmpty || currentSpaceIDs.isEmpty { return true }
|
|
for id in windowSpaceIDs where currentSpaceIDs.contains(id) {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// MARK: - AX attribute helpers
|
|
|
|
private static func copyString(_ element: AXUIElement, _ attribute: String) -> String? {
|
|
var value: CFTypeRef?
|
|
guard AXUIElementCopyAttributeValue(element, attribute as CFString, &value) == .success,
|
|
let value = value else { return nil }
|
|
return value as? String
|
|
}
|
|
|
|
private static func copyBool(_ element: AXUIElement, _ attribute: String) -> Bool {
|
|
var value: CFTypeRef?
|
|
guard AXUIElementCopyAttributeValue(element, attribute as CFString, &value) == .success,
|
|
let value = value else { return false }
|
|
if CFGetTypeID(value) == CFBooleanGetTypeID() {
|
|
return CFBooleanGetValue((value as! CFBoolean))
|
|
}
|
|
if let number = value as? NSNumber {
|
|
return number.boolValue
|
|
}
|
|
return false
|
|
}
|
|
|
|
private static func copyElements(_ element: AXUIElement, _ attribute: String) -> [AXUIElement] {
|
|
var value: CFTypeRef?
|
|
guard AXUIElementCopyAttributeValue(element, attribute as CFString, &value) == .success,
|
|
let value = value else { return [] }
|
|
return value as? [AXUIElement] ?? []
|
|
}
|
|
}
|