Skip to content

Commit aaadc38

Browse files
authored
[NFC] Optimize EdgeInsets code (#710)
1 parent c047c6a commit aaadc38

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

Sources/OpenSwiftUICore/Layout/Edge/EdgeInsets.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ 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
311+
guard s.width >= 0, s.height >= 0
313312
else { return .null }
314313
return s
315314
}
@@ -326,8 +325,7 @@ extension CGRect {
326325
s.size.width += insets.horizontal
327326
s.size.height += insets.vertical
328327

329-
guard s.size.width >= 0,
330-
s.size.height >= 0
328+
guard s.width >= 0, s.height >= 0
331329
else { return .null }
332330
return s
333331
}
@@ -340,15 +338,15 @@ extension CGRect {
340338
extension CGSize {
341339
package func inset(by insets: EdgeInsets) -> CGSize {
342340
CGSize(
343-
width: max(width - insets.horizontal, 0),
344-
height: max(height - insets.vertical, 0)
341+
width: max(0, width - insets.horizontal),
342+
height: max(0, height - insets.vertical)
345343
)
346344
}
347345

348346
package func outset(by insets: EdgeInsets) -> CGSize {
349347
CGSize(
350-
width: max(width + insets.horizontal, 0),
351-
height: max(height + insets.vertical, 0)
348+
width: max(0, width + insets.horizontal),
349+
height: max(0, height + insets.vertical)
352350
)
353351
}
354352
}

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(0, $0 - insets.horizontal) },
106+
height: height.map { max(0, $0 - insets.vertical) }
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.horizontal)
128+
let newHeight = max(0, value.height - insets.vertical)
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)