public enum MinimumSizeLearningError: Error, Equatable, Sendable { case invalidBundleID(String) case invalidSize(bundleID: String) case unsupportedVersion(bundleID: String, version: Int) } public struct MinimumSizeLearning: Equatable, Sendable { public private(set) var learned: [String: LearnedMinimumSize] public private(set) var pending: [String: Size] public init() { learned = [:] pending = [:] } public init(learned: [String: LearnedMinimumSize]) throws { for (bundleID, minimum) in learned { try Self.validate(bundleID: bundleID, minimum: minimum) } self.learned = learned pending = [:] } public static func combinedMinimum( _ first: LearnedMinimumSize?, _ second: LearnedMinimumSize? ) -> LearnedMinimumSize? { guard let first else { return second } guard let second else { return first } precondition(first.schemaVersion == second.schemaVersion, "Minimum size versions must agree") return LearnedMinimumSize( size: Size( width: max(first.size.width, second.size.width), height: max(first.size.height, second.size.height)), schemaVersion: first.schemaVersion) } public static func refusedMinimum( requested: Rect, accepted: Rect, knownMinimum: Size = Size() ) -> Size? { precondition(requested.isValid && accepted.isValid && knownMinimum.isValid, "Minimum read-backs must be valid") let width = accepted.size.width > requested.size.width let height = accepted.size.height > requested.size.height guard width || height else { return nil } // Position refusal is independent of size refusal; only constrained dimensions are learned. let candidate = Size( width: width ? max(knownMinimum.width, accepted.size.width) : knownMinimum.width, height: height ? max(knownMinimum.height, accepted.size.height) : knownMinimum.height) return candidate == knownMinimum ? nil : candidate } public static func confirmationFrame( requested: Rect, accepted: Rect, knownMinimum: Size = Size() ) -> Rect { precondition(requested.isValid && accepted.isValid && knownMinimum.isValid, "Minimum probes must be valid") func extent(_ requested: Double, _ accepted: Double, _ minimum: Double) -> Double { guard accepted > requested else { return requested } // Cross the observed rounding excess before testing whether the app really hit a lower bound. let floor = max(2, minimum.rounded(.up), requested / 2, requested - 64) return min(requested, max(floor, requested - (accepted - requested) - 8)) } return Rect( origin: requested.origin, size: Size( width: extent(requested.size.width, accepted.size.width, knownMinimum.width), height: extent(requested.size.height, accepted.size.height, knownMinimum.height)) ) .roundedToWholePoints() } /// A mismatch starts a new pair; invalid observations interrupt the pair. @discardableResult public mutating func observe(_ size: Size, for bundleID: String) throws -> LearnedMinimumSize? { guard Self.validBundleID(bundleID) else { throw MinimumSizeLearningError.invalidBundleID(bundleID) } guard Self.validSize(size) else { pending[bundleID] = nil throw MinimumSizeLearningError.invalidSize(bundleID: bundleID) } guard pending[bundleID] == size else { pending[bundleID] = size return nil } let minimum = LearnedMinimumSize(size: size) learned[bundleID] = minimum pending[bundleID] = nil return minimum } /// Records a value that was already confirmed by two independent resize read-backs. public mutating func recordConfirmed(_ minimum: LearnedMinimumSize, for bundleID: String) throws { try Self.validate(bundleID: bundleID, minimum: minimum) learned[bundleID] = minimum pending[bundleID] = nil } public mutating func reset(for bundleID: String) { learned[bundleID] = nil pending[bundleID] = nil } public mutating func resetAll() { learned.removeAll() pending.removeAll() } private static func validBundleID(_ bundleID: String) -> Bool { !bundleID.isEmpty && !bundleID.contains(where: \.isWhitespace) } private static func validSize(_ size: Size) -> Bool { size.width.isFinite && size.height.isFinite && size.width >= 0 && size.height >= 0 && (size.width > 0 || size.height > 0) } private static func validate(bundleID: String, minimum: LearnedMinimumSize) throws { guard validBundleID(bundleID) else { throw MinimumSizeLearningError.invalidBundleID(bundleID) } guard minimum.schemaVersion == LearnedMinimumSize.currentVersion else { throw MinimumSizeLearningError.unsupportedVersion(bundleID: bundleID, version: minimum.schemaVersion) } guard validSize(minimum.size) else { throw MinimumSizeLearningError.invalidSize(bundleID: bundleID) } } }