Skip to content

Commit 17d2904

Browse files
committed
Fix NaN handling in inset(by:)
1 parent c6e6fbd commit 17d2904

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,11 @@ extension CGRect {
308308
s.size.width -= insets.horizontal
309309
s.size.height -= insets.vertical
310310

311-
guard s.size.width >= 0,
312-
s.size.height >= 0
313-
else { return .null }
314-
return s
311+
if s.size.width < 0 || s.size.height < 0 {
312+
return .null
313+
} else {
314+
return s
315+
}
315316
}
316317

317318
package func inset(by insets: EdgeInsets) -> CGRect {
@@ -340,15 +341,15 @@ extension CGRect {
340341
extension CGSize {
341342
package func inset(by insets: EdgeInsets) -> CGSize {
342343
CGSize(
343-
width: max(width - insets.horizontal, 0),
344-
height: max(height - insets.vertical, 0)
344+
width: max(0, width - insets.horizontal),
345+
height: max(0, height - insets.vertical)
345346
)
346347
}
347348

348349
package func outset(by insets: EdgeInsets) -> CGSize {
349350
CGSize(
350-
width: max(width + insets.horizontal, 0),
351-
height: max(height + insets.vertical, 0)
351+
width: max(0, width + insets.horizontal),
352+
height: max(0, height + insets.vertical)
352353
)
353354
}
354355
}

Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ extension _ProposedSize {
102102
/// - Returns: A new proposed size with insets applied.
103103
package func inset(by insets: EdgeInsets) -> _ProposedSize {
104104
_ProposedSize(
105-
width: width.map { max($0 - insets.leading - insets.trailing, .zero) },
106-
height: height.map { max($0 - insets.top - insets.bottom, .zero) }
105+
width: width.map { max(.zero, $0 - insets.leading - insets.trailing) },
106+
height: height.map { max(.zero, $0 - insets.top - insets.bottom) }
107107
)
108108
}
109109

Sources/OpenSwiftUICore/Layout/View/ViewSize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ extension ViewSize {
124124
/// - Parameter insets: The edge insets to apply.
125125
/// - Returns: A new view size that has been inset by the specified amounts.
126126
package func inset(by insets: EdgeInsets) -> ViewSize {
127-
let newWidth = max(value.width - (insets.leading + insets.trailing), 0)
128-
let newHeight = max(value.height - (insets.top + insets.bottom), 0)
127+
let newWidth = max(0, value.width - (insets.leading + insets.trailing))
128+
let newHeight = max(0, value.height - (insets.top + insets.bottom))
129129
return ViewSize(
130130
value: CGSize(width: newWidth, height: newHeight),
131131
proposal: CGSize(

Tests/OpenSwiftUICoreTests/Layout/Geometry/ProposedSizeTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ struct ProposedSizeTests {
114114
let partialInset = partialSize.inset(by: insets)
115115
#expect(partialInset.width == nil)
116116
#expect(partialInset.height == 180)
117+
118+
// Test with nan dimensions
119+
let nanSize = _ProposedSize(width: .nan, height: 200)
120+
let nanInset = nanSize.inset(by: insets)
121+
#expect(nanInset.width == 0)
122+
#expect(nanInset.height == 180)
117123
}
118124

119125
// MARK: - Axis Access Tests

0 commit comments

Comments
 (0)