diff --git a/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift b/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift index 361bfcbf7..be8a646d7 100644 --- a/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift +++ b/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift @@ -308,10 +308,11 @@ extension CGRect { s.size.width -= insets.horizontal s.size.height -= insets.vertical - guard s.size.width >= 0, - s.size.height >= 0 - else { return .null } - return s + if s.size.width < 0 || s.size.height < 0 { + return .null + } else { + return s + } } package func inset(by insets: EdgeInsets) -> CGRect { @@ -340,15 +341,15 @@ extension CGRect { extension CGSize { package func inset(by insets: EdgeInsets) -> CGSize { CGSize( - width: max(width - insets.horizontal, 0), - height: max(height - insets.vertical, 0) + width: max(0, width - insets.horizontal), + height: max(0, height - insets.vertical) ) } package func outset(by insets: EdgeInsets) -> CGSize { CGSize( - width: max(width + insets.horizontal, 0), - height: max(height + insets.vertical, 0) + width: max(0, width + insets.horizontal), + height: max(0, height + insets.vertical) ) } } diff --git a/Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift b/Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift index 3d3c9ecf5..f671e5e8f 100644 --- a/Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift +++ b/Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift @@ -102,8 +102,8 @@ extension _ProposedSize { /// - Returns: A new proposed size with insets applied. package func inset(by insets: EdgeInsets) -> _ProposedSize { _ProposedSize( - width: width.map { max($0 - insets.leading - insets.trailing, .zero) }, - height: height.map { max($0 - insets.top - insets.bottom, .zero) } + width: width.map { max(.zero, $0 - insets.leading - insets.trailing) }, + height: height.map { max(.zero, $0 - insets.top - insets.bottom) } ) } diff --git a/Sources/OpenSwiftUICore/Layout/View/ViewSize.swift b/Sources/OpenSwiftUICore/Layout/View/ViewSize.swift index ef50334dc..2971b64e1 100644 --- a/Sources/OpenSwiftUICore/Layout/View/ViewSize.swift +++ b/Sources/OpenSwiftUICore/Layout/View/ViewSize.swift @@ -124,8 +124,8 @@ extension ViewSize { /// - Parameter insets: The edge insets to apply. /// - Returns: A new view size that has been inset by the specified amounts. package func inset(by insets: EdgeInsets) -> ViewSize { - let newWidth = max(value.width - (insets.leading + insets.trailing), 0) - let newHeight = max(value.height - (insets.top + insets.bottom), 0) + let newWidth = max(0, value.width - (insets.leading + insets.trailing)) + let newHeight = max(0, value.height - (insets.top + insets.bottom)) return ViewSize( value: CGSize(width: newWidth, height: newHeight), proposal: CGSize( diff --git a/Tests/OpenSwiftUICoreTests/Layout/Geometry/ProposedSizeTests.swift b/Tests/OpenSwiftUICoreTests/Layout/Geometry/ProposedSizeTests.swift index 6f6575626..ec00330a1 100644 --- a/Tests/OpenSwiftUICoreTests/Layout/Geometry/ProposedSizeTests.swift +++ b/Tests/OpenSwiftUICoreTests/Layout/Geometry/ProposedSizeTests.swift @@ -114,6 +114,12 @@ struct ProposedSizeTests { let partialInset = partialSize.inset(by: insets) #expect(partialInset.width == nil) #expect(partialInset.height == 180) + + // Test with nan dimensions + let nanSize = _ProposedSize(width: .nan, height: 200) + let nanInset = nanSize.inset(by: insets) + #expect(nanInset.width == 0) + #expect(nanInset.height == 180) } // MARK: - Axis Access Tests