Skip to content

Commit 3f6bd56

Browse files
committed
Inlining
1 parent aee5aa2 commit 3f6bd56

File tree

10 files changed

+120
-15
lines changed

10 files changed

+120
-15
lines changed

Sources/Flow/HFlow.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import SwiftUI
1919
/// }
2020
///
2121
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
22+
@frozen
2223
public struct HFlow<Content: View>: View {
23-
private let layout: HFlowLayout
24-
private let content: Content
24+
@usableFromInline
25+
let layout: HFlowLayout
26+
@usableFromInline
27+
let content: Content
2528

2629
/// Creates a horizontal flow with the given spacing and vertical alignment.
2730
///
@@ -38,6 +41,7 @@ public struct HFlow<Content: View>: View {
3841
/// mode tries to distribute items more evenly by minimizing the empty
3942
/// spaces left in each row, while respecting their order.
4043
/// - content: A view builder that creates the content of this flow.
44+
@inlinable
4145
public init(
4246
alignment: VerticalAlignment = .center,
4347
itemSpacing: CGFloat? = nil,
@@ -69,6 +73,7 @@ public struct HFlow<Content: View>: View {
6973
/// mode tries to distribute items more evenly by minimizing the empty
7074
/// spaces left in each row, while respecting their order.
7175
/// - content: A view builder that creates the content of this flow.
76+
@inlinable
7277
public init(
7378
alignment: VerticalAlignment = .center,
7479
spacing: CGFloat? = nil,
@@ -86,6 +91,7 @@ public struct HFlow<Content: View>: View {
8691
)
8792
}
8893

94+
@inlinable
8995
public var body: some View {
9096
layout {
9197
content
@@ -114,6 +120,7 @@ extension HFlow: Layout where Content == EmptyView {
114120
/// - distributeItemsEvenly: Instead of prioritizing the first rows, this
115121
/// mode tries to distribute items more evenly by minimizing the empty
116122
/// spaces left in each row, while respecting their order.
123+
@inlinable
117124
public init(
118125
alignment: VerticalAlignment = .center,
119126
itemSpacing: CGFloat? = nil,
@@ -144,6 +151,7 @@ extension HFlow: Layout where Content == EmptyView {
144151
/// - distributeItemsEvenly: Instead of prioritizing the first rows, this
145152
/// mode tries to distribute items more evenly by minimizing the empty
146153
/// spaces left in each row, while respecting their order.
154+
@inlinable
147155
public init(
148156
alignment: VerticalAlignment = .center,
149157
spacing: CGFloat? = nil,
@@ -160,15 +168,26 @@ extension HFlow: Layout where Content == EmptyView {
160168
}
161169
}
162170

163-
public func sizeThatFits(proposal: ProposedViewSize, subviews: LayoutSubviews, cache: inout FlowLayoutCache) -> CGSize {
171+
@inlinable
172+
public func sizeThatFits(
173+
proposal: ProposedViewSize,
174+
subviews: LayoutSubviews,
175+
cache: inout FlowLayoutCache
176+
) -> CGSize {
164177
layout.sizeThatFits(
165178
proposal: proposal,
166179
subviews: subviews,
167180
cache: &cache
168181
)
169182
}
170183

171-
public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: LayoutSubviews, cache: inout FlowLayoutCache) {
184+
@inlinable
185+
public func placeSubviews(
186+
in bounds: CGRect,
187+
proposal: ProposedViewSize,
188+
subviews: LayoutSubviews,
189+
cache: inout FlowLayoutCache
190+
) {
172191
layout.placeSubviews(
173192
in: bounds,
174193
proposal: proposal,
@@ -177,10 +196,12 @@ extension HFlow: Layout where Content == EmptyView {
177196
)
178197
}
179198

199+
@inlinable
180200
public func makeCache(subviews: LayoutSubviews) -> FlowLayoutCache {
181201
FlowLayoutCache(subviews, axis: .horizontal)
182202
}
183203

204+
@inlinable
184205
public static var layoutProperties: LayoutProperties {
185206
HFlowLayout.layoutProperties
186207
}

Sources/Flow/HFlowLayout.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import SwiftUI
22

33
/// A layout that arranges its children in a horizontally flowing manner.
44
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
5+
@frozen
56
public struct HFlowLayout {
6-
private let layout: FlowLayout
7+
@usableFromInline
8+
let layout: FlowLayout
79

810
/// Creates a horizontal flow with the given spacing and vertical alignment.
911
///
@@ -17,6 +19,7 @@ public struct HFlowLayout {
1719
/// - distributeItemsEvenly: Instead of prioritizing the first rows, this
1820
/// mode tries to distribute items more evenly by minimizing the empty
1921
/// spaces left in each row, while respecting their order.
22+
@inlinable
2023
public init(
2124
alignment: VerticalAlignment = .center,
2225
itemSpacing: CGFloat? = nil,
@@ -36,18 +39,22 @@ public struct HFlowLayout {
3639

3740
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
3841
extension HFlowLayout: Layout {
42+
@inlinable
3943
public func sizeThatFits(proposal: ProposedViewSize, subviews: LayoutSubviews, cache: inout FlowLayoutCache) -> CGSize {
4044
layout.sizeThatFits(proposal: proposal, subviews: subviews, cache: &cache)
4145
}
4246

47+
@inlinable
4348
public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: LayoutSubviews, cache: inout FlowLayoutCache) {
4449
layout.placeSubviews(in: bounds, proposal: proposal, subviews: subviews, cache: &cache)
4550
}
4651

52+
@inlinable
4753
public func makeCache(subviews: LayoutSubviews) -> FlowLayoutCache {
4854
FlowLayoutCache(subviews, axis: .horizontal)
4955
}
5056

57+
@inlinable
5158
public static var layoutProperties: LayoutProperties {
5259
var properties = LayoutProperties()
5360
properties.stackOrientation = .horizontal

Sources/Flow/Internal/Layout.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import CoreFoundation
22
import SwiftUI
33

44
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
5+
@usableFromInline
56
struct FlowLayout {
67
let axis: Axis
78
var itemSpacing: CGFloat?
@@ -28,6 +29,7 @@ struct FlowLayout {
2829
private typealias Line = [ItemWithSpacing<Item>]
2930
private typealias Lines = [ItemWithSpacing<Line>]
3031

32+
@usableFromInline
3133
func sizeThatFits(
3234
proposal proposedSize: ProposedViewSize,
3335
subviews: some Subviews,
@@ -44,6 +46,7 @@ struct FlowLayout {
4446
return CGSize(size: size, axis: axis)
4547
}
4648

49+
@usableFromInline
4750
func placeSubviews(
4851
in bounds: CGRect,
4952
proposal: ProposedViewSize,
@@ -257,10 +260,12 @@ struct FlowLayout {
257260

258261
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
259262
extension FlowLayout: Layout {
263+
@usableFromInline
260264
func makeCache(subviews: LayoutSubviews) -> FlowLayoutCache {
261265
makeCache(subviews)
262266
}
263267

268+
@usableFromInline
264269
static func vertical(
265270
alignment: HorizontalAlignment,
266271
itemSpacing: CGFloat?,
@@ -279,6 +284,7 @@ extension FlowLayout: Layout {
279284
}
280285
}
281286

287+
@usableFromInline
282288
static func horizontal(
283289
alignment: VerticalAlignment,
284290
itemSpacing: CGFloat?,
@@ -298,7 +304,8 @@ extension FlowLayout: Layout {
298304
}
299305
}
300306

301-
private extension Array where Element == Size {
307+
extension Array where Element == Size {
308+
@inlinable
302309
func reduce(
303310
_ initial: Size,
304311
breadth: (CGFloat, CGFloat) -> CGFloat,

Sources/Flow/Internal/LineBreaking.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CoreFoundation
22

3+
@inlinable
34
func knuthPlassLineBreakingAlgorithm(
45
proposedBreadth: CGFloat,
56
sizes: [Size],
@@ -21,7 +22,8 @@ func knuthPlassLineBreakingAlgorithm(
2122
return breakpoints
2223
}
2324

24-
private func calculateOptimalBreaks(
25+
@usableFromInline
26+
func calculateOptimalBreaks(
2527
proposedBreadth: CGFloat,
2628
sizes: [Size],
2729
spacings: [CGFloat]

Sources/Flow/Internal/Protocols.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import SwiftUI
22

33
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
4+
@usableFromInline
45
protocol Subviews: RandomAccessCollection where Element: Subview, Index == Int {}
56

67
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
78
extension LayoutSubviews: Subviews {}
89

910
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
11+
@usableFromInline
1012
protocol Subview {
1113
var spacing: ViewSpacing { get }
1214
var priority: Double { get }
@@ -17,11 +19,13 @@ protocol Subview {
1719

1820
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
1921
extension LayoutSubview: Subview {
22+
@usableFromInline
2023
func dimensions(_ proposal: ProposedViewSize) -> Dimensions {
2124
dimensions(in: proposal)
2225
}
2326
}
2427

28+
@usableFromInline
2529
protocol Dimensions {
2630
var width: CGFloat { get }
2731
var height: CGFloat { get }
@@ -32,13 +36,15 @@ protocol Dimensions {
3236
extension ViewDimensions: Dimensions {}
3337

3438
extension Dimensions {
39+
@usableFromInline
3540
func size(on axis: Axis) -> Size {
3641
Size(
3742
breadth: value(on: axis),
3843
depth: value(on: axis.perpendicular)
3944
)
4045
}
4146

47+
@usableFromInline
4248
func value(on axis: Axis) -> CGFloat {
4349
switch axis {
4450
case .horizontal: width

Sources/Flow/Internal/Size.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import CoreFoundation
22
import SwiftUI
33

4+
@usableFromInline
45
struct Size {
6+
@usableFromInline
57
var breadth: CGFloat
8+
@usableFromInline
69
var depth: CGFloat
710

11+
@usableFromInline
12+
init(breadth: CGFloat, depth: CGFloat) {
13+
self.breadth = breadth
14+
self.depth = depth
15+
}
16+
17+
@usableFromInline
818
static let zero = Size(breadth: 0, depth: 0)
919

20+
@usableFromInline
1021
func adding(_ value: CGFloat, on axis: Axis) -> Size {
1122
var size = self
1223
size[axis] += value
1324
return size
1425
}
1526

16-
fileprivate subscript(axis: Axis) -> CGFloat {
27+
@usableFromInline
28+
subscript(axis: Axis) -> CGFloat {
1729
get {
1830
self[keyPath: keyPath(on: axis)]
1931
}
@@ -31,6 +43,7 @@ struct Size {
3143
}
3244

3345
extension Axis {
46+
@usableFromInline
3447
var perpendicular: Axis {
3548
switch self {
3649
case .horizontal: .vertical
@@ -47,16 +60,19 @@ protocol FixedOrientation2DCoordinate {
4760
}
4861

4962
extension FixedOrientation2DCoordinate {
63+
@inlinable
5064
func size(on axis: Axis) -> Size {
5165
Size(breadth: value(on: axis), depth: value(on: axis.perpendicular))
5266
}
5367
}
5468

5569
extension CGPoint: FixedOrientation2DCoordinate {
70+
@inlinable
5671
init(size: Size, axis: Axis) {
5772
self.init(x: size[axis], y: size[axis.perpendicular])
5873
}
5974

75+
@inlinable
6076
func value(on axis: Axis) -> CGFloat {
6177
switch axis {
6278
case .horizontal: x
@@ -66,10 +82,12 @@ extension CGPoint: FixedOrientation2DCoordinate {
6682
}
6783

6884
extension CGSize: FixedOrientation2DCoordinate {
85+
@inlinable
6986
init(size: Size, axis: Axis) {
7087
self.init(width: size[axis], height: size[axis.perpendicular])
7188
}
7289

90+
@inlinable
7391
func value(on axis: Axis) -> CGFloat {
7492
switch axis {
7593
case .horizontal: width
@@ -80,10 +98,12 @@ extension CGSize: FixedOrientation2DCoordinate {
8098

8199
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
82100
extension ProposedViewSize: FixedOrientation2DCoordinate {
101+
@inlinable
83102
init(size: Size, axis: Axis) {
84103
self.init(width: size[axis], height: size[axis.perpendicular])
85104
}
86105

106+
@inlinable
87107
func value(on axis: Axis) -> CGFloat {
88108
switch axis {
89109
case .horizontal: width ?? .infinity

Sources/Flow/Internal/Utils.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
extension Sequence {
2+
@inlinable
23
func adjacentPairs() -> some Sequence<(Element, Element)> {
34
zip(self, self.dropFirst())
45
}
56

7+
@inlinable
68
func sum<Result: Numeric>(of block: (Element) -> Result) -> Result {
79
reduce(into: .zero) { $0 += block($1) }
810
}

Sources/Flow/Support.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public enum Justification {
3030
/// it's considered an internal implementation detail.
3131
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
3232
public struct FlowLayoutCache {
33+
@usableFromInline
3334
struct SubviewCache {
3435
var priority: Double
3536
var spacing: ViewSpacing
3637
var min: Size
3738
var ideal: Size
3839
var max: Size
3940

41+
@usableFromInline
4042
init(_ subview: some Subview, axis: Axis) {
4143
priority = subview.priority
4244
spacing = subview.spacing
@@ -46,8 +48,10 @@ public struct FlowLayoutCache {
4648
}
4749
}
4850

51+
@usableFromInline
4952
let subviewsCache: [SubviewCache]
5053

54+
@inlinable
5155
init(_ subviews: some Subviews, axis: Axis) {
5256
subviewsCache = subviews.map {
5357
SubviewCache($0, axis: axis)

0 commit comments

Comments
 (0)