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

Refactor: support self-sizing on SwiftUI #42

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 40 additions & 0 deletions Sources/KarrotListKit/SwiftUISupport/ComponentRepresented.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ struct ComponentRepresented<C: Component>: UIViewRepresentable {
func makeCoordinator() -> C.Coordinator {
component.makeCoordinator()
}

func _overrideSizeThatFits(
_ size: inout CGSize,
in proposedSize: _ProposedSize,
uiView: C.Content
) {
size = uiView.sizeThatFits(
CGSize(
width: proposedSize.width ?? UIView.noIntrinsicMetric,
height: proposedSize.height ?? UIView.noIntrinsicMetric
)
)
}

#if swift(>=5.7)
@available(iOS 16.0, *)
func sizeThatFits(
_ proposal: ProposedViewSize,
uiView: C.Content,
context: Context
) -> CGSize? {
uiView.sizeThatFits(
CGSize(
width: proposal.width ?? UIView.noIntrinsicMetric,
height: proposal.height ?? UIView.noIntrinsicMetric
)
)
}
#endif
}

private extension SwiftUI._ProposedSize {

var width: CGFloat? {
Mirror(reflecting: self).children.first(where: { $0.label == "width" })?.value as? CGFloat
}

var height: CGFloat? {
Mirror(reflecting: self).children.first(where: { $0.label == "height" })?.value as? CGFloat
}
}

extension Component {
Expand Down
Loading