diff --git a/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift b/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift index 361bfcbf7..dc1955669 100644 --- a/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift +++ b/Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift @@ -308,8 +308,7 @@ extension CGRect { s.size.width -= insets.horizontal s.size.height -= insets.vertical - guard s.size.width >= 0, - s.size.height >= 0 + guard s.width >= 0, s.height >= 0 else { return .null } return s } @@ -326,8 +325,7 @@ extension CGRect { s.size.width += insets.horizontal s.size.height += insets.vertical - guard s.size.width >= 0, - s.size.height >= 0 + guard s.width >= 0, s.height >= 0 else { return .null } return s } @@ -340,15 +338,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..e8284aa8b 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(0, $0 - insets.horizontal) }, + height: height.map { max(0, $0 - insets.vertical) } ) } diff --git a/Sources/OpenSwiftUICore/Layout/View/ViewSize.swift b/Sources/OpenSwiftUICore/Layout/View/ViewSize.swift index ef50334dc..392bdf458 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.horizontal) + let newHeight = max(0, value.height - insets.vertical) 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