Cotton code
87 lines · 3.2 KB · SwiftRaw
// The red ink ring round a card's corner: the corner's words ringed by hand// (`RingedCorner`, an event's, a subscription's, a transaction's "why now"),// and the ring itself (`CornerRing`) the deck circles an open date corner// with while it is edited in place (CottonUI's `DeckEditable`).import CottonPaperimport SwiftUIenum CornerRingKnobs {    /// The corner's words, and the ring's reach round them.    static let wordsSize: CGFloat = 14    static let ringPad = EdgeInsets(top: 12, leading: 20, bottom: 12, trailing: 20)    static let stroke: CGFloat = 1.6}/// The corner's words typed and ringed by hand in red.public struct RingedCorner: View {    let words: String    var pressing = false    public init(words: String, pressing: Bool = false) {        self.words = words        self.pressing = pressing    }    public var body: some View {        Text(words)            .font(CottonFonts.type(CornerRingKnobs.wordsSize))            .lineLimit(1)            .faceInk(pressing ? .red : .ink)            .padding(CornerRingKnobs.ringPad)            .background(CornerRing())            .rotationEffect(.degrees(-3))            .accessibilityIdentifier("face-corner")    }}/// The red ink ring round an open corner (image 4): Card design's drawn ring/// when it is in the catalogue, else a ring drawn by hand in code — two/// passes of a pen that does not quite close.public struct CornerRing: View {    @Environment(\.theme) private var theme    public init() {}    public var body: some View {        if let ring = CottonMedia.image("edit-corner-ring") {            ring.resizable()                .blendMode(theme.printBlend)        } else {            ZStack {                PenRing(start: 0.06, sweep: 1.04, tilt: -2.5)                    .stroke(theme.print(.red), style: StrokeStyle(lineWidth: CornerRingKnobs.stroke, lineCap: .round))                PenRing(start: 0.55, sweep: 0.62, tilt: -1, squash: 0.94)                    .stroke(theme.print(.red).opacity(0.7), style: StrokeStyle(lineWidth: CornerRingKnobs.stroke * 0.7, lineCap: .round))            }            .blendMode(theme.printBlend)        }    }}/// One pass of a pen round an ellipse: from `start` (in turns) for `sweep`/// turns, the ellipse leant by `tilt` degrees, its height `squash` of the box.struct PenRing: Shape {    var start: Double    var sweep: Double    var tilt: Double    var squash: Double = 1    func path(in rect: CGRect) -> Path {        var path = Path()        let centre = CGPoint(x: rect.midX, y: rect.midY)        let rx = rect.width / 2, ry = rect.height / 2 * squash        let lean = tilt * .pi / 180        let steps = 72        for step in 0...steps {            let turn = start + sweep * Double(step) / Double(steps)            let angle = turn * 2 * .pi            // The pen drifts outward as it goes, so the ends do not meet.            let drift = 1 + 0.035 * Double(step) / Double(steps)            let x = cos(angle) * rx * drift, y = sin(angle) * ry * drift            let point = CGPoint(x: centre.x + x * cos(lean) - y * sin(lean), y: centre.y + x * sin(lean) + y * cos(lean))            if step == 0 { path.move(to: point) } else { path.addLine(to: point) }        }        return path    }}