import ApplicationServices public import CoreGraphics import Foundation public import ParcelCore /// App and window-server data already obtained by the caller, without additional AX calls. public struct WindowFactsContext: Sendable { public var bundleID: String public var appName: String public var activationPolicy: AppActivationPolicy? public var isHelper: Bool? public var level: Int? public var layer: Int? public var serverSize: Size? public init( bundleID: String, appName: String = "", activationPolicy: AppActivationPolicy? = nil, isHelper: Bool? = nil, level: Int? = nil, layer: Int? = nil, serverSize: Size? = nil ) { self.bundleID = bundleID self.appName = appName self.activationPolicy = activationPolicy self.isHelper = isHelper self.level = level self.layer = layer self.serverSize = serverSize } } public enum WindowFactsReader { /// Call with a window obtained from the application inside its owning AppThread.perform closure. public static func gather(window: AXElement, context: WindowFactsContext) throws -> WindowFacts { guard !Thread.isMainThread else { throw AXPlatformError.unavailable("Window facts must be read on the owning app thread") } let window = try window.cachingAttributes(attributes) return try gather(source: AXWindowFactsAccess(window: window), context: context) } /// Resolve an auxiliary's owner on its app thread without moving or focusing either window. public static func parentWindowID(window: AXElement) throws -> CGWindowID? { guard !Thread.isMainThread else { throw AXPlatformError.unavailable("Window parents must be read on the owning app thread") } return try window.factParentWindow().windowID } public static func gatherCreatedWindow( window: AXElement, windowID: CGWindowID, context: WindowFactsContext, isOnScreen: Bool ) throws -> AppThreadCreatedWindow { guard !Thread.isMainThread else { throw AXPlatformError.unavailable("Window facts must be read on the owning app thread") } let window = try window.cachingAttributes(attributes) return try gatherCreatedWindow( source: AXWindowFactsAccess(window: window), windowID: windowID, context: context, isOnScreen: isOnScreen, frame: { try window.frame }) } static func gatherCreatedWindow( source: some WindowFactsAccess, windowID: CGWindowID, context: WindowFactsContext, isOnScreen: Bool, frame: () throws -> CGRect ) throws -> AppThreadCreatedWindow { let parent = try optionalWindowFact { try source.parentWindow() } let facts = try gather(source: source, context: context, parent: parent) return AppThreadCreatedWindow( windowID: windowID, facts: facts, frame: try frame(), isOnScreen: isOnScreen, parentWindowID: parent?.windowID) } static let attributes = [ kAXRoleAttribute, kAXSubroleAttribute, kAXTitleAttribute, kAXModalAttribute, "AXMinSize", "AXMaxSize", "AXFullScreen", kAXMinimizedAttribute, kAXPositionAttribute, kAXSizeAttribute, ] static func gather(source: some WindowFactsAccess, context: WindowFactsContext) throws -> WindowFacts { let parent = try optionalWindowFact { try source.parentWindow() } return try gather(source: source, context: context, parent: parent) } private static func gather( source: some WindowFactsAccess, context: WindowFactsContext, parent: WindowParentObservation? ) throws -> WindowFacts { let role = try optionalWindowFact { try source.string(.role) } let subrole = try optionalWindowFact { try source.string(.subrole) } let title = try optionalWindowFact { try source.string(.title) } let close = try optionalWindowFact { try source.button(.close) } let minimize = try optionalWindowFact { try source.button(.minimize) } let zoom = try optionalWindowFact { try source.button(.zoom) } let fullScreenButton = try optionalWindowFact { try source.button(.fullScreen) } let resizable = try optionalWindowFact { try source.sizeSettable() } let modal = try optionalWindowFact { try source.boolean(.modal) } let childRoles = try optionalBoundedWindowFact { try source.childRoles() } let minimumSize = try optionalWindowFact { try source.size(.minimum) } let maximumSize = try optionalWindowFact { try source.size(.maximum) } let fullScreen = try optionalWindowFact { try source.boolean(.fullScreen) } let minimized = try optionalWindowFact { try source.boolean(.minimized) } let nativeTabs = try optionalBoundedWindowFact { try source.nativeTabs() } return WindowFacts( bundleID: context.bundleID, appName: context.appName, activationPolicy: context.activationPolicy, isHelper: context.isHelper, role: role, subrole: subrole, title: title, hasCloseButton: close, hasMinimizeButton: minimize, hasZoomButton: zoom, hasFullScreenButton: fullScreenButton, isResizable: resizable, isModal: modal, hasParent: parent?.hasParent, hasSheet: childRoles.flatMap(sheetPresence), minimumSize: minimumSize, maximumSize: maximumSize, isFullScreen: fullScreen, isMinimized: minimized, level: context.level, layer: context.layer, serverSize: context.serverSize, hasNativeTabs: nativeTabs?.hasNativeTabs, nativeTabGroup: nativeTabs?.group ) } static func sheetPresence(_ roles: [String?]) -> Bool? { if roles.contains(where: { $0 == kAXSheetRole }) { return true } return roles.contains(where: { $0 == nil }) ? nil : false } } func optionalBoundedWindowFact(_ read: () throws -> Value) throws -> Value? { do { return try optionalWindowFact(read) } catch NativeTabScanError.limitExceeded { // A bounded tree scan cannot prove absence when the remote tree exceeds its budget. return nil } } func optionalWindowFact(_ read: () throws -> Value) throws -> Value? { do { return try read() } catch AXPlatformError.attributeUnsupported { return nil } catch AXPlatformError.noValue { return nil } } enum WindowStringFact: String { case role = "AXRole" case subrole = "AXSubrole" case title = "AXTitle" } enum WindowButtonFact: String { case close = "AXCloseButton" case minimize = "AXMinimizeButton" case zoom = "AXZoomButton" case fullScreen = "AXFullScreenButton" } enum WindowBooleanFact: String { case modal = "AXModal" case fullScreen = "AXFullScreen" case minimized = "AXMinimized" } enum WindowSizeFact: String { case minimum = "AXMinSize" case maximum = "AXMaxSize" } protocol WindowFactsAccess { func string(_ attribute: WindowStringFact) throws -> String func button(_ attribute: WindowButtonFact) throws -> Bool func boolean(_ attribute: WindowBooleanFact) throws -> Bool func size(_ attribute: WindowSizeFact) throws -> Size func sizeSettable() throws -> Bool func parentRole() throws -> String func parentWindow() throws -> WindowParentObservation func childRoles() throws -> [String?] func nativeTabs() throws -> NativeTabObservation } extension WindowFactsAccess { func parentWindow() throws -> WindowParentObservation { WindowParentObservation(hasParent: WindowParentReader.isWindowRole(try parentRole())) } func nativeTabs() throws -> NativeTabObservation { throw AXPlatformError.attributeUnsupported } } private struct AXWindowFactsAccess: WindowFactsAccess { let window: AXElement func string(_ attribute: WindowStringFact) throws -> String { try window.factString(attribute.rawValue) } func button(_ attribute: WindowButtonFact) throws -> Bool { try window.factElementPresent(attribute.rawValue) } func boolean(_ attribute: WindowBooleanFact) throws -> Bool { try window.factBoolean(attribute.rawValue) } func size(_ attribute: WindowSizeFact) throws -> Size { let size = try window.factSize(attribute.rawValue) return Size(width: Double(size.width), height: Double(size.height)) } func sizeSettable() throws -> Bool { try window.factSizeSettable() } func parentRole() throws -> String { try window.factParentRole() } func parentWindow() throws -> WindowParentObservation { try window.factParentWindow() } func childRoles() throws -> [String?] { try window.factChildRoles() } func nativeTabs() throws -> NativeTabObservation { try window.factNativeTabs() } }