Skip to content

Instantly share code, notes, and snippets.

@sebj
Last active September 1, 2023 09:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebj/a053cc42005e214a4257cb7014fdaa15 to your computer and use it in GitHub Desktop.
Save sebj/a053cc42005e214a4257cb7014fdaa15 to your computer and use it in GitHub Desktop.
AnyInsettableShape
/// A type-erased shape that is able to inset itself to produce another shape.
///
/// See: [AnyShape](https://developer.apple.com/documentation/swiftui/anyshape), [InsettableShape](https://developer.apple.com/documentation/swiftui/insettableshape)
struct AnyInsettableShape: InsettableShape {
private let _sizeThatFits: (ProposedViewSize) -> CGSize
private let _path: (CGRect) -> Path
private let _inset: (CGFloat) -> AnyInsettableShape
init<S>(_ shape: S) where S : InsettableShape {
_sizeThatFits = shape.sizeThatFits
_path = shape.path
_inset = { AnyInsettableShape(shape.inset(by: $0)) }
}
func sizeThatFits(_ proposal: ProposedViewSize) -> CGSize {
_sizeThatFits(proposal)
}
func path(in rect: CGRect) -> Path {
_path(rect)
}
func inset(by amount: CGFloat) -> some InsettableShape {
_inset(amount)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment