Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194import ApplicationServicespublic import CoreGraphicsimport Foundationpublic 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<Value>(_ 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<Value>(_ 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() }}