public import Foundation public import ParcelCore public struct StackChip: Equatable, Sendable { public let window: WindowID public let title: String public let selected: Bool public let frame: CGRect public let index: Int public let count: Int public let indicatorFrame: CGRect public let active: Bool public var accessibilityLabel: String { String(format: UIStrings.text("%@, tab %lld of %lld"), title, Int64(index + 1), Int64(count)) } } public struct StackChipGroup: Equatable, Sendable { public let windows: [WindowID] public let frame: CGRect public let chips: [StackChip] public let active: Bool public let visible: WindowID public var countText: String { windows.count.formatted() } public var countFrame: CGRect { guard !active else { return .zero } let width = StackChips.countWidth(for: windows.count) return CGRect(x: frame.width - width, y: 0, width: width, height: frame.height) } public var accessibilityLabel: String { StackText.stackedWindows(windows.count) } } public enum StackChips { static let minimumControlHeight = 16.0 static func countWidth(for count: Int) -> Double { 16 + Double(count.formatted().count) * 8 } public static func groups(in state: State, expanding window: WindowID? = nil) -> [StackChipGroup] { let expanded = groups(in: state, expanding: window, compact: false) guard let window, !expanded.contains(where: { $0.windows.contains(window) }) else { return expanded } return expanded + groups(in: state, compact: true).filter { $0.windows.contains(window) } } static func groups(in state: State, expanding window: WindowID? = nil, compact: Bool) -> [StackChipGroup] { let occupied = state.visibleWindows.compactMap { state.observedFrames[$0]?.learningRect } var used: [CGRect] = [] return state.workspaces.keys.sorted().filter(state.isActive).flatMap { key -> [StackChipGroup] in guard let workspace = state.workspaces[key], let display = state.displays[key.display]?.frame.learningRect else { return [] } // An unobserved visible window can occupy any gap on its display. guard !state.visibleWindows.contains(where: { state.observedFrames[$0]?.learningRect.isFiniteAndPositive != true && (state.workspace(containing: $0)?.display == key.display || state.workspace(containing: $0) == nil) }) else { return [] } let stacks = workspace.tree?.stacks ?? [] return stacks.compactMap { stack in guard stack.windows.count > 1, let tile = state.frames[stack.visible]?.learningRect, tile.isFiniteAndPositive, display.isFiniteAndPositive else { return nil } let overlaps = occupied.filter { $0.minX < tile.maxX && $0.maxX > tile.minX && $0 != tile } let top = overlaps.filter { $0.maxY <= tile.minY }.map(\.maxY).max() ?? display.minY let bottom = overlaps.filter { $0.minY >= tile.maxY }.map(\.minY).min() ?? display.maxY let above = max(0, tile.minY - top) let below = max(0, bottom - tile.maxY) let expanding = !compact && (window.map(stack.windows.contains) ?? false) let active = !compact && ((state.focusedWindow.map(stack.windows.contains) ?? false) || expanding) let navigationWidth = active ? 66.0 : countWidth(for: stack.windows.count) let width = min( expanding ? display.width : tile.width, Double(stack.windows.count) * (active ? 140 : 22) + navigationWidth) guard width >= navigationWidth + Double(stack.windows.count) * (active ? 8 : 12) else { return nil } let x = min(max(tile.minX, display.minX), display.maxX - width) let upper = CGRect(x: x, y: tile.minY - min(24, above), width: width, height: min(24, above)) let lower = CGRect(x: x, y: tile.maxY, width: width, height: min(24, below)) let candidates = above >= below ? [upper, lower] : [lower, upper] guard let frame = candidates.first(where: { candidate in candidate.height >= minimumControlHeight && display.contains(candidate) && !(occupied + used).contains(where: { $0.intersects(candidate) }) }) else { return nil } used.append(frame) let height = frame.height let cellWidth = (width - navigationWidth) / Double(stack.windows.count) let chips = stack.windows.enumerated().map { index, id in let name = state.windows[id]?.title ?? "" let selected = id == stack.visible let x = (active ? 22 : 0) + Double(index) * cellWidth let indicatorWidth = active ? min(cellWidth - (selected ? 4 : 6), selected ? 24 : 10) : min(12, cellWidth - 4) let indicatorHeight = active ? 3.0 : (selected ? 5.0 : 3.0) return StackChip( window: id, title: name.isEmpty ? UIStrings.text("Untitled window") : name, selected: selected, frame: CGRect(x: x, y: 0, width: cellWidth, height: height), index: index, count: stack.windows.count, indicatorFrame: CGRect( x: x + (cellWidth - indicatorWidth) / 2, y: active ? 1 : (height - indicatorHeight) / 2, width: indicatorWidth, height: indicatorHeight), active: active) } return StackChipGroup( windows: stack.windows, frame: frame, chips: chips, active: active, visible: stack.visible) } } } }