Ancestors

Written by John Siracusa on 2024-12-18 at 03:31

Welcome to my new series on things I don’t understand about Apple’s premier user interface framework. I’m calling it…

[#]SwiftUWhy

To be clear, these are things I don’t understand, not necessarily things that are “wrong.” They sure look wrong (or at least “suboptimal”) to me! But maybe there are good reasons, and I just don’t know them yet. SwiftUI experts and historians, please tell me!

The first entry is coming up…

=> More informations about this toot | More toots from siracusa@mastodon.social

Written by John Siracusa on 2024-12-18 at 03:35

Why in the world does this interface look like this:

.padding(.top, 1)

.padding(.leading, 2)

.padding(.bottom, 3)

.padding(.trailing, 4)

…or this:

.padding(EdgeInsets(top: 1, leading: 2, bottom: 3, trailing: 4))

…instead of the much more idiomatic:

.padding(top: 1, leading: 2, bottom: 3, trailing: 4)

Like, who in the world thought of this foo(.key, value) API interface in a language with named parameters?!

[#]SwiftUWhy

=> More informations about this toot | More toots from siracusa@mastodon.social

Written by marksmith on 2024-12-18 at 04:47

@siracusa I don't like it either. I add this to my projects:

https://gist.github.com/mjmsmith/040ffb4a8536b61bf479ad5b0abeea6e

=> More informations about this toot | More toots from marksmith@mastodon.social

Written by robb on 2024-12-18 at 17:55

@marksmith @siracusa FWIW, that changes the semantics of nil from “platform dependent padding” to “no padding” and if you were to animate from nil to some value, you'd lose the view identity due to the conditional.

Not trying to tell you what to do with your code base, just calling this out for those unaware.

=> More informations about this toot | More toots from dlx@mastodon.social

Written by marksmith on 2024-12-18 at 18:12

@dlx @siracusa I could be misunderstanding, but doesn't it have the same behavior as the built-in padding modifier? (In the image, trailing is applied both ways with the same result.)

=> View attached media

=> More informations about this toot | More toots from marksmith@mastodon.social

Toot

Written by robb on 2024-12-18 at 18:15

@marksmith @siracusa This only affects the case where you pass in nil, e.g. .padding(.bottom) which is effectively .padding(.bottom, nil).

=> View attached media

=> More informations about this toot | More toots from dlx@mastodon.social

Descendants

Written by marksmith on 2024-12-18 at 18:56

@dlx @siracusa Oh yes, you're right. In my modifier, passing nil (implicitly or explicitly) leaves the value alone rather than resetting it to the default.

A bit hacky under the covers, but I could use .nan as the default value in the padding() View extension, and test for .isNaN instead of nil in EdgePaddingModifier. That would make the behaviors match.

=> More informations about this toot | More toots from marksmith@mastodon.social

Written by robb on 2024-12-18 at 19:01

@marksmith @siracusa yeah, if you think that's worth it.

=> More informations about this toot | More toots from dlx@mastodon.social

Written by Doug on 2024-12-18 at 19:05

@dlx @marksmith @siracusa Should be easy enough to fix the animations by summing the values into a single EdgeInsets to remove the conditional too :)

=> More informations about this toot | More toots from digitalfx@mastodon.social

Written by Doug on 2024-12-18 at 19:06

@dlx @marksmith @siracusa Ah, although that would then conflict with the .nan default 😕

=> More informations about this toot | More toots from digitalfx@mastodon.social

Written by marksmith on 2024-12-18 at 19:11

@digitalfx @dlx @siracusa It's enough to make you think Apple had reasons for doing it the way they did in the first place. :)

=> More informations about this toot | More toots from marksmith@mastodon.social

Written by John Siracusa on 2024-12-18 at 19:47

@marksmith It’s not! There’s no reason to bend over backwards to match the weird functionality of an existing API when adding new, more idiomatic versions. The weird versions still exist for the people who want them!

=> More informations about this toot | More toots from siracusa@mastodon.social

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113675189862576629
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
307.667588 milliseconds
Gemini-to-HTML Time
3.216021 milliseconds

This content has been proxied by September (3851b).