From f92a3b467eb602d18f27458052a567a71cf642fe Mon Sep 17 00:00:00 2001 From: Vladimir <49140851+VladimirCreator@users.noreply.github.com> Date: Fri, 8 Sep 2023 18:25:23 +0500 Subject: [PATCH] Upload a draft / Create `./ChipLayout.swift` --- ChipLayout.swift | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ChipLayout.swift diff --git a/ChipLayout.swift b/ChipLayout.swift new file mode 100644 index 0000000..9b6ed7e --- /dev/null +++ b/ChipLayout.swift @@ -0,0 +1,48 @@ +/* Read Me + -> History Entry | iPadOS 17.0 | Swift Playgrounds | _ | ChipLayout.swift | Initially Modified: 03:29 PM Fri 08 Sep 2023 + -> History Entry | iPadOS 17.0 | Swift Playgrounds | _ | ChipLayout.swift | Last Modified: 06:08 PM Fri 08 Sep 2023 +*/ + +import SwiftUI + +internal struct NePridumalNazvanieLayout: Layout { + // This is code is not safe! + // The Reason: It assumes that `proposal.width` is not `nil`. + internal func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { + // + // Initially Modified: __:__ PM Fri 8 Sep 2023 + // Last Modified: 06:08 PM Fri 8 Sep 2023 + // + guard let availableWidth: CGFloat = proposal.width else { fatalError("`proposal.width` is `nil`") } + + var occupiedWidth: CGFloat = .zero + return subviews.reduce(CGSize.zero) { accumulator, view in + let size: CGSize = view.sizeThatFits(.unspecified) + occupiedWidth += size.width + + if occupiedWidth > availableWidth { + occupiedWidth = size.width + return CGSize(width: availableWidth, height: accumulator.height + size.height) + } + return CGSize(width: availableWidth, height: accumulator.height == .zero ? size.height : accumulator.height) + } + } + + internal func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { + var occupiedWidth: CGFloat = .zero + var occupiedHeight: CGFloat = .zero + + subviews.forEach { view in + let size = view.sizeThatFits(.unspecified) + if occupiedWidth + size.width > bounds.width { + occupiedWidth = .zero; occupiedHeight += size.height + } + + let x = bounds.origin.x + size.width / 2 + occupiedWidth + let y = bounds.origin.y + size.height / 2 + occupiedHeight + + occupiedWidth += size.width + view.place(at: CGPoint(x: x, y: y), anchor: .center, proposal: proposal) + } + } +}