|
1 | 1 | import SwiftUI
|
2 | 2 |
|
3 |
| -// based on https://swiftui.diegolavalle.com/posts/linewrapping-stacks/ |
| 3 | +/// This View draws the WrappingHStack content taking into account the passed width, alignment and spacings. |
| 4 | +/// Note that the passed LineManager and ContentManager should be reused whenever possible. |
4 | 5 | struct InternalWrappingHStack: View {
|
5 |
| - let width: CGFloat |
6 | 6 | let alignment: HorizontalAlignment
|
7 | 7 | let spacing: WrappingHStack.Spacing
|
8 |
| - let content: [WrappingHStack.ViewType] |
9 |
| - let firstItemOfEachLine: [Int] |
10 | 8 | let lineSpacing: CGFloat
|
| 9 | + let lineManager: LineManager |
| 10 | + let contentManager: ContentManager |
11 | 11 |
|
12 |
| - init(width: CGFloat, alignment: HorizontalAlignment, spacing: WrappingHStack.Spacing, lineSpacing: CGFloat, content: [WrappingHStack.ViewType]) { |
13 |
| - self.width = width |
| 12 | + init(width: CGFloat, alignment: HorizontalAlignment, spacing: WrappingHStack.Spacing, lineSpacing: CGFloat, lineManager: LineManager, contentManager: ContentManager) { |
14 | 13 | self.alignment = alignment
|
15 | 14 | self.spacing = spacing
|
16 | 15 | self.lineSpacing = lineSpacing
|
17 |
| - self.content = content |
| 16 | + self.contentManager = contentManager |
| 17 | + self.lineManager = lineManager |
18 | 18 |
|
19 |
| - firstItemOfEachLine = content |
20 |
| - .enumerated() |
21 |
| - .reduce((firstItemOfEachLine: [], currentLineWidth: width)) { (result, contentIterator) -> (firstItemOfEachLine: [Int], currentLineWidth: CGFloat) in |
22 |
| - var (firstItemOfEachLine, currentLineWidth) = result |
23 |
| - |
24 |
| - switch contentIterator.element { |
25 |
| - case .newLine: |
26 |
| - return (firstItemOfEachLine + [contentIterator.offset], width) |
27 |
| - case .any(let anyView) where Self.isVisible(view: anyView): |
28 |
| - let itemWidth = Self.getWidth(of: anyView) |
29 |
| - if result.currentLineWidth + itemWidth + spacing.minSpacing > width { |
30 |
| - currentLineWidth = itemWidth |
31 |
| - firstItemOfEachLine.append(contentIterator.offset) |
32 |
| - } else { |
33 |
| - currentLineWidth += itemWidth + spacing.minSpacing |
34 |
| - } |
35 |
| - return (firstItemOfEachLine, currentLineWidth) |
36 |
| - default: |
37 |
| - return result |
38 |
| - } |
39 |
| - }.0 |
40 |
| - } |
41 |
| - |
42 |
| - static func getWidth(of anyView: AnyView) -> Double { |
43 |
| -#if os(iOS) |
44 |
| - let hostingController = UIHostingController(rootView: HStack { anyView }) |
45 |
| -#else |
46 |
| - let hostingController = NSHostingController(rootView: HStack { anyView }) |
47 |
| -#endif |
48 |
| - return hostingController.sizeThatFits(in: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width |
49 |
| - } |
50 |
| - |
51 |
| - var totalLines: Int { |
52 |
| - firstItemOfEachLine.count |
53 |
| - } |
54 |
| - |
55 |
| - func startOf(line i: Int) -> Int { |
56 |
| - firstItemOfEachLine[i] |
57 |
| - } |
58 |
| - |
59 |
| - func endOf(line i: Int) -> Int { |
60 |
| - i == totalLines - 1 ? content.count - 1 : firstItemOfEachLine[i + 1] - 1 |
61 |
| - } |
62 |
| - |
63 |
| - func hasExactlyOneElement(line i: Int) -> Bool { |
64 |
| - startOf(line: i) == endOf(line: i) |
| 19 | + if !lineManager.isSetUp { |
| 20 | + lineManager.setup(contentManager: contentManager, width: width, spacing: spacing) |
| 21 | + } |
65 | 22 | }
|
66 | 23 |
|
67 | 24 | func shouldHaveSideSpacers(line i: Int) -> Bool {
|
68 | 25 | if case .constant = spacing {
|
69 | 26 | return true
|
70 | 27 | }
|
71 |
| - if case .dynamic = spacing, hasExactlyOneElement(line: i) { |
| 28 | + if case .dynamic = spacing, lineManager.hasExactlyOneElement(line: i) { |
72 | 29 | return true
|
73 | 30 | }
|
74 | 31 | return false
|
75 | 32 | }
|
76 |
| - |
77 |
| - @inline(__always) static func isVisible(view: AnyView) -> Bool { |
78 |
| -#if os(iOS) |
79 |
| - return UIHostingController(rootView: view).sizeThatFits(in: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width > 0 |
80 |
| -#else |
81 |
| - return NSHostingController(rootView: view).sizeThatFits(in: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width > 0 |
82 |
| -#endif |
83 |
| - } |
84 | 33 |
|
85 | 34 | var body: some View {
|
86 | 35 | VStack(alignment: alignment, spacing: lineSpacing) {
|
87 |
| - ForEach(0 ..< totalLines, id: \.self) { lineIndex in |
| 36 | + ForEach(0 ..< lineManager.totalLines, id: \.self) { lineIndex in |
88 | 37 | HStack(spacing: 0) {
|
89 | 38 | if alignment == .center || alignment == .trailing, shouldHaveSideSpacers(line: lineIndex) {
|
90 | 39 | Spacer(minLength: 0)
|
91 | 40 | }
|
92 | 41 |
|
93 |
| - ForEach(startOf(line: lineIndex) ... endOf(line: lineIndex), id: \.self) { |
| 42 | + ForEach(lineManager.startOf(line: lineIndex) ... lineManager.endOf(line: lineIndex), id: \.self) { |
94 | 43 | if case .dynamicIncludingBorders = spacing,
|
95 |
| - startOf(line: lineIndex) == $0 |
| 44 | + lineManager.startOf(line: lineIndex) == $0 |
96 | 45 | {
|
97 | 46 | Spacer(minLength: spacing.minSpacing)
|
98 | 47 | }
|
99 | 48 |
|
100 |
| - if case .any(let anyView) = content[$0], Self.isVisible(view: anyView) { |
| 49 | + if case .any(let anyView) = contentManager.items[$0], contentManager.isVisible(viewIndex: $0) { |
101 | 50 | anyView
|
102 | 51 | }
|
103 | 52 |
|
104 |
| - if endOf(line: lineIndex) != $0 { |
105 |
| - if case .any(let anyView) = content[$0], !Self.isVisible(view: anyView) { } else { |
| 53 | + if lineManager.endOf(line: lineIndex) != $0 { |
| 54 | + if case .any = contentManager.items[$0], !contentManager.isVisible(viewIndex: $0) { } else { |
106 | 55 | if case .constant(let exactSpacing) = spacing {
|
107 | 56 | Spacer(minLength: 0)
|
108 | 57 | .frame(width: exactSpacing)
|
|
0 commit comments