Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly account for zero-width spacers in implied distribution #127

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Paralayout/ViewDistributionItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIView>()
Expand All @@ -92,6 +93,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable {

case .fixed:
totalFixedSpace += layoutSize
hasFixedSpacers = true

case .flexible:
totalFlexibleSpace += layoutSize
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions ParalayoutTests/DistributionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

}