Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUICore/Layout/View/ViewSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading