Cotton code
193 lines · 8.6 KB · SwiftRaw
// Cotton Vault's own line glyphs, one per symbol name the core sends (and the// app's own places and moves), drawn the same shapes as Android's// (`apps/android/.../keys/vault/VaultUi.kt`, `glyph`): no platform icon set,// so both phones name the same things the same way. The stroke is the look's// `xxs` space; an unknown name draws a dot rather than nothing.import CottonKitimport SwiftUIstruct VaultGlyphView: View {    let symbol: String    var ink: Color    var size: CGFloat    @Environment(\.vault) private var look    var body: some View {        let width = look.gap("xxs")        Canvas { context, canvas in            VaultGlyphs.draw(symbol, in: &context, side: min(canvas.width, canvas.height), ink: ink, width: width)        }        .frame(width: size, height: size)        .accessibilityHidden(true)    }}enum VaultGlyphs {    static func draw(_ symbol: String, in context: inout GraphicsContext, side s: CGFloat, ink: Color, width: CGFloat) {        let style = StrokeStyle(lineWidth: width, lineCap: .round, lineJoin: .round)        func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: x * s, y: y * s) }        func stroke(_ path: Path) { context.stroke(path, with: .color(ink), style: style) }        func line(_ points: [(CGFloat, CGFloat)], close: Bool = false) {            var path = Path()            for (i, point) in points.enumerated() {                if i == 0 { path.move(to: p(point.0, point.1)) } else { path.addLine(to: p(point.0, point.1)) }            }            if close { path.closeSubpath() }            stroke(path)        }        func box(_ l: CGFloat, _ t: CGFloat, _ r: CGFloat, _ b: CGFloat, _ radius: CGFloat = 0.08) {            stroke(Path(roundedRect: CGRect(x: l * s, y: t * s, width: (r - l) * s, height: (b - t) * s), cornerRadius: radius * s))        }        func ring(_ x: CGFloat, _ y: CGFloat, _ r: CGFloat) {            stroke(Path(ellipseIn: CGRect(x: (x - r) * s, y: (y - r) * s, width: 2 * r * s, height: 2 * r * s)))        }        func dot(_ x: CGFloat, _ y: CGFloat, _ r: CGFloat) {            context.fill(Path(ellipseIn: CGRect(x: x * s - r, y: y * s - r, width: 2 * r, height: 2 * r)), with: .color(ink))        }        func star() -> Path {            var path = Path()            for i in 0..<10 {                let r: CGFloat = i % 2 == 0 ? 0.42 : 0.18                let a = (-90.0 + Double(i) * 36.0) * .pi / 180                let point = p(0.5 + r * CGFloat(cos(a)), 0.54 + r * CGFloat(sin(a)))                if i == 0 { path.move(to: point) } else { path.addLine(to: point) }            }            path.closeSubpath()            return path        }        switch symbol {        case "copy":            box(0.32, 0.32, 0.82, 0.82)            line([(0.2, 0.66), (0.2, 0.2), (0.66, 0.2)])        case "eye":            var eye = Path()            eye.move(to: p(0.1, 0.5))            eye.addQuadCurve(to: p(0.9, 0.5), control: p(0.5, 0.12))            eye.addQuadCurve(to: p(0.1, 0.5), control: p(0.5, 0.88))            eye.closeSubpath()            stroke(eye)            ring(0.5, 0.5, 0.12)        case "eye.slash":            draw("eye", in: &context, side: s, ink: ink, width: width)            line([(0.18, 0.82), (0.82, 0.18)])        case "key", "login", "passkey":            ring(0.32, 0.5, 0.16)            line([(0.48, 0.5), (0.88, 0.5)])            line([(0.78, 0.5), (0.78, 0.64)])            line([(0.66, 0.5), (0.66, 0.62)])        case "clock", "code":            ring(0.5, 0.5, 0.38)            line([(0.5, 0.28), (0.5, 0.5), (0.66, 0.6)])        case "person", "identity":            ring(0.5, 0.34, 0.16)            line([(0.18, 0.88), (0.22, 0.72), (0.5, 0.6), (0.78, 0.72), (0.82, 0.88)])        case "star", "star.slash":            stroke(star())            if symbol == "star.slash" { line([(0.15, 0.85), (0.85, 0.15)]) }        case "star.fill":            context.fill(star(), with: .color(ink))        case "trash", "bin":            line([(0.16, 0.26), (0.84, 0.26)])            line([(0.38, 0.26), (0.4, 0.14), (0.6, 0.14), (0.62, 0.26)])            line([(0.24, 0.26), (0.3, 0.88), (0.7, 0.88), (0.76, 0.26)])        case "pencil":            line([(0.2, 0.8), (0.24, 0.62), (0.66, 0.2), (0.8, 0.34), (0.38, 0.76)], close: true)        case "safari", "globe":            ring(0.5, 0.5, 0.38)            line([(0.12, 0.5), (0.88, 0.5)])            stroke(Path(ellipseIn: CGRect(x: 0.34 * s, y: 0.12 * s, width: 0.32 * s, height: 0.76 * s)))        case "arrow.uturn.left":            line([(0.36, 0.2), (0.16, 0.4), (0.36, 0.6)])            line([(0.16, 0.4), (0.64, 0.4), (0.84, 0.6), (0.64, 0.82), (0.44, 0.82)])        case "note":            box(0.22, 0.12, 0.78, 0.88)            line([(0.34, 0.36), (0.66, 0.36)])            line([(0.34, 0.52), (0.66, 0.52)])            line([(0.34, 0.68), (0.54, 0.68)])        case "card":            box(0.1, 0.24, 0.9, 0.76)            line([(0.1, 0.4), (0.9, 0.4)])            line([(0.2, 0.62), (0.4, 0.62)])        case "wifi":            for r in [0.18, 0.32, 0.46] as [CGFloat] {                var arc = Path()                arc.addArc(center: p(0.5, 0.8), radius: r * s, startAngle: .degrees(-135), endAngle: .degrees(-45), clockwise: false)                stroke(arc)            }            dot(0.5, 0.8, width)        case "address":            var pin = Path()            pin.move(to: p(0.5, 0.9))            pin.addQuadCurve(to: p(0.2, 0.38), control: p(0.16, 0.5))            pin.addQuadCurve(to: p(0.5, 0.1), control: p(0.3, 0.1))            pin.addQuadCurve(to: p(0.8, 0.38), control: p(0.7, 0.1))            pin.addQuadCurve(to: p(0.5, 0.9), control: p(0.84, 0.5))            pin.closeSubpath()            stroke(pin)            ring(0.5, 0.38, 0.1)        case "api_key":            line([(0.36, 0.28), (0.14, 0.5), (0.36, 0.72)])            line([(0.64, 0.28), (0.86, 0.5), (0.64, 0.72)])        case "document":            line([(0.22, 0.12), (0.6, 0.12), (0.78, 0.3), (0.78, 0.88), (0.22, 0.88)], close: true)            line([(0.6, 0.12), (0.6, 0.3), (0.78, 0.3)])        // The app's own places and moves (not the core's symbols).        case "tab.items":            box(0.14, 0.18, 0.86, 0.82)            line([(0.14, 0.42), (0.86, 0.42)])            line([(0.3, 0.62), (0.6, 0.62)])        case "tab.generator", "dice":            box(0.16, 0.16, 0.84, 0.84, 0.14)            for (x, y) in [(0.36, 0.36), (0.64, 0.64), (0.64, 0.36), (0.36, 0.64)] as [(CGFloat, CGFloat)] { dot(x, y, width * 1.4) }        case "tab.security":            line([(0.5, 0.1), (0.84, 0.24), (0.8, 0.56), (0.5, 0.9), (0.2, 0.56), (0.16, 0.24)], close: true)            line([(0.36, 0.5), (0.46, 0.6), (0.66, 0.4)])        // Cotton Health's places (the same glyphs on Android: a test tube, a        // heart, a report with its lines).        case "tab.labs":            line([(0.38, 0.1), (0.38, 0.72)])            line([(0.62, 0.1), (0.62, 0.72)])            line([(0.3, 0.1), (0.7, 0.1)])            stroke(Path { path in                path.addArc(center: p(0.5, 0.72), radius: 0.12 * s, startAngle: .degrees(0), endAngle: .degrees(180), clockwise: false)            })            line([(0.38, 0.48), (0.62, 0.48)])        case "tab.body":            stroke(Path { path in                path.move(to: p(0.5, 0.84))                path.addCurve(to: p(0.5, 0.3), control1: p(0.04, 0.52), control2: p(0.24, 0.06))                path.addCurve(to: p(0.5, 0.84), control1: p(0.76, 0.06), control2: p(0.96, 0.52))            })        case "tab.reports":            line([(0.22, 0.12), (0.6, 0.12), (0.78, 0.3), (0.78, 0.88), (0.22, 0.88)], close: true)            line([(0.34, 0.46), (0.66, 0.46)])            line([(0.34, 0.6), (0.66, 0.6)])            line([(0.34, 0.74), (0.54, 0.74)])        case "tab.settings":            line([(0.14, 0.3), (0.86, 0.3)])            line([(0.14, 0.7), (0.86, 0.7)])            dot(0.36, 0.3, 0.09 * s)            dot(0.64, 0.7, 0.09 * s)        case "search":            ring(0.42, 0.42, 0.26)            line([(0.62, 0.62), (0.86, 0.86)])        case "plus":            line([(0.5, 0.18), (0.5, 0.82)])            line([(0.18, 0.5), (0.82, 0.5)])        case "back":            line([(0.62, 0.16), (0.28, 0.5), (0.62, 0.84)])        case "chevron":            line([(0.4, 0.24), (0.64, 0.5), (0.4, 0.76)])        case "check":            line([(0.2, 0.52), (0.42, 0.74), (0.82, 0.28)])        case "close":            line([(0.24, 0.24), (0.76, 0.76)])            line([(0.76, 0.24), (0.24, 0.76)])        default:            // A name with no glyph draws nothing (SHAPES "Decided" 4).            Log.write("vault: no glyph for \(symbol)")        }    }}