Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import ApplicationServicesimport CoreGraphics
struct WindowParentObservation: Equatable { var hasParent: Bool? var windowID: CGWindowID?}
enum WindowParentReader { static let ancestorLimit = 8
static func isWindowRole(_ role: String) -> Bool { [kAXWindowRole, kAXSheetRole, "AXDialog"].contains(role) }
static func read<Node>( window: Node, parent: (Node) throws -> Node?, containingWindow: (Node) throws -> Node?, role: (Node) throws -> String?, same: (Node, Node) -> Bool, identify: (Node) throws -> CGWindowID? ) throws -> WindowParentObservation { var visited = [window] var current = window var reachedApplication = false var unknownRole = false for _ in 0..<ancestorLimit { guard let candidate = try parent(current) else { break } guard !visited.contains(where: { same($0, candidate) }) else { break } visited.append(candidate) if let role = try role(candidate) { if isWindowRole(role) { return WindowParentObservation(hasParent: true, windowID: try identify(candidate)) } if role == kAXApplicationRole { // A top-level window's AXWindow attribute normally points to itself. reachedApplication = true break } } else { unknownRole = true } current = candidate } if let candidate = try containingWindow(window), !same(candidate, window), let role = try role(candidate), isWindowRole(role) { return WindowParentObservation(hasParent: true, windowID: try identify(candidate)) } // Exhausted, missing, or cyclic ancestry cannot prove that there is no owner. return WindowParentObservation(hasParent: reachedApplication && !unknownRole ? false : nil) }}