SPB Git

Repository Public License

LunarShape.swift 1.6 KB 47 lines swift

1import SwiftUI23/// The three silhouettes of the system: capsule, continuous rounded rectangle, circle. One concrete type so that4/// modifiers can take "a Lunar shape" without generics.5public struct LunarShape: InsettableShape, Hashable, Sendable {6    public enum Kind: Hashable, Sendable {7        case capsule8        case rounded(CGFloat)9        case circle10    }1112    public var kind: Kind13    var inset: CGFloat = 01415    public init(_ kind: Kind) { self.kind = kind }1617    public static let capsule = LunarShape(.capsule)18    public static let circle = LunarShape(.circle)19    public static func rounded(_ radius: CGFloat) -> LunarShape { LunarShape(.rounded(radius)) }20    /// Radius tokens as shapes.21    public static let xs = rounded(LunarRadius.xs)22    public static let sm = rounded(LunarRadius.sm)23    public static let md = rounded(LunarRadius.md)24    public static let lg = rounded(LunarRadius.lg)25    public static let xl = rounded(LunarRadius.xl)2627    public func path(in rect: CGRect) -> Path {28        let r = rect.insetBy(dx: inset, dy: inset)29        guard r.width > 0, r.height > 0 else { return Path() }30        switch kind {31        case .capsule:32            return Capsule(style: .continuous).path(in: r)33        case .rounded(let radius):34            let clamped = max(0, min(radius - inset, min(r.width, r.height) / 2))35            return RoundedRectangle(cornerRadius: clamped, style: .continuous).path(in: r)36        case .circle:37            return Circle().path(in: r)38        }39    }4041    public func inset(by amount: CGFloat) -> LunarShape {42        var copy = self43        copy.inset += amount44        return copy45    }46}47
Files main
Menu

Theme