From e15857d72eeff4166c6c4fe8acff2f6585185e5d Mon Sep 17 00:00:00 2001 From: Laszlo Teveli Date: Mon, 15 Jul 2024 18:45:00 +0200 Subject: [PATCH] Refactor --- Sources/Flow/Internal/Layout.swift | 44 ++++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Sources/Flow/Internal/Layout.swift b/Sources/Flow/Internal/Layout.swift index 984a1bb5..cf4f675d 100644 --- a/Sources/Flow/Internal/Layout.swift +++ b/Sources/Flow/Internal/Layout.swift @@ -97,16 +97,20 @@ struct FlowLayout: Sendable { private func alignAndPlace( _ item: Line.Element, in line: Lines.Element, - at placement: Size + at target: Size ) { - var placement = placement + var position = target + let lineDepth = line.size.depth let size = Size(breadth: item.size.breadth, depth: line.size.depth) let proposedSize = ProposedViewSize(size: size, axis: axis) - let depth = item.size.depth - if depth > 0 { - placement.depth += (align(item.item.subview.dimensions(proposedSize)) / depth) * (line.size.depth - depth) + let itemDepth = item.size.depth + if itemDepth > 0 { + let dimensions = item.item.subview.dimensions(proposedSize) + let alignedPosition = align(dimensions) + position.depth += (alignedPosition / itemDepth) * (lineDepth - itemDepth) } - item.item.subview.place(at: .init(size: placement, axis: axis), anchor: .topLeading, proposal: proposedSize) + let point = CGPoint(size: position, axis: axis) + item.item.subview.place(at: point, anchor: .topLeading, proposal: proposedSize) } private func calculateLayout( @@ -137,7 +141,7 @@ struct FlowLayout: Sendable { var lines: Lines = [] for (start, end) in breakpoints.adjacentPairs() { - var line = ItemWithSpacing(item: [], size: .zero) + var line = Lines.Element(item: [], size: .zero) for index in start ..< end { let subview = subviews[index] let size = sizes[index] @@ -159,23 +163,21 @@ struct FlowLayout: Sendable { } private func updateLineSpacings(in lines: inout Lines) { - let lineSpacings = lines.map { line in - line.item.reduce(into: ViewSpacing()) { $0.formUnion($1.item.cache.spacing) } - } - for index in lines.indices.dropFirst() { - let spacing = self.lineSpacing ?? lineSpacings[index].distance(to: lineSpacings[index.advanced(by: -1)], along: axis.perpendicular) - lines[index].leadingSpace = spacing + if let lineSpacing { + for index in lines.indices.dropFirst() { + lines[index].leadingSpace = lineSpacing + } + } else { + let lineSpacings = lines.map { line in + line.item.reduce(into: ViewSpacing()) { $0.formUnion($1.item.cache.spacing) } + } + for (previous, index) in lines.indices.adjacentPairs() { + let spacing = lineSpacings[index].distance(to: lineSpacings[previous], along: axis.perpendicular) + lines[index].leadingSpace = spacing + } } } - private func itemSpacing( - toPrevious index: S.Index, - subviews: S - ) -> CGFloat { - guard index != subviews.startIndex else { return 0 } - return self.itemSpacing ?? subviews[index.advanced(by: -1)].spacing.distance(to: subviews[index].spacing, along: axis) - } - private func updateFlexibleItems( in line: inout ItemWithSpacing, proposedSize: ProposedViewSize,