From cc3615707af7e24477dfb69f3ffc85829b6e8fdd Mon Sep 17 00:00:00 2001 From: Nick Entin Date: Tue, 11 Jun 2024 16:22:03 -0700 Subject: [PATCH 1/2] Correctly account for zero-width spacers in implied distribution Currently the implied distribution does not behave as documented in cases where the fixed spacers sum to zero (either by using zero-size spacers or by combining positive- and negative-sized spacers). This updates the logic to correctly identify the use of _any_ fixed spacer. Resolves #126 --- Paralayout/ViewDistributionItem.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Paralayout/ViewDistributionItem.swift b/Paralayout/ViewDistributionItem.swift index ee61fae..740c147 100644 --- a/Paralayout/ViewDistributionItem.swift +++ b/Paralayout/ViewDistributionItem.swift @@ -66,6 +66,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable { var distributionItems = [ViewDistributionItem]() var totalViewSize: CGFloat = 0 var totalFixedSpace: CGFloat = 0 + var hasFixedSpacers: Bool = false var totalFlexibleSpace: CGFloat = 0 var subviewsToDistribute = Set() @@ -92,6 +93,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable { case .fixed: totalFixedSpace += layoutSize + hasFixedSpacers = true case .flexible: totalFlexibleSpace += layoutSize @@ -107,7 +109,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable { // Insert flexible space if necessary. if totalFlexibleSpace == 0 { - if totalFixedSpace == 0 { + if !hasFixedSpacers { // No spacers at all: insert `1.flexible` between all items. for i in 0 ..< (distributionItems.count + 1) { distributionItems.insert(1.flexible, at: i * 2) From 5eb1e6239a6ca4d1949f3bd6fe7f53ed04490653 Mon Sep 17 00:00:00 2001 From: Nick Entin Date: Thu, 29 Aug 2024 01:02:00 -0700 Subject: [PATCH 2/2] Add test --- ParalayoutTests/DistributionTests.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ParalayoutTests/DistributionTests.swift b/ParalayoutTests/DistributionTests.swift index 03645df..b869fd6 100644 --- a/ParalayoutTests/DistributionTests.swift +++ b/ParalayoutTests/DistributionTests.swift @@ -214,4 +214,26 @@ final class DistributionTests: XCTestCase { } } + // MARK: - Tests - Spacers + + @MainActor + func testZeroSizeFixedSpacer() { + let container = UIView(frame: .init(x: 0, y: 0, width: 100, height: 400)) + container.semanticContentAttribute = .forceRightToLeft + + let firstSubview = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100)) + container.addSubview(firstSubview) + let secondSubview = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100)) + container.addSubview(secondSubview) + + container.applyVerticalSubviewDistribution { + firstSubview + 0.fixed + secondSubview + } + + XCTAssertEqual(firstSubview.frame, .init(x: 0, y: 100, width: 100, height: 100)) + XCTAssertEqual(secondSubview.frame, .init(x: 0, y: 200, width: 100, height: 100)) + } + }