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

SwiftUI Style .foo() wrappers #88

Merged
merged 3 commits into from
May 6, 2020
Merged

SwiftUI Style .foo() wrappers #88

merged 3 commits into from
May 6, 2020

Conversation

kyleve
Copy link
Collaborator

@kyleve kyleve commented Apr 26, 2020

This PR implements SwiftUI-style wrappers which I think improve the succinctness and readability of Blueprint code.

The main file to check out for updates/examples to written consumer code is ViewController.swift.

SwiftUI-Style .foo() methods for wrapping types

Before this change, if you wanted to have a scroll view, which wrapped a box, which wrapped an inset, which wrapped a constrained size, which wrapped a label (and so on), the code would look something like this:

ScrollView(.fittingHeight) (
   wrapping: Box(
        backgroundColor .lightGrey,
       wrapping: Inset(
          uniformInset: 10.0,
          wrapping: ConstrainedSize(
             height: .atLeast(20.0),
             wrapping: Label(
                text: "Hello, world!"
             )
         )
      )
   )
)

Which obviously is pretty verbose... It's nice that from the shape of the code you see what's going on, but it's pretty hard to follow in practice, especially if these inner statements get broken up into intermediary variables.

After this change, to write this code, this becomes like the following, similar to SwiftUI:

Label(text: "Hello, World!")
   .constrainedTo(height: .atLeast(20.0))
   .inset(by: 20.0)
   .box(background: .lightGrey)
   .scrollable(.fittingHeight)

BlueprintUI/Sources/Layout/ConstrainedAspectRatio.swift Outdated Show resolved Hide resolved
BlueprintUI/Sources/Layout/Inset.swift Outdated Show resolved Hide resolved
BlueprintUI/Sources/Layout/Stack.swift Outdated Show resolved Hide resolved
BlueprintUICommonControls/Sources/Box.swift Show resolved Hide resolved
@kylebshr
Copy link
Contributor

1) SwiftUI methods

I really like this, and actually added similar functions in a side project over the weekend for some custom "view modifiers". I think it's both easier to read and write - I often start out with a base element like a label, and in the current form it's clunky to repeatedly wrap it in elements, and often "obscures" the real UI.

2) Stack improvements

StackChild: I think this makes sense, having named static methods instead of having to read the docs for grow/shrink priority every time seems great (jokes aside, the names are way more readable and beginner friendly).

Operators: I'm not a huge fan of custom operators for UI, since they're not very discoverable, lead to divergence in code style (since it doesn't make sense to remove the functions) and it just doesn't "feel right" to me. Not a super strong opinion here though :)

@kyleve
Copy link
Collaborator Author

kyleve commented Apr 28, 2020

@kylebshr re operators: generally agree, but I think there's a time and place for them, eg common operations like this. As long as you can do everything without operators that you can do with them, I feel it's fine.

@kyleve
Copy link
Collaborator Author

kyleve commented Apr 29, 2020

@kyleve: TODO: Split Stack changes out into a new PR

  • TODO: Split Stack changes out into a new PR

@kyleve
Copy link
Collaborator Author

kyleve commented Apr 30, 2020

I've removed the stack changes from this PR

@kyleve kyleve changed the title [For Discussion] SwiftUI Style .foo() wrappers, Stack improvements [For Discussion] SwiftUI Style .foo() wrappers Apr 30, 2020
@kyleve kyleve changed the title [For Discussion] SwiftUI Style .foo() wrappers SwiftUI Style .foo() wrappers Apr 30, 2020
@kyleve kyleve marked this pull request as ready for review April 30, 2020 04:15
Copy link
Contributor

@kylebshr kylebshr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after comments!

@kyleve kyleve force-pushed the kve/api-ergo-improvements branch from 55aa13a to 3d89548 Compare May 6, 2020 02:52
@kylebshr
Copy link
Contributor

kylebshr commented May 6, 2020 via email

@kyleve kyleve force-pushed the kve/api-ergo-improvements branch from 0e1e6d1 to be14258 Compare May 6, 2020 03:08
@kyleve kyleve merged commit 4797e78 into master May 6, 2020
@mattfaluotico
Copy link
Contributor

Popping in for a comment on Stack Improvements, even though this is merged. IMO you get a bit more clarity about what is being added when the Element proceeds the stacking traits.

Was dabbling and added a similar StackChild, with the flex traits using function builders (like this PR adds)

so you could get:

Row {
    Label(text: "A truly great label")
        .growPriority(1)
}

I like @kyleve's use of fixed a lot!

Row {
    Label(text: "A truly great label")
        .fixed
    Spacer()
    Label(text: "Another great label")    
}

@watt watt deleted the kve/api-ergo-improvements branch November 24, 2021 02:15
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 this pull request may close these issues.

3 participants