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

Add ResultBuilder-backed API for subview distribution #116

Closed
dfed opened this issue Apr 21, 2024 · 1 comment · Fixed by #117
Closed

Add ResultBuilder-backed API for subview distribution #116

dfed opened this issue Apr 21, 2024 · 1 comment · Fixed by #117

Comments

@dfed
Copy link
Contributor

dfed commented Apr 21, 2024

Paralayout's applyVerticalSubviewDistribution and applyHorizontalSubviewDistribution methods are incredibly powerful. Adding a result builder could make the code that creates these distributions more expressive.

In the simplest of cases, utilizing a distribution builder would enable removing the need to declare an array. Taking us from:

containerView.applyVerticalSubviewDistribution(
    [
        1.flexible,
        titleLabel,
        16.fixed,
        bodyLabel,
        16.fixed,
        actionButton,
        1.flexible,
    ]
)

to:

containerView.applyVerticalSubviewDistribution {
    1.flexible
    titleLabel
    16.fixed
    bodyLabel
    16.fixed
    actionButton
    1.flexible
}

But that's just the beginning. Since result builders allow for inline if statements, they'd enable writing distribution like:

containerView.applyVerticalSubviewDistribution {
    1.flexible
    titleLabel
    if bodyLabel.hasText {
        16.fixed
        bodyLabel
    }
    16.fixed
    actionButton
    1.flexible
}

Result builders also enable inlining for loop and if-else statements, enabling consumers to inline complex distributions. The benefits here purely syntactic – there'd be zero changes required to the underlying Paralayout library. The only additions would be:

  1. A @resultbuilder public struct ViewDistributionBuilder type.
  2. A applyVerticalSubviewDistribution method that takes a @ViewDistributionBuilder _ distribution: () -> [ViewDistributionSpecifying] closure rather than a _ distribution: [ViewDistributionSpecifying] argument. This new method would call through to the existing applyVerticalSubviewDistribution method.
  3. A applyHorizontalSubviewDistribution method that takes a @ViewDistributionBuilder _ distribution: () -> [ViewDistributionSpecifying] closure rather than a _ distribution: [ViewDistributionSpecifying] argument. This new method would call through to the existing applyVerticalSubviewDistribution method.

Result builders were introduced in Swift 5.4, so this functionality would only be added for consumers using Swift 5.4 and above. I'd be happy to put up a PR that accomplishes this – I recently did something similar in another view layout library.

@NickEntin
Copy link
Collaborator

Sounds great! Love the inline if example 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants