public struct TabViewCardGeometry: Sendable, Equatable { public enum ValidationError: Error, Sendable, Equatable { case invalidViewport case invalidCard(Int) case geometryOutOfRange } public static let minimumSize = Size(width: 180, height: 64) public static let spacing: Double = 8 public static let margin: Double = 16 public let frames: [Rect] public let contentFrame: Rect public let usesOverflowLayout: Bool /// Keeps readable spatial cards; dense cards reflow in input order rather than shrinking or overlapping. /// All coordinates remain global. The content frame may extend beyond the viewport for scrolling. public init(frames proposed: [Rect], viewport: Rect) throws { guard Self.valid(viewport) else { throw ValidationError.invalidViewport } for (index, frame) in proposed.enumerated() { guard Self.valid(frame) else { throw ValidationError.invalidCard(index) } } guard (viewport.size.width * viewport.size.width + viewport.size.height * viewport.size.height).isFinite else { throw ValidationError.geometryOutOfRange } let readable = proposed.allSatisfy { frame in frame.size.width >= Self.minimumSize.width && frame.size.height >= Self.minimumSize.height && frame.minX >= viewport.minX && frame.minY >= viewport.minY && frame.maxX <= viewport.maxX && frame.maxY <= viewport.maxY } if readable && !Self.overlap(proposed) { frames = proposed contentFrame = viewport usesOverflowLayout = false return } let availableWidth = max(Self.minimumSize.width, viewport.size.width - Self.margin * 2) let fittingColumns = ((availableWidth + Self.spacing) / (Self.minimumSize.width + Self.spacing)).rounded(.down) let columns = Int(min(Double(proposed.count), fittingColumns)) let rows = 1 + (proposed.count - 1) / columns let width = Self.margin * 2 + Double(columns) * Self.minimumSize.width + Double(columns - 1) * Self.spacing let height = Self.margin * 2 + Double(rows) * Self.minimumSize.height + Double(rows - 1) * Self.spacing let content = Rect( origin: viewport.origin, size: Size(width: max(viewport.size.width, width), height: max(viewport.size.height, height))) guard Self.valid(content), (content.size.width * content.size.width + content.size.height * content.size.height).isFinite else { throw ValidationError.geometryOutOfRange } var arranged: [Rect] = [] arranged.reserveCapacity(proposed.count) for index in proposed.indices { let frame = Rect( x: viewport.minX + Self.margin + Double(index % columns) * (Self.minimumSize.width + Self.spacing), y: viewport.minY + Self.margin + Double(index / columns) * (Self.minimumSize.height + Self.spacing), width: Self.minimumSize.width, height: Self.minimumSize.height) guard Self.valid(frame), frame.maxX <= content.maxX, frame.maxY <= content.maxY else { throw ValidationError.geometryOutOfRange } arranged.append(frame) } frames = arranged contentFrame = content usesOverflowLayout = true } private static func valid(_ frame: Rect) -> Bool { frame.isValid && frame.size.width > 0 && frame.size.height > 0 && frame.maxX.isFinite && frame.maxY.isFinite && frame.maxX > frame.minX && frame.maxY > frame.minY && frame.center.xCoordinate.isFinite && frame.center.yCoordinate.isFinite } private static func overlap(_ frames: [Rect]) -> Bool { for (index, frame) in frames.enumerated() { for other in frames.dropFirst(index + 1) { if min(frame.maxX, other.maxX) > max(frame.minX, other.minX) && min(frame.maxY, other.maxY) > max(frame.minY, other.minY) { return true } } } return false } } public struct TabViewSelection: Sendable, Equatable { public struct Entry: Sendable, Equatable { public let window: WindowID public let display: DisplayID public let frame: Rect public init(window: WindowID, display: DisplayID, frame: Rect) { self.window = window self.display = display self.frame = frame } } public enum Step: Sendable, Equatable { case forward, backward } public enum ValidationError: Error, Sendable, Equatable { case duplicateWindow(WindowID) case invalidFrame(WindowID) case geometryOutOfRange case unknownFocusedWindow(WindowID) } public let entries: [Entry] private var selectedIndex: Int? public var selectedWindow: WindowID? { selectedIndex.map { entries[$0].window } } /// Forward follows the list toward its beginning; backward follows it toward its end. /// Without a focused window, the first entry is selected (the last when opening backward). /// An empty list has no selection, regardless of focus. A nonempty list rejects an unknown focus. /// Frames and the combined center range must be representable by directional distance calculations. public init(entries: [Entry], focusedWindow: WindowID?, opening: Step = .forward) throws { var seen: Set = [] for entry in entries { guard seen.insert(entry.window).inserted else { throw ValidationError.duplicateWindow(entry.window) } let frame = entry.frame guard frame.isValid, frame.size.width > 0, frame.size.height > 0, frame.maxX.isFinite, frame.maxY.isFinite, frame.maxX > frame.minX, frame.maxY > frame.minY, frame.center.xCoordinate.isFinite, frame.center.yCoordinate.isFinite else { throw ValidationError.invalidFrame(entry.window) } } // DirectionalDistance squares center offsets, so both offsets and their sum must remain finite. if let first = entries.first { var minX = first.frame.center.xCoordinate var maxX = minX var minY = first.frame.center.yCoordinate var maxY = minY for entry in entries.dropFirst() { let center = entry.frame.center minX = min(minX, center.xCoordinate) maxX = max(maxX, center.xCoordinate) minY = min(minY, center.yCoordinate) maxY = max(maxY, center.yCoordinate) } let width = maxX - minX let height = maxY - minY guard (width * width + height * height).isFinite else { throw ValidationError.geometryOutOfRange } } self.entries = entries guard !entries.isEmpty else { selectedIndex = nil return } if let focusedWindow { guard let index = entries.firstIndex(where: { $0.window == focusedWindow }) else { throw ValidationError.unknownFocusedWindow(focusedWindow) } selectedIndex = Self.steppedIndex(index, count: entries.count, step: opening) } else { selectedIndex = opening == .forward ? 0 : entries.count - 1 } } /// Returns false and leaves the selection unchanged when the window is not an entry. @discardableResult public mutating func select(_ window: WindowID) -> Bool { guard let index = entries.firstIndex(where: { $0.window == window }) else { return false } selectedIndex = index return true } /// Advances with wraparound, returning the new selection or nil for an empty list. @discardableResult public mutating func step(_ step: Step) -> WindowID? { guard let index = selectedIndex else { return nil } selectedIndex = Self.steppedIndex(index, count: entries.count, step: step) return selectedWindow } /// Uses directional alignment and distance across all displays. Ties follow entry order. @discardableResult public mutating func move(_ direction: Direction) -> Bool { guard let index = selectedIndex else { return false } let source = entries[index].frame var best: (index: Int, distance: DirectionalDistance)? for (candidateIndex, candidate) in entries.enumerated() where candidateIndex != index { guard let distance = direction.distance(from: source, to: candidate.frame) else { continue } if let current = best, !(distance < current.distance) { continue } best = (candidateIndex, distance) } guard let best else { return false } selectedIndex = best.index return true } private static func steppedIndex(_ index: Int, count: Int, step: Step) -> Int { switch step { case .forward: index == 0 ? count - 1 : index - 1 case .backward: index == count - 1 ? 0 : index + 1 } } }