EPOD Window Bar: a window strip above the macOS Dock
EPOD Window Bar is a macOS agent app. It shows a thin strip above the Dock with one tab per window of the frontmost application. One click raises the window. Build from source with XcodeGen and Xcode. Signing uses Config.xcconfig (ad-hoc by default; a local Local.xcconfig can set a team ID). The app needs Accessibility and Screen Recording permissions, and runs in a reduced mode without Screen Recording. See README.md and docs/ for details.
This commit is contained in:
325
Sources/Tracking/WindowEnumerator.swift
Normal file
325
Sources/Tracking/WindowEnumerator.swift
Normal file
@@ -0,0 +1,325 @@
|
||||
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
|
||||
}
|
||||
|
||||
// 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 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
|
||||
}
|
||||
}
|
||||
result.append(CGRawWindow(windowID: windowID,
|
||||
title: title,
|
||||
layer: layer,
|
||||
bounds: bounds))
|
||||
}
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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,
|
||||
screenBoundsCG: screenBoundsCG,
|
||||
hasCG: cgWindow != nil)
|
||||
|
||||
debug.append(WindowDebugRecord(windowID: id,
|
||||
title: title,
|
||||
subrole: subrole,
|
||||
layer: layer,
|
||||
bounds: bounds,
|
||||
spaceIDs: spaceIDs,
|
||||
isMinimized: isMinimized,
|
||||
isFullScreen: isFullScreen,
|
||||
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,
|
||||
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")
|
||||
}
|
||||
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] ?? []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user