SPB Git

Repository Public

Theme.swift 6.0 KB 118 lines swift

1// SP Wallet iOS2// File: Components/Theme.swift3// Description: Design tokens of SP Wallet on Apple platforms — the same palette, radii and type roles as the web design system4//              (docs/v9/DESIGN.md of the backend repository), expressed with system fonts and dynamic colours. Colour never5//              carries a meaning alone: every state also has an icon and a word.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI10import UIKit1112extension Color {13    init(light: UInt32, dark: UInt32) {14        self.init(uiColor: UIColor { $0.userInterfaceStyle == .dark ? UIColor(hex: dark) : UIColor(hex: light) })15    }16}17extension UIColor {18    convenience init(hex: UInt32) {19        self.init(red: CGFloat((hex >> 16) & 0xff) / 255, green: CGFloat((hex >> 8) & 0xff) / 255, blue: CGFloat(hex & 0xff) / 255, alpha: 1)20    }21}2223enum Theme {24    static let bg = Color(light: 0xf3f4f7, dark: 0x06080c)25    static let surface = Color(light: 0xffffff, dark: 0x0f131b)26    static let elev = Color(light: 0xeceef3, dark: 0x1a202c)27    static let ink = Color(light: 0x0a0d14, dark: 0xf4f6fa)28    static let mute = Color(light: 0x596273, dark: 0x98a2b6)29    static let hairline = Color(light: 0xe2e5ec, dark: 0x232a38)30    static let emerald = Color(light: 0x087a55, dark: 0x35e7ac)31    static let blue = Color(light: 0x2257e0, dark: 0x7ea6ff)32    static let warn = Color(light: 0x9a5b06, dark: 0xf4b65b)33    static let danger = Color(light: 0xc22a40, dark: 0xff7d88)34    static let primary = Color(light: 0x0a0d14, dark: 0x35e7ac)35    static let onPrimary = Color(light: 0xffffff, dark: 0x04130d)36    static let onDanger = Color(light: 0xffffff, dark: 0x2a0508)37    /** The hero card keeps one dark base in both appearances (white text at 9.6:1). */38    static let heroBase = Color(light: 0x0b1020, dark: 0x0d1424)39    /** Data-viz series: five fixed slots + "other", never cycled. */40    static let series: [Color] = [Color(light: 0x2257e0, dark: 0x7ea6ff), Color(light: 0xd9730d, dark: 0xffa54a), Color(light: 0x0e9aa7, dark: 0x4fd6e0),41                                  Color(light: 0xb89300, dark: 0xf2d04b), Color(light: 0xb5379a, dark: 0xf07fd6)]42    static let seriesOther = Color(light: 0x98a2b6, dark: 0x596273)4344    enum Radius { static let small: CGFloat = 12; static let control: CGFloat = 16; static let card: CGFloat = 22; static let hero: CGFloat = 28 }45    enum Space { static let gutter: CGFloat = 20; static let card: CGFloat = 18; static let section: CGFloat = 18 }46}4748extension Font {49    /** Hero figures: proportional digits, tight tracking. */50    static let spDisplay = Font.system(size: 44, weight: .semibold, design: .default)51    static let spTitle = Font.system(size: 28, weight: .semibold)52    static let spHeadline = Font.system(size: 17, weight: .semibold)53    static let spBody = Font.system(size: 15.5)54    static let spCallout = Font.system(size: 14)55    static let spFootnote = Font.system(size: 13)56    static let spEyebrow = Font.system(size: 11.5, weight: .semibold)57    /** Addresses, hashes, identifiers: always monospaced, never truncated on an approval. */58    static let spMono = Font.system(size: 13.5, design: .monospaced)59}6061extension View {62    /** A resting card of the design system. */63    func spCard(padding: CGFloat = Theme.Space.card) -> some View {64        self.padding(padding)65            .frame(maxWidth: .infinity, alignment: .leading)66            .background(Theme.surface, in: RoundedRectangle(cornerRadius: Theme.Radius.card, style: .continuous))67            .overlay(RoundedRectangle(cornerRadius: Theme.Radius.card, style: .continuous).strokeBorder(Theme.hairline, lineWidth: 0.5))68    }69    func spEyebrow() -> some View { self.font(.spEyebrow).tracking(0.7).textCase(.uppercase).foregroundStyle(Theme.mute) }70    /** Page background + standard scroll padding. */71    func spPage() -> some View { self.background(Theme.bg.ignoresSafeArea()) }72}7374/** The primary action: full width, 52 pt, one per screen. */75struct PrimaryButtonStyle: ButtonStyle {76    var destructive = false77    @Environment(\.isEnabled) private var enabled78    func makeBody(configuration: Configuration) -> some View {79        configuration.label80            .font(.system(size: 16.5, weight: .semibold))81            .frame(maxWidth: .infinity, minHeight: 52)82            .foregroundStyle(destructive ? Theme.onDanger : Theme.onPrimary)83            .background(destructive ? Theme.danger : Theme.primary, in: RoundedRectangle(cornerRadius: Theme.Radius.control, style: .continuous))84            .opacity(enabled ? (configuration.isPressed ? 0.85 : 1) : 0.4)85            .scaleEffect(configuration.isPressed ? 0.98 : 1)86            .animation(.easeOut(duration: 0.14), value: configuration.isPressed)87    }88}8990struct SecondaryButtonStyle: ButtonStyle {91    @Environment(\.isEnabled) private var enabled92    func makeBody(configuration: Configuration) -> some View {93        configuration.label94            .font(.system(size: 16, weight: .semibold))95            .frame(maxWidth: .infinity, minHeight: 50)96            .foregroundStyle(Theme.ink)97            .background(Theme.elev, in: RoundedRectangle(cornerRadius: Theme.Radius.control, style: .continuous))98            .opacity(enabled ? (configuration.isPressed ? 0.8 : 1) : 0.4)99    }100}101102extension ButtonStyle where Self == PrimaryButtonStyle {103    static var spPrimary: PrimaryButtonStyle { PrimaryButtonStyle() }104    static var spDestructive: PrimaryButtonStyle { PrimaryButtonStyle(destructive: true) }105}106extension ButtonStyle where Self == SecondaryButtonStyle { static var spSecondary: SecondaryButtonStyle { SecondaryButtonStyle() } }107108extension View {109    /**110     Give a presented content the app's whole environment explicitly. Sheets, covers, popovers, navigation destinations, table cells and111     context menus do not reliably inherit `@Environment` objects: every one of them must end with `.wallet(model)`112     (a missing object is a crash, not a blank screen).113     */114    func wallet(_ model: AppModel) -> some View {115        self.environment(model).environment(model.router).environment(model.env.preferences)116    }117}118
Files main
Menu

Theme