Last active
August 13, 2025 20:12
-
-
Save juliensagot/62b8d081dc9b1bc6243a3c729eb63bbe to your computer and use it in GitHub Desktop.
Adds horizontal/vertical inset support to Shapes with full InsettableShape compatibility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private struct AsymmetricInsettableShape<S: Shape>: InsettableShape { | |
| private let shape: S | |
| private var horizontal: CGFloat | |
| private var vertical: CGFloat | |
| private var insetAccumulator: CGFloat = 0 | |
| init( | |
| shape: S, | |
| horizontal: CGFloat, | |
| vertical: CGFloat | |
| ) { | |
| self.shape = shape | |
| self.horizontal = horizontal | |
| self.vertical = vertical | |
| } | |
| func path(in rect: CGRect) -> Path { | |
| let insetRect = rect.insetBy( | |
| dx: horizontal + insetAccumulator, | |
| dy: vertical + insetAccumulator | |
| ) | |
| return shape.path(in: insetRect) | |
| } | |
| func inset(by amount: CGFloat) -> some InsettableShape { | |
| var copy = self | |
| copy.insetAccumulator += amount | |
| return copy | |
| } | |
| var animatableData: AnimatablePair<CGFloat, AnimatablePair<CGFloat, CGFloat>> { | |
| get { AnimatablePair(horizontal, AnimatablePair(vertical, insetAccumulator)) } | |
| set { | |
| horizontal = newValue.first | |
| vertical = newValue.second.first | |
| insetAccumulator = newValue.second.second | |
| } | |
| } | |
| } | |
| extension Shape { | |
| public func inset(horizontal: CGFloat, vertical: CGFloat) -> some InsettableShape { | |
| AsymmetricInsettableShape(shape: self, horizontal: horizontal, vertical: vertical) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment