import Foundation import ParcelConfig struct MinimumStackGeometry: Codable, Sendable { let minimumWidths: [Double] let maximumWidths: [Double?]? let reservedLeft: Double let reservedRight: Double let visible: ScreenRect let innerGap: Double let outerGap: Double var gaps: LayoutGaps { LayoutGaps(inner: innerGap, outer: outerGap) } init( minimumWidths: [Double], maximumWidths: [Double?]? = nil, screen: ScreenRect, gaps: LayoutGaps = RealityGeometry.defaultGaps ) throws { guard screen.isValid, minimumWidths.count == 3, minimumWidths.allSatisfy({ $0.isFinite && $0 > 0 }), maximumWidths.map({ widths in widths.count == minimumWidths.count && zip(widths, minimumWidths).allSatisfy { maximum, minimum in maximum.map { $0.isFinite && $0 > 0 && $0 >= minimum - 1 } ?? true } }) ?? true else { throw RealityError("Minimum stacking needs three measured positive widths and a valid screen") } let widths = minimumWidths.sorted() let pair = widths[1] + widths[2] + gaps.inner let triple = widths.reduce(0, +) + 2 * gaps.inner let available = min(screen.inset(gaps.outer).width, ((pair + triple) / 2).rounded(.down)) let reserve = (screen.inset(gaps.outer).width - available).rounded(.up) innerGap = gaps.inner outerGap = gaps.outer guard available >= pair, available < triple, reserve <= 400 else { throw RealitySkip("Measured app minimums cannot establish two fitting tiles but not three on this display") } self.minimumWidths = minimumWidths self.maximumWidths = maximumWidths reservedLeft = (reserve / 2).rounded(.down) reservedRight = reserve - reservedLeft visible = ScreenRect( x: screen.x + reservedLeft, y: screen.y, width: screen.width - reserve, height: screen.height) } } enum CoverageOracle { private static func columns(_ frames: [ScreenRect], visible: ScreenRect, gaps: LayoutGaps) -> Bool { guard FrameOracle.stripeShape(frames, visible: visible, vertical: true, inner: gaps.inner, outer: gaps.outer), frames.allSatisfy({ visible.inset(gaps.outer).contains($0, tolerance: 0) }) else { return false } let ordered = frames.sorted { $0.x < $1.x } return zip(ordered, ordered.dropFirst()).allSatisfy { abs($1.x - $0.maxX - gaps.inner) <= 1 } } static func repairedGap(_ frames: [ScreenRect], visible: ScreenRect, gaps: LayoutGaps) -> Bool { guard frames.count == 2, frames.allSatisfy(\.isValid) else { return false } let expected = ConfigReloadGeometry(outerGap: gaps.outer, reservedTop: 0, innerGap: gaps.inner) .columns(count: 2, visible: visible) return zip(frames.sorted { $0.x < $1.x }, expected).allSatisfy { $0.matches($1, tolerance: 1) } && columns(frames, visible: visible, gaps: gaps) } static func groups(_ frames: [ScreenRect]) -> [[ScreenRect]] { var result: [[ScreenRect]] = [] for frame in frames { if let index = result.firstIndex(where: { $0.allSatisfy { $0.matches(frame, tolerance: 1) } }) { result[index].append(frame) } else { result.append([frame]) } } return result } static func stacked( _ frames: [ScreenRect], counts: [Int], visible: ScreenRect, gaps: LayoutGaps = RealityGeometry.defaultGaps ) -> Bool { guard frames.count == counts.reduce(0, +), counts.allSatisfy({ $0 > 0 }), frames.allSatisfy({ $0.isValid && visible.inset(gaps.outer).contains($0, tolerance: 0) }) else { return false } let grouped = groups(frames) return grouped.map(\.count).sorted() == counts.sorted() && columns(grouped.compactMap(\.first), visible: visible, gaps: gaps) } static func stackMembers(_ frames: [ScreenRect]) -> [Int] { frames.indices.filter { index in frames[index].isValid && frames.indices.contains { other in other != index && frames[other].isValid && abs(frames[other].x - frames[index].x) <= 1 && abs(frames[other].y - frames[index].y) <= 1 && abs(frames[other].height - frames[index].height) <= 1 } } } static func minimumStack(_ frames: [ScreenRect], geometry: MinimumStackGeometry) -> Bool { guard frames.count == geometry.minimumWidths.count, zip(frames, geometry.minimumWidths).allSatisfy({ $0.width >= $1 - 1 }) else { return false } let area = geometry.visible.inset(geometry.outerGap) guard frames.allSatisfy({ $0.isValid && area.contains($0, tolerance: 0) }) else { return false } var groups: [[Int]] = [] for index in frames.indices.sorted(by: { frames[$0].x < frames[$1].x }) { if let group = groups.firstIndex(where: { abs(frames[$0[0]].x - frames[index].x) <= 1 }) { groups[group].append(index) } else { groups.append([index]) } } guard groups.count == 2, groups.map(\.count).sorted() == [1, 2] else { return false } let content = area.width - geometry.innerGap let firstMinimum = groups[0].map { geometry.minimumWidths[$0] }.max() ?? 0 let secondMinimum = groups[1].map { geometry.minimumWidths[$0] }.max() ?? 0 let firstWidth = frames[groups[1][0]].x - area.x - geometry.innerGap guard firstWidth > 0, content - firstWidth > 0, firstWidth >= firstMinimum - 1, content - firstWidth >= secondMinimum - 1 else { return false } let slots = [ ScreenRect(x: area.x, y: area.y, width: firstWidth, height: area.height), ScreenRect( x: area.x + firstWidth + geometry.innerGap, y: area.y, width: content - firstWidth, height: area.height), ] for (group, slot) in zip(groups, slots) { for index in group { let maximum = geometry.maximumWidths?[index] ?? nil let expected = ScreenRect( x: slot.x, y: slot.y, width: min(slot.width, maximum ?? slot.width), height: slot.height) guard frames[index].matches(expected, tolerance: 1) else { return false } } } return true } static func unchanged(_ frames: [ScreenRect], baseline: [ScreenRect]) -> Bool { !baseline.isEmpty && frames.count == baseline.count && frames.allSatisfy(\.isValid) && baseline.allSatisfy(\.isValid) && zip(frames, baseline).allSatisfy { $0.matches($1, tolerance: 1) } } static func centeredFloat( _ frame: ScreenRect, tiles: [ScreenRect], visible: ScreenRect, gaps: LayoutGaps = RealityGeometry.defaultGaps ) -> Bool { let area = visible.inset(gaps.outer) return frame.isValid && area.contains(frame, tolerance: 0) && abs((frame.x + frame.maxX) / 2 - (area.x + area.maxX) / 2) <= 1 && abs((frame.y + frame.maxY) / 2 - (area.y + area.maxY) / 2) <= 1 && !tiles.contains { $0.matches(frame) } && columns(tiles, visible: visible, gaps: gaps) } static func edgeResized( _ frames: [ScreenRect], baseline: [ScreenRect], visible: ScreenRect, delta: Double, gaps: LayoutGaps = RealityGeometry.defaultGaps ) -> Bool { guard frames.count == 2, baseline.count == 2, delta.isFinite, abs(delta) > 10, baseline[0].x < baseline[1].x, columns(baseline, visible: visible, gaps: gaps), columns(frames, visible: visible, gaps: gaps) else { return false } let growth = frames[0].width - baseline[0].width let shrink = baseline[1].width - frames[1].width return abs(growth - delta) <= 3 && abs(shrink - delta) <= 3 && abs(frames[0].x - baseline[0].x) <= 1 && abs(frames[1].maxX - baseline[1].maxX) <= 1 } } @MainActor func requireCoverageFramesStay( _ session: RealitySession, owned: [OwnedWindow], baseline: [ScreenRect], evidence: String ) async throws { var samples: [Observation] = [] let start = ContinuousClock.now do { repeat { let rows = try FrameOracle.read() let elapsed = start.duration(to: .now).components samples.append( Observation( elapsedSeconds: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18, windows: rows)) guard CoverageOracle.unchanged(try FrameOracle.frames(rows, ids: owned.map(\.id)), baseline: baseline) else { throw RealityError("\(evidence): an owned real frame changed during the stability interval") } try session.requireProtectedFrames() try await Task.sleep(for: .milliseconds(40)) } while start.duration(to: .now) < .seconds(1) } catch { try encodeJSON(samples, to: session.directory.appendingPathComponent("\(evidence).json")) throw error } try encodeJSON(samples, to: session.directory.appendingPathComponent("\(evidence).json")) try await session.expect("\(evidence): all owned CG frames stayed unchanged for one second") { CoverageOracle.unchanged(try FrameOracle.frames($0, ids: owned.map(\.id)), baseline: baseline) } }