Skip to content

Commit f92a3b4

Browse files
Upload a draft / Create ./ChipLayout.swift
1 parent 83934ae commit f92a3b4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

ChipLayout.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Read Me
2+
-> History Entry | iPadOS 17.0 | Swift Playgrounds | _ | ChipLayout.swift | Initially Modified: 03:29 PM Fri 08 Sep 2023
3+
-> History Entry | iPadOS 17.0 | Swift Playgrounds | _ | ChipLayout.swift | Last Modified: 06:08 PM Fri 08 Sep 2023
4+
*/
5+
6+
import SwiftUI
7+
8+
internal struct NePridumalNazvanieLayout: Layout {
9+
// This is code is not safe!
10+
// The Reason: It assumes that `proposal.width` is not `nil`.
11+
internal func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
12+
//
13+
// Initially Modified: __:__ PM Fri 8 Sep 2023
14+
// Last Modified: 06:08 PM Fri 8 Sep 2023
15+
//
16+
guard let availableWidth: CGFloat = proposal.width else { fatalError("`proposal.width` is `nil`") }
17+
18+
var occupiedWidth: CGFloat = .zero
19+
return subviews.reduce(CGSize.zero) { accumulator, view in
20+
let size: CGSize = view.sizeThatFits(.unspecified)
21+
occupiedWidth += size.width
22+
23+
if occupiedWidth > availableWidth {
24+
occupiedWidth = size.width
25+
return CGSize(width: availableWidth, height: accumulator.height + size.height)
26+
}
27+
return CGSize(width: availableWidth, height: accumulator.height == .zero ? size.height : accumulator.height)
28+
}
29+
}
30+
31+
internal func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
32+
var occupiedWidth: CGFloat = .zero
33+
var occupiedHeight: CGFloat = .zero
34+
35+
subviews.forEach { view in
36+
let size = view.sizeThatFits(.unspecified)
37+
if occupiedWidth + size.width > bounds.width {
38+
occupiedWidth = .zero; occupiedHeight += size.height
39+
}
40+
41+
let x = bounds.origin.x + size.width / 2 + occupiedWidth
42+
let y = bounds.origin.y + size.height / 2 + occupiedHeight
43+
44+
occupiedWidth += size.width
45+
view.place(at: CGPoint(x: x, y: y), anchor: .center, proposal: proposal)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)