diff --git a/Paralayout/GeometryAdditions.swift b/Paralayout/GeometryAdditions.swift index 9d0e3ac..39d1f1f 100644 --- a/Paralayout/GeometryAdditions.swift +++ b/Paralayout/GeometryAdditions.swift @@ -14,6 +14,7 @@ // limitations under the License. // +import os import UIKit // MARK: - Operator Overloads @@ -144,6 +145,8 @@ extension CGRect { /// Divides the receiver in two. /// + /// The `slice` will always have a length of the specified `amount` in the perpendicular axis to the `edge`. The `remainder` will have the length remaining along that axis, clamped to zero if the `amount` is longer than the receiver's length. + /// /// - parameter from: The edge from which the amount is interpreted. /// - parameter amount: The size of the slice (absolute). /// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of @@ -152,34 +155,42 @@ extension CGRect { switch edge { case .minXEdge: // Left. - assert(amount <= width, "Cannot slice rect \(self) at edge \(edge) by \(amount)!") + if amount > width { + ParalayoutAlertForInvalidSliceDimensions() + } return ( CGRect(x: minX, y: minY, width: amount, height: height), - CGRect(x: minX + amount, y: minY, width: width - amount, height: height) + CGRect(x: minX + amount, y: minY, width: max(width - amount, 0), height: height) ) case .minYEdge: // Top. - assert(amount <= height, "Cannot slice rect \(self) at edge \(edge) by \(amount)!") + if amount > height { + ParalayoutAlertForInvalidSliceDimensions() + } return( CGRect(x: minX, y: minY, width: width, height: amount), - CGRect(x: minX, y: minY + amount, width: width, height: height - amount) + CGRect(x: minX, y: minY + amount, width: width, height: max(height - amount, 0)) ) case .maxXEdge: // Right. - assert(amount <= width, "Cannot slice rect \(self) at edge \(edge) by \(amount)!") + if amount > width { + ParalayoutAlertForInvalidSliceDimensions() + } return( CGRect(x: maxX - amount, y: minY, width: amount, height: height), - CGRect(x: minX, y: minY, width: width - amount, height: height) + CGRect(x: minX, y: minY, width: max(width - amount, 0), height: height) ) case .maxYEdge: // Bottom. - assert(amount <= height, "Cannot slice rect \(self) at edge \(edge) by \(amount)!") + if amount > height { + ParalayoutAlertForInvalidSliceDimensions() + } return( CGRect(x: minX, y: maxY - amount, width: width, height: amount), - CGRect(x: minX, y: minY, width: width, height: height - amount) + CGRect(x: minX, y: minY, width: width, height: max(height - amount, 0)) ) } } @@ -282,3 +293,20 @@ extension NSDirectionalEdgeInsets { } } + +// MARK: - + +nonisolated +private func ParalayoutAlertForInvalidSliceDimensions() { + os_log( + "%@", + log: ParalayoutLog, + type: .default, + """ + Paralayout detected a slice with an `amount` larger than the receiver's length in the specified axis. \ + Set a symbolic breakpoint for \"ParalayoutAlertForInvalidSliceDimensions\" to debug. \ + Call stack: + \(Thread.callStackSymbols.dropFirst(1).joined(separator: "\n")) + """ + ) +} diff --git a/Paralayout/ParalayoutLog.swift b/Paralayout/ParalayoutLog.swift new file mode 100644 index 0000000..de629fb --- /dev/null +++ b/Paralayout/ParalayoutLog.swift @@ -0,0 +1,20 @@ +// +// Copyright © 2024 Block, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +//    http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import os + +nonisolated +internal let ParalayoutLog = OSLog(subsystem: "com.squareup.Paralayout", category: "layout") diff --git a/Paralayout/UIView+Alignment.swift b/Paralayout/UIView+Alignment.swift index a6e5564..0b8c7ed 100644 --- a/Paralayout/UIView+Alignment.swift +++ b/Paralayout/UIView+Alignment.swift @@ -170,8 +170,6 @@ extension AlignmentContext { // MARK: - -@MainActor -private let ParalayoutLog = OSLog(subsystem: "com.squareup.Paralayout", category: "layout") /// Triggered when an alignment method is called that uses mismatched position types, i.e. aligning a view's leading or /// trailing edge to another view's left or right edge, or vice versa. This type of mismatch is likely to look correct