public struct WindowID: RawRepresentable, Sendable, Equatable, Hashable, Codable, Comparable { public let rawValue: UInt32 public init(rawValue: UInt32) { self.rawValue = rawValue } public static func < (lhs: Self, rhs: Self) -> Bool { lhs.rawValue < rhs.rawValue } } public struct AppID: Sendable, Equatable, Hashable, Codable { public let pid: Int32 public let bundleID: String public init(pid: Int32, bundleID: String) { self.pid = pid self.bundleID = bundleID } } public struct DisplayID: RawRepresentable, Sendable, Equatable, Hashable, Codable, Comparable { public let rawValue: String public init(rawValue: String) { self.rawValue = rawValue } public static func < (lhs: Self, rhs: Self) -> Bool { lhs.rawValue < rhs.rawValue } } public struct SpaceID: RawRepresentable, Sendable, Equatable, Hashable, Codable, Comparable { public let rawValue: UInt64 public init(rawValue: UInt64) { self.rawValue = rawValue } public static func < (lhs: Self, rhs: Self) -> Bool { lhs.rawValue < rhs.rawValue } } public struct WorkspaceKey: Sendable, Equatable, Hashable, Codable, Comparable { public let display: DisplayID public let space: SpaceID public init(display: DisplayID, space: SpaceID) { self.display = display self.space = space } public static func < (lhs: Self, rhs: Self) -> Bool { lhs.display == rhs.display ? lhs.space < rhs.space : lhs.display < rhs.display } } public struct LearnedMinimumSize: Sendable, Equatable, Codable { public static let currentVersion = 1 public let schemaVersion: Int public let size: Size public init(size: Size, schemaVersion: Int = LearnedMinimumSize.currentVersion) { self.size = size self.schemaVersion = schemaVersion } } public enum WindowManagementReason: String, Sendable, Equatable, Codable { case automatic, user, rule, whenFull } public struct ManagedWindow: Sendable, Equatable, Codable { public let id: WindowID public var app: AppID public var title: String public var role: String public var subrole: String? public var minimum: LearnedMinimumSize? public var isFloating: Bool public var managementReason: WindowManagementReason public var facts: WindowFacts? public var initialPosition: Int? package var isRestoringMinimized = false public var minimumSize: Size? { minimum?.size } public init( id: WindowID, app: AppID, title: String = "", role: String = "AXWindow", subrole: String? = nil, minimumSize: Size? = nil, isFloating: Bool = false, facts: WindowFacts? = nil, initialPosition: Int? = nil, managementReason: WindowManagementReason = .automatic ) { self.id = id self.app = app self.title = title self.role = role self.subrole = subrole minimum = minimumSize.map { LearnedMinimumSize(size: $0) } self.isFloating = isFloating self.managementReason = managementReason self.facts = facts self.initialPosition = initialPosition } } public struct Display: Sendable, Equatable, Codable { public let id: DisplayID public var frame: Rect public var fullFrame: Rect public var physicalFrame: Rect? { get { fullFrame } set { fullFrame = newValue ?? frame } } public init(id: DisplayID, frame: Rect, fullFrame: Rect? = nil) { self.id = id self.frame = frame self.fullFrame = fullFrame ?? frame } public init(id: DisplayID, frame: Rect, physicalFrame: Rect?) { self.init(id: id, frame: frame, fullFrame: physicalFrame) } public var isValid: Bool { frame.isValid && fullFrame.isValid && fullFrame.minX <= frame.minX && fullFrame.minY <= frame.minY && fullFrame.maxX >= frame.maxX && fullFrame.maxY >= frame.maxY } public var hasUsableGeometry: Bool { isValid && frame.size.width > 0 && frame.size.height > 0 && fullFrame.size.width > 0 && fullFrame.size.height > 0 } private enum CodingKeys: String, CodingKey { case id, frame, fullFrame, physicalFrame } public init(from decoder: any Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) let fullFrame = try values.decodeIfPresent(Rect.self, forKey: .fullFrame) let physicalFrame = try values.decodeIfPresent(Rect.self, forKey: .physicalFrame) if let fullFrame, let physicalFrame, fullFrame != physicalFrame { throw DecodingError.dataCorruptedError( forKey: .fullFrame, in: values, debugDescription: "Display bounds must agree") } self.init( id: try values.decode(DisplayID.self, forKey: .id), frame: try values.decode(Rect.self, forKey: .frame), fullFrame: fullFrame ?? physicalFrame) } public func encode(to encoder: any Encoder) throws { var values = encoder.container(keyedBy: CodingKeys.self) try values.encode(id, forKey: .id) try values.encode(frame, forKey: .frame) try values.encode(fullFrame, forKey: .fullFrame) } } public enum Layout: String, Sendable, Equatable, Codable, CaseIterable { case grid, columns, rows, side, tree, bsp, accordion var preservesTree: Bool { self == .tree || self == .bsp || self == .accordion } public var next: Layout { switch self { case .grid: .columns case .columns: .rows case .rows: .side case .side: .grid case .tree: .bsp case .bsp: .accordion case .accordion: .grid } } } public enum WhenDisplayFull: String, Sendable, Equatable, Codable, CaseIterable { case swap, stack } public struct MainRatio: Sendable, Equatable, Codable { public let value: Double public static let half = MainRatio(1.0 / 2) public static let threeFifths = MainRatio(3.0 / 5) public static let twoThirds = MainRatio(2.0 / 3) public static let bounds = 0.2...0.8 public init(_ value: Double) { precondition(value.isFinite, "A ratio must be finite") self.value = min(Self.bounds.upperBound, max(Self.bounds.lowerBound, value)) } public init(from decoder: any Decoder) throws { let container = try decoder.singleValueContainer() let value = try container.decode(Double.self) guard value.isFinite else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "A ratio must be finite") } self.init(value) } public func encode(to encoder: any Encoder) throws { var container = encoder.singleValueContainer() try container.encode(value) } } public struct Gaps: Sendable, Equatable, Codable { public var inner: Double public var outer: Insets public init(inner: Double = 0, outer: Insets = Insets()) { self.inner = inner self.outer = outer } } public struct WindowStack: Sendable, Equatable, Codable { public private(set) var windows: [WindowID] public private(set) var visible: WindowID public internal(set) var isUserMade: Bool // Oldest to newest; selection never reorders presentation. private(set) var recency: [WindowID] public init(_ first: WindowID, others: [WindowID] = [], isUserMade: Bool = false) { self.isUserMade = isUserMade var seen: Set = [first] windows = [first] + others.filter { seen.insert($0).inserted } visible = first recency = Array(windows.dropFirst()) + [first] } private enum CodingKeys: String, CodingKey { case windows, visible, recency, isUserMade } public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let windows = try container.decode([WindowID].self, forKey: .windows) let visible = try container.decode(WindowID.self, forKey: .visible) guard !windows.isEmpty, Set(windows).count == windows.count, windows.contains(visible) else { throw DecodingError.dataCorruptedError( forKey: .windows, in: container, debugDescription: "A stack needs unique members and a visible member") } let recency: [WindowID] if container.contains(.recency) { recency = try container.decode([WindowID].self, forKey: .recency) guard recency.count == windows.count, Set(recency) == Set(windows), recency.last == visible else { throw DecodingError.dataCorruptedError( forKey: .recency, in: container, debugDescription: "Stack recency must contain each member once, with the visible member last") } } else { // Old payloads have no selection history; retain their last-hidden fallback. recency = windows.filter { $0 != visible } + [visible] } self.windows = windows self.visible = visible self.recency = recency if container.contains(.isUserMade) { isUserMade = try container.decode(Bool.self, forKey: .isUserMade) } else { isUserMade = false } } func memoryRemapping(_ mapping: [WindowID: WindowID]) -> WindowStack? { let members = windows.compactMap { mapping[$0] } guard let first = members.first else { return nil } var result = WindowStack(first, others: Array(members.dropFirst()), isUserMade: isUserMade) result.recency = recency.compactMap { mapping[$0] } guard let visible = result.recency.last else { preconditionFailure("A remapped stack must retain recency") } result.visible = visible return result } mutating func append(_ window: WindowID) { insert(window, before: nil) } mutating func insert(_ window: WindowID, before next: WindowID?) { guard !windows.contains(window) else { return } let index = next.flatMap { windows.firstIndex(of: $0) } ?? windows.count windows.insert(window, at: index) recency.append(window) visible = window } mutating func select(_ window: WindowID) { guard visible != window, windows.contains(window) else { return } recency.removeAll { $0 == window } recency.append(window) visible = window } mutating func cycle(forward: Bool) { guard let index = windows.firstIndex(of: visible) else { preconditionFailure("A stack must contain its visible member") } select(windows[(index + (forward ? 1 : windows.count - 1)) % windows.count]) } func removing(_ window: WindowID) -> WindowStack? { guard windows.contains(window) else { return self } guard windows.count > 1 else { return nil } var result = self result.windows.removeAll { $0 == window } result.recency.removeAll { $0 == window } guard let visible = result.recency.last else { preconditionFailure("A nonempty stack must retain selection history") } result.visible = visible return result } func replacing(_ old: WindowID, with new: WindowID) -> WindowStack { guard windows.contains(old), old != new else { return self } let replacement: (WindowID) -> WindowID = { member in if member == old { new } else if member == new { old } else { member } } var result = self result.windows = windows.map(replacement) result.recency = recency.map(replacement) result.visible = replacement(visible) return result } }