Skip to content

Commit

Permalink
Upload a draft / Create ./ChipLayout.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirCreator authored Sep 8, 2023
1 parent 83934ae commit f92a3b4
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ChipLayout.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit f92a3b4

Please sign in to comment.