SPB Git

Repository Public

Theme.swift 14.5 KB 338 lines swift

1// SP Vault — visual identity shared by the apps and the AutoFill extension: ink / paper surfaces, verdigris accent,2// brass for cautions, serif headings, monospaced secrets. Same tokens as apps/web/src/styles.css.3import SwiftUI4#if os(macOS)5import AppKit6#else7import UIKit8#endif910extension Color {11    init(hex: UInt32, alpha: Double = 1) {12        self.init(.sRGB, red: Double((hex >> 16) & 0xff) / 255, green: Double((hex >> 8) & 0xff) / 255, blue: Double(hex & 0xff) / 255, opacity: alpha)13    }1415    /// A colour that follows the light / dark appearance.16    init(light: UInt32, dark: UInt32, lightAlpha: Double = 1, darkAlpha: Double = 1) {17        #if os(macOS)18        self.init(nsColor: NSColor(name: nil) { a in19            let isDark = a.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua20            return NSColor(Color(hex: isDark ? dark : light, alpha: isDark ? darkAlpha : lightAlpha))21        })22        #else23        self.init(uiColor: UIColor { t in24            let isDark = t.userInterfaceStyle == .dark25            return UIColor(Color(hex: isDark ? dark : light, alpha: isDark ? darkAlpha : lightAlpha))26        })27        #endif28    }29}3031enum Palette {32    static let bg = Color(light: 0xf5f3ee, dark: 0x0c1113)33    static let s1 = Color(light: 0xffffff, dark: 0x131b1e)34    static let s2 = Color(light: 0xf1eee7, dark: 0x19242a)35    static let s3 = Color(light: 0xe8e4da, dark: 0x1f2d34)36    static let line = Color(light: 0xddd8cc, dark: 0x24323a)37    static let lineStrong = Color(light: 0xc3bcac, dark: 0x34474f)38    static let text = Color(light: 0x132022, dark: 0xe7efee)39    static let muted = Color(light: 0x536468, dark: 0x8fa3a6)40    static let accent = Color(light: 0x0e7c61, dark: 0x3fd0a8)41    static let accentInk = Color(light: 0xffffff, dark: 0x052219)42    static let accentSoft = Color(light: 0x0e7c61, dark: 0x3fd0a8, lightAlpha: 0.09, darkAlpha: 0.12)43    static let accentLine = Color(light: 0x0e7c61, dark: 0x3fd0a8, lightAlpha: 0.38, darkAlpha: 0.4)44    static let brass = Color(light: 0x855b0b, dark: 0xd9b46c)45    static let brassSoft = Color(light: 0x9a6b12, dark: 0xd9b46c, lightAlpha: 0.1, darkAlpha: 0.12)46    static let brassLine = Color(light: 0x9a6b12, dark: 0xd9b46c, lightAlpha: 0.4, darkAlpha: 0.38)47    static let danger = Color(light: 0xc2362b, dark: 0xff6b5e)48    static let dangerInk = Color(light: 0xffffff, dark: 0x2a0906)49    static let dangerSoft = Color(light: 0xc2362b, dark: 0xff6b5e, lightAlpha: 0.07, darkAlpha: 0.11)50    static let dangerLine = Color(light: 0xc2362b, dark: 0xff6b5e, lightAlpha: 0.38, darkAlpha: 0.42)51    static let digit = Color(light: 0x1559a6, dark: 0x7cc4ff)52    static let symbol = Color(light: 0x8a4d06, dark: 0xd9b46c)53}5455enum Metrics {56    static let radius: CGFloat = 1057    static let radiusLarge: CGFloat = 1458    #if os(macOS)59    static let rowPadding: CGFloat = 1260    static let tap: CGFloat = 2861    #else62    static let rowPadding: CGFloat = 1663    static let tap: CGFloat = 4464    #endif65}6667extension Font {68    static func serifTitle(_ size: CGFloat) -> Font { .system(size: size, weight: .semibold, design: .serif) }69    static var vaultLargeTitle: Font { .system(.largeTitle, design: .serif, weight: .semibold) }70    static var vaultTitle: Font { .system(.title2, design: .serif, weight: .semibold) }71    static var vaultHeadline: Font { .system(.title3, design: .serif, weight: .semibold) }72    static var secret: Font { .system(.body, design: .monospaced) }73    static var eyebrow: Font { .system(.caption2, weight: .semibold) }74}7576// ── surfaces ──7778struct VaultCard: ViewModifier {79    var padding: CGFloat = 080    var fill: Color = Palette.s181    func body(content: Content) -> some View {82        content83            .padding(padding)84            .background(fill, in: RoundedRectangle(cornerRadius: Metrics.radiusLarge, style: .continuous))85            .overlay(RoundedRectangle(cornerRadius: Metrics.radiusLarge, style: .continuous).strokeBorder(Palette.line, lineWidth: 1))86    }87}8889extension View {90    func vaultCard(padding: CGFloat = 0, fill: Color = Palette.s1) -> some View { modifier(VaultCard(padding: padding, fill: fill)) }91    /// Identifier for a container: makes the container its own accessibility element that keeps its children.92    /// (A plain `accessibilityIdentifier` on a container is pushed down to every child and hides their own identifiers.)93    func containerID(_ id: String) -> some View { accessibilityElement(children: .contain).accessibilityIdentifier(id) }94    /// Page background of the app (ink / paper).95    func vaultBackground() -> some View { background(Palette.bg.ignoresSafeArea()) }96}9798struct Eyebrow: View {99    let text: String100    init(_ text: String) { self.text = text }101    var body: some View {102        Text(text.uppercased()).font(.eyebrow).kerning(0.9).foregroundStyle(Palette.muted).accessibilityAddTraits(.isHeader)103    }104}105106// ── buttons ──107108struct VaultButtonStyle: ButtonStyle {109    enum Kind { case primary, secondary, ghost, danger, dangerGhost }110    var kind: Kind = .secondary111    var block = false112    var compact = false113    @Environment(\.isEnabled) private var enabled114115    func makeBody(configuration: Configuration) -> some View {116        let fg: Color = switch kind {117        case .primary: Palette.accentInk118        case .secondary: Palette.text119        case .ghost: Palette.muted120        case .danger: Palette.dangerInk121        case .dangerGhost: Palette.danger122        }123        let bg: Color = switch kind {124        case .primary: Palette.accent125        case .secondary: Palette.s2126        case .ghost, .dangerGhost: .clear127        case .danger: Palette.danger128        }129        let border: Color = switch kind {130        case .secondary: Palette.lineStrong131        default: .clear132        }133        #if os(macOS)134        let v: CGFloat = compact ? 4 : 7, h: CGFloat = compact ? 9 : 14135        #else136        let v: CGFloat = compact ? 8 : 13, h: CGFloat = compact ? 12 : 18137        #endif138        return configuration.label139            .font(.body.weight(.semibold))140            .lineLimit(1)141            .foregroundStyle(fg)142            .padding(.vertical, v).padding(.horizontal, h)143            .frame(maxWidth: block ? .infinity : nil)144            .background(bg, in: RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous))145            .overlay(RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous).strokeBorder(border, lineWidth: 1))146            .opacity(enabled ? (configuration.isPressed ? 0.78 : 1) : 0.45)147            .contentShape(RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous))148            .animation(.easeOut(duration: 0.12), value: configuration.isPressed)149    }150}151152extension ButtonStyle where Self == VaultButtonStyle {153    static var vaultPrimary: VaultButtonStyle { VaultButtonStyle(kind: .primary) }154    static var vaultPrimaryBlock: VaultButtonStyle { VaultButtonStyle(kind: .primary, block: true) }155    static var vaultSecondary: VaultButtonStyle { VaultButtonStyle(kind: .secondary) }156    static var vaultSecondaryBlock: VaultButtonStyle { VaultButtonStyle(kind: .secondary, block: true) }157    static var vaultCompact: VaultButtonStyle { VaultButtonStyle(kind: .secondary, compact: true) }158    static var vaultGhost: VaultButtonStyle { VaultButtonStyle(kind: .ghost) }159    static var vaultGhostBlock: VaultButtonStyle { VaultButtonStyle(kind: .ghost, block: true) }160    static var vaultDanger: VaultButtonStyle { VaultButtonStyle(kind: .danger) }161    static var vaultDangerGhost: VaultButtonStyle { VaultButtonStyle(kind: .dangerGhost) }162}163164/// A square icon button with a real tap target and a mandatory accessibility label.165struct IconButton: View {166    let systemName: String167    let label: String168    var tint: Color = Palette.muted169    let action: () -> Void170    var body: some View {171        Button(action: action) {172            Image(systemName: systemName).font(.system(size: 15, weight: .medium)).foregroundStyle(tint)173                .frame(width: Metrics.tap, height: Metrics.tap).contentShape(Rectangle())174        }175        .buttonStyle(.plain)176        .accessibilityLabel(label)177        #if os(macOS)178        .help(label)179        #endif180    }181}182183// ── callouts & errors ──184185struct Callout: View {186    enum Kind { case info, ok, warn, danger }187    var kind: Kind = .info188    var title: String?189    var text: String190    var body: some View {191        let (icon, tint, soft, line): (String, Color, Color, Color) = switch kind {192        case .info: ("info.circle", Palette.muted, Palette.s2, Palette.line)193        case .ok: ("checkmark.shield", Palette.accent, Palette.accentSoft, Palette.accentLine)194        case .warn: ("exclamationmark.triangle", Palette.brass, Palette.brassSoft, Palette.brassLine)195        case .danger: ("exclamationmark.octagon.fill", Palette.danger, Palette.dangerSoft, Palette.dangerLine)196        }197        HStack(alignment: .firstTextBaseline, spacing: 10) {198            Image(systemName: icon).foregroundStyle(tint).accessibilityHidden(true)199            VStack(alignment: .leading, spacing: 3) {200                if let title { Text(title).font(.callout.weight(.semibold)).foregroundStyle(Palette.text) }201                Text(text).font(.callout).foregroundStyle(title == nil ? Palette.text : Palette.muted).fixedSize(horizontal: false, vertical: true)202            }203            Spacer(minLength: 0)204        }205        .padding(12)206        .background(soft, in: RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous))207        .overlay(RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous).strokeBorder(line, lineWidth: 1))208        .accessibilityElement(children: .combine)209    }210}211212/// Error text. A message that starts with "SECURITY" (a check against a malicious server failed) is shown as a213/// prominent red block that cannot be mistaken for an ordinary failure.214struct ErrorText: View {215    let text: String?216    var body: some View {217        if let text, !text.isEmpty {218            if text.uppercased().hasPrefix("SECURITY") {219                Callout(kind: .danger, title: "Security warning", text: text.replacingOccurrences(of: "SECURITY: ", with: "").capitalizedFirst)220                    .accessibilityIdentifier("security-error")221            } else {222                Label(text.capitalizedFirst, systemImage: "exclamationmark.circle").font(.callout).foregroundStyle(Palette.danger)223                    .fixedSize(horizontal: false, vertical: true).accessibilityIdentifier("error-text")224            }225        }226    }227}228229extension String {230    var capitalizedFirst: String { isEmpty ? self : prefix(1).uppercased() + dropFirst() }231}232233// ── brand ──234235struct BrandMark: View {236    var size: CGFloat = 30237    var showName = true238    var body: some View {239        HStack(spacing: size * 0.32) {240            ZStack {241                RoundedRectangle(cornerRadius: size * 0.26, style: .continuous).fill(Palette.accentSoft)242                RoundedRectangle(cornerRadius: size * 0.26, style: .continuous).strokeBorder(Palette.accent, lineWidth: max(1.5, size * 0.06))243                KeyholeShape().fill(Palette.accent).frame(width: size * 0.36, height: size * 0.52)244            }245            .frame(width: size, height: size)246            .accessibilityHidden(true)247            if showName { Text("SP Vault").font(.serifTitle(size * 0.72)).foregroundStyle(Palette.text) }248        }249        .accessibilityElement(children: .combine)250        .accessibilityLabel("SP Vault")251    }252}253254struct KeyholeShape: Shape {255    func path(in r: CGRect) -> Path {256        var p = Path()257        let d = r.width258        p.addEllipse(in: CGRect(x: r.minX, y: r.minY, width: d, height: d))259        p.move(to: CGPoint(x: r.midX - d * 0.22, y: r.minY + d * 0.7))260        p.addLine(to: CGPoint(x: r.midX + d * 0.22, y: r.minY + d * 0.7))261        p.addLine(to: CGPoint(x: r.midX + d * 0.34, y: r.maxY))262        p.addLine(to: CGPoint(x: r.midX - d * 0.34, y: r.maxY))263        p.closeSubpath()264        return p265    }266}267268/// Ink background with a faint verdigris glow at the top (signed-out and locked screens).269struct AuthBackdrop: View {270    var body: some View {271        ZStack {272            Palette.bg273            RadialGradient(colors: [Palette.accent.opacity(0.13), .clear], center: .top, startRadius: 0, endRadius: 520)274        }275        .ignoresSafeArea()276        .accessibilityHidden(true)277    }278}279280// ── item glyph ──281282/// The square initial that stands for an item (no remote favicon is ever fetched: that would tell a third party283/// which sites are in the vault).284struct ItemGlyph: View {285    let title: String286    var symbol: String?287    var size: CGFloat = 36288289    private static let tints: [Color] = [290        Color(light: 0x7a3fb8, dark: 0xc084fc), Color(light: 0x0e7c61, dark: 0x3fd0a8), Color(light: 0x855b0b, dark: 0xd9b46c),291        Color(light: 0x1559a6, dark: 0x7cc4ff), Color(light: 0xb03a6a, dark: 0xf78fb3), Color(light: 0x9a4a12, dark: 0xf2a65a),292    ]293    private var tint: Color {294        let h = title.unicodeScalars.reduce(UInt32(7)) { ($0 &* 31) &+ $1.value }295        return Self.tints[Int(h % UInt32(Self.tints.count))]296    }297    var body: some View {298        ZStack {299            RoundedRectangle(cornerRadius: size * 0.26, style: .continuous).fill(tint.opacity(0.16))300            RoundedRectangle(cornerRadius: size * 0.26, style: .continuous).strokeBorder(tint.opacity(0.4), lineWidth: 1)301            if let symbol {302                Image(systemName: symbol).font(.system(size: size * 0.42, weight: .medium)).foregroundStyle(tint)303            } else {304                Text(String(title.trimmingCharacters(in: .whitespaces).prefix(1)).uppercased().isEmpty ? "·" : String(title.trimmingCharacters(in: .whitespaces).prefix(1)).uppercased())305                    .font(.system(size: size * 0.5, weight: .semibold, design: .serif)).foregroundStyle(tint)306            }307        }308        .frame(width: size, height: size)309        .accessibilityHidden(true)310    }311}312313// ── secrets ──314315/// A revealed secret in monospace, digits and symbols tinted so that 0/O and 1/l can be told apart.316struct ColoredSecret: View {317    let text: String318    var font: Font = .secret319    var body: some View {320        text.reduce(Text("")) { acc, ch in321            let color: Color = ch.isNumber ? Palette.digit : (ch.isLetter || ch.isWhitespace ? Palette.text : Palette.symbol)322            return acc + Text(String(ch)).foregroundColor(color)323        }324        .font(font)325        .textSelection(.enabled)326    }327}328329struct Pill: View {330    let text: String331    var tint: Color = Palette.muted332    var body: some View {333        Text(text).font(.caption.weight(.medium)).foregroundStyle(tint)334            .padding(.horizontal, 8).padding(.vertical, 2)335            .overlay(Capsule().strokeBorder(tint.opacity(0.45), lineWidth: 1))336    }337}338
Files main
Menu

Theme