import Foundation public enum WindowDisposition: String, Sendable, Equatable, Codable { case tile, float, ignore } public enum ClassificationReason: Sendable, Equatable, Codable { case standardWindow case fullScreen case minimized case notWindow case helper case dialog case sheet case panel case nonResizable case fixedSize case elevatedLayer case tinyWindow case pictureInPicture case quickTerminal case launcher case quickLook case splash case defaultFloatApp(bundleID: String) case nativeTab case legacyRule(bundleID: String) case manual case whenFull case rule(index: Int, bundleID: String) } public struct WindowClassification: Sendable, Equatable, Codable { public var disposition: WindowDisposition public var reason: ClassificationReason public var display: DisplayID? public var position: Int? public var layout: Layout? public var space: Int? public var moveToSpace: Bool public init( disposition: WindowDisposition, reason: ClassificationReason, display: DisplayID? = nil, position: Int? = nil, layout: Layout? = nil, space: Int? = nil, moveToSpace: Bool = false ) { self.disposition = disposition self.reason = reason self.display = display self.position = position self.layout = layout self.space = space self.moveToSpace = moveToSpace } private enum CodingKeys: String, CodingKey { case disposition, reason, display, position, layout, space, moveToSpace } public init(from decoder: any Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) disposition = try values.decode(WindowDisposition.self, forKey: .disposition) reason = try values.decode(ClassificationReason.self, forKey: .reason) display = try values.decodeIfPresent(DisplayID.self, forKey: .display) position = try values.decodeIfPresent(Int.self, forKey: .position) layout = try values.decodeIfPresent(Layout.self, forKey: .layout) space = try values.decodeIfPresent(Int.self, forKey: .space) moveToSpace = try values.decodeIfPresent(Bool.self, forKey: .moveToSpace) ?? false } } public enum WindowRuleError: Error, Sendable, Equatable { case missingMatch case missingAction case conflictingTitleMatchers case invalidPosition case invalidSpace } public struct WindowRule: Sendable { public let bundleID: String? public let appName: String? public let titleContains: String? public let titleRegex: String? public let role: String? public let subrole: String? public let action: WindowDisposition? public let display: DisplayID? public let position: Int? public let layout: Layout? public let space: Int? public let moveToSpace: Bool private let regex: NSRegularExpression? public init( bundleID: String? = nil, appName: String? = nil, titleContains: String? = nil, titleRegex: String? = nil, role: String? = nil, subrole: String? = nil, action: WindowDisposition? = nil, display: DisplayID? = nil, position: Int? = nil, layout: Layout? = nil, space: Int? = nil, moveToSpace: Bool = false ) throws { guard [bundleID, appName, titleContains, titleRegex, role, subrole].contains(where: { $0 != nil }) else { throw WindowRuleError.missingMatch } guard action != nil || display != nil || position != nil || layout != nil || space != nil else { throw WindowRuleError.missingAction } guard space.map({ (1...16).contains($0) }) != false else { throw WindowRuleError.invalidSpace } guard titleContains == nil || titleRegex == nil else { throw WindowRuleError.conflictingTitleMatchers } guard position.map({ $0 >= 0 }) != false else { throw WindowRuleError.invalidPosition } self.bundleID = bundleID self.appName = appName self.titleContains = titleContains self.titleRegex = titleRegex self.role = role self.subrole = subrole self.action = action self.display = display self.position = position self.layout = layout self.space = space self.moveToSpace = moveToSpace regex = try titleRegex.map { try NSRegularExpression(pattern: $0) } } public func matches(_ facts: WindowFacts) -> Bool { if let bundleID, facts.bundleID != bundleID { return false } if let appName, facts.appName != appName { return false } if let role, facts.role != role { return false } if let subrole, facts.subrole != subrole { return false } if let titleContains, facts.title?.contains(titleContains) != true { return false } if let regex { guard let title = facts.title else { return false } let range = NSRange(title.startIndex..