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

8) Infinite size elements #450

Merged
merged 3 commits into from
Apr 14, 2023
Merged

Conversation

watt
Copy link
Collaborator

@watt watt commented Apr 11, 2023

This is PR 8 in a stack targeting feature/caffeinated-layout. See #447.

This PR updates existing elements that don't fully adhere to the Caffeinated Layout contract, Image and TextField.

Additionally, ScrollView has been updated to protect against placing elements into infinite bounds. ScrollView is uniquely vulnerable to this because it introduces unconstrained constraints to its subelements. Most elements don't need to worry about this, but for any that do, a new convenience for replacing infinite values on CGSize can help.

@watt watt requested a review from a team as a code owner April 11, 2023 21:57
@watt watt mentioned this pull request Apr 12, 2023

extension CGSize {
/// A size with `infinity` in both dimensions.
public static let infinity = CGSize(width: CGFloat.infinity, height: .infinity)
Copy link
Contributor

Choose a reason for hiding this comment

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

One has to love it.

case .legacy:
mode = .useImageSize
case .caffeinated:
mode = .infinite
Copy link
Contributor

@nononoah nononoah Apr 13, 2023

Choose a reason for hiding this comment

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

Could you unpack this a bit for me?

Given an unconstrained measurement space, Image now requests infinite layout space. Does that not change the layout behavior of images in these contexts, and therefore any container of these images?

Or is it that .infinite opts them out of caffeinated layout in a way that leads to the restoration of existing behavior?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Given an unconstrained measurement space, Image now requests infinite layout space. Does that not change the layout behavior of images in these contexts, and therefore any container of these images?

It does! Practically speaking, there's not that much impact, because the need to measure an element fully unconstrained is somewhat rare.

There's perhaps a semantic change — if you were counting on unconstrained measurement to get you something like an intrinsic size, or SwiftUI's concept of "ideal size", that's no longer possible. But it was never guaranteed to work that way, and would be inconsistent depending on what you measured.

Or is it that .infinite opts them out of caffeinated layout in a way that leads to the restoration of existing behavior?

Nope, there's no way to opt out at the element level.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this because of the cache hinting optimizations? I guess I would expect for the measurement modes that don't stretch the image, I'd expect to return the size of image, even in an infinite constraint.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this because of the cache hinting optimizations?

It's required by the new layout contract, yeah.

I guess I would expect for the measurement modes that don't stretch the image, I'd expect to return the size of image, even in an infinite constraint.

Well, the aspect fit & fill modes already fill space today, even under finite constraints. Given one finite constraint, they'll fill it, no matter how large. So the reasoning is that if the size grows continuously with the constraint, why wouldn't it grow to infinity when the constraint grows to infinity?

We could change the behavior of these modes to return the size of the image without filling space, and that would be compliant too, but it would be a much bigger breaking change to existing layouts.

@watt watt merged commit e06aa03 into feature/caffeinated-layout Apr 14, 2023
@watt watt deleted the watt/caffeinated-layout/8 branch April 14, 2023 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants