|
| 1 | +// |
| 2 | +// WrappingHStack.swift |
| 3 | +// |
| 4 | +// CotEditor |
| 5 | +// https://coteditor.com |
| 6 | +// |
| 7 | +// Created by 1024jp on 2024-02-10. |
| 8 | +// |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +// |
| 11 | +// © 2024 1024jp |
| 12 | +// |
| 13 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | +// you may not use this file except in compliance with the License. |
| 15 | +// You may obtain a copy of the License at |
| 16 | +// |
| 17 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | +// |
| 19 | +// Unless required by applicable law or agreed to in writing, software |
| 20 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | +// See the License for the specific language governing permissions and |
| 23 | +// limitations under the License. |
| 24 | +// |
| 25 | + |
| 26 | +import SwiftUI |
| 27 | + |
| 28 | +struct WrappingHStack<Content: View>: View { |
| 29 | + |
| 30 | + var horizontalSpacing: Double = 4 |
| 31 | + var verticalSpacing: Double = 4 |
| 32 | + var content: () -> Content |
| 33 | + |
| 34 | + |
| 35 | + var body: some View { |
| 36 | + |
| 37 | + WrappingHStackLayout(horizontalSpacing: self.horizontalSpacing, verticalSpacing: self.verticalSpacing) { |
| 38 | + self.content() |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +private struct WrappingHStackLayout: Layout { |
| 45 | + |
| 46 | + let horizontalSpacing: Double |
| 47 | + let verticalSpacing: Double |
| 48 | + |
| 49 | + |
| 50 | + func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { |
| 51 | + |
| 52 | + let width = proposal.replacingUnspecifiedDimensions().width |
| 53 | + let rowCount = Double(self.countRows(for: subviews, in: width)) |
| 54 | + let minHeight = subviews |
| 55 | + .map { $0.sizeThatFits(proposal).height } |
| 56 | + .reduce(0) { max($0, $1).rounded(.up) } |
| 57 | + let height = rowCount * minHeight + max(rowCount - 1, 0) * self.verticalSpacing |
| 58 | + |
| 59 | + return CGSize(width: width, height: height) |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { |
| 64 | + |
| 65 | + let minHeight = subviews |
| 66 | + .map { $0.sizeThatFits(proposal).height } |
| 67 | + .reduce(0) { max($0, $1).rounded(.up) } |
| 68 | + |
| 69 | + var point = bounds.origin |
| 70 | + for subview in subviews { |
| 71 | + let width = subview.sizeThatFits(proposal).width |
| 72 | + |
| 73 | + if point.x + width > bounds.maxX { |
| 74 | + point.x = bounds.minX |
| 75 | + point.y += minHeight + self.verticalSpacing |
| 76 | + } |
| 77 | + |
| 78 | + subview.place(at: point, anchor: .topLeading, proposal: proposal) |
| 79 | + point.x += width + self.horizontalSpacing |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + /// Calculates the number of rows when the subviews are laid out within the given `containerWidth`. |
| 85 | + /// |
| 86 | + /// - Parameters: |
| 87 | + /// - subviews: The subviews to lay out. |
| 88 | + /// - containerWidth: The width of the view to fit in. |
| 89 | + /// - Returns: The number of rows. |
| 90 | + private func countRows(for subviews: Subviews, in containerWidth: Double) -> Int { |
| 91 | + |
| 92 | + var count = 0 |
| 93 | + |
| 94 | + var x: Double = 0 |
| 95 | + for subview in subviews { |
| 96 | + let width = subview.sizeThatFits(.unspecified).width |
| 97 | + |
| 98 | + if x + width > containerWidth { |
| 99 | + x = 0 |
| 100 | + count += 1 |
| 101 | + } |
| 102 | + x += width + self.horizontalSpacing |
| 103 | + } |
| 104 | + |
| 105 | + if x > 0 { |
| 106 | + count += 1 |
| 107 | + } |
| 108 | + |
| 109 | + return count |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +// MARK: - Preview |
| 116 | + |
| 117 | +#Preview { |
| 118 | + let words = ["Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "sed", "do"] |
| 119 | + |
| 120 | + return WrappingHStack { |
| 121 | + ForEach(words, id: \.self) { |
| 122 | + Text($0) |
| 123 | + .monospacedDigit() |
| 124 | + .padding(.horizontal, 2) |
| 125 | + .overlay(RoundedRectangle(cornerRadius: 3).fill(Color.accentColor.opacity(0.2))) |
| 126 | + } |
| 127 | + } |
| 128 | + .border(.separator) |
| 129 | + .padding() |
| 130 | + .frame(width: 180) |
| 131 | +} |
0 commit comments