SPB Git

Repository Public

Theme.swift 13.4 KB 221 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/Theme.swift3// Description: SP Drive design system v2 — the one token set shared with the web (CSS variables) and iOS: colour roles (light, dark, increased contrast), an editorial type scale, spacing, radii, elevation, and a motion vocabulary that honours Reduce Motion. The table in docs/next/DESIGN-SYSTEM-V2.md is the contract; names here match it.4import AppKit5import SwiftUI67enum SPDTheme {8    // MARK: Colour roles9    /// A colour with its four variants. Hex values are sRGB; `a` is the alpha.10    struct Role: Sendable {11        let light: UInt32, dark: UInt32, lightHC: UInt32, darkHC: UInt3212        let la: Double, da: Double13        init(_ light: UInt32, _ dark: UInt32, hc lightHC: UInt32? = nil, _ darkHC: UInt32? = nil, alpha la: Double = 1, _ da: Double? = nil) {14            self.light = light; self.dark = dark; self.lightHC = lightHC ?? light; self.darkHC = darkHC ?? dark; self.la = la; self.da = da ?? la15        }16        var nsColor: NSColor {17            NSColor(name: nil) { appearance in18                let match = appearance.bestMatch(from: [.aqua, .darkAqua, .accessibilityHighContrastAqua, .accessibilityHighContrastDarkAqua]) ?? .aqua19                switch match {20                case .darkAqua: return Role.ns(dark, da)21                case .accessibilityHighContrastAqua: return Role.ns(lightHC, min(1, la * 1.6))22                case .accessibilityHighContrastDarkAqua: return Role.ns(darkHC, min(1, da * 1.6))23                default: return Role.ns(light, la)24                }25            }26        }27        var color: SwiftUI.Color { SwiftUI.Color(nsColor: nsColor) }28        static func ns(_ hex: UInt32, _ a: Double) -> NSColor {29            NSColor(srgbRed: CGFloat((hex >> 16) & 0xFF) / 255, green: CGFloat((hex >> 8) & 0xFF) / 255, blue: CGFloat(hex & 0xFF) / 255, alpha: a)30        }31    }32    enum Palette {33        /// Identity. Violet is the accent of every control; indigo is its companion in the mark only.34        static let brand = Role(0x6D3DF2, 0x9882FF, hc: 0x4F22C9, 0xB7A8FF)35        static let brandIndigo = Role(0x4A55E0, 0x8E97FF, hc: 0x2F39B8, 0xAEB5FF)36        /// Quiet wash of the brand: selection, hovered rows, valid drop targets.37        static let brandWash = Role(0x6D3DF2, 0x9882FF, hc: 0x4F22C9, 0xB7A8FF, alpha: 0.10, 0.18)38        static let selection = Role(0x6D3DF2, 0x9882FF, hc: 0x4F22C9, 0xB7A8FF, alpha: 0.16, 0.30)39        // Surfaces, from the back of the window forward.40        static let canvas = Role(0xFFFFFF, 0x1B1B1F, hc: 0xFFFFFF, 0x000000)41        static let surface = Role(0xF6F6F9, 0x232328, hc: 0xF2F2F6, 0x101013)42        static let surfaceRaised = Role(0xFFFFFF, 0x2B2B31, hc: 0xFFFFFF, 0x1A1A1E)43        static let sunken = Role(0xEEEEF3, 0x161619, hc: 0xE6E6EC, 0x000000)44        static let hairline = Role(0x000000, 0xFFFFFF, alpha: 0.10, 0.12)45        // Text.46        static let textPrimary = Role(0x16161A, 0xF2F2F5, hc: 0x000000, 0xFFFFFF)47        static let textSecondary = Role(0x5C5C66, 0xA6A6B0, hc: 0x33333A, 0xD4D4DC)48        static let textTertiary = Role(0x8A8A94, 0x7C7C86, hc: 0x55555E, 0xB0B0BA)49        static let onBrand = Role(0xFFFFFF, 0x14102B, hc: 0xFFFFFF, 0x000000)50        // Status. Never the only carrier of meaning: always paired with a symbol or a word.51        static let positive = Role(0x1E8E5A, 0x4CC38A, hc: 0x116B41, 0x7BE0AE)52        static let warning = Role(0xB76E00, 0xF0B454, hc: 0x8A5200, 0xFFD084)53        static let critical = Role(0xC8322B, 0xFF6B63, hc: 0x9E1C16, 0xFF9A94)54    }55    static let brand = Palette.brand.color56    static let brandIndigo = Palette.brandIndigo.color57    static let brandWash = Palette.brandWash.color58    static let selection = Palette.selection.color59    static let canvas = Palette.canvas.color60    static let surface = Palette.surface.color61    static let surfaceRaised = Palette.surfaceRaised.color62    static let sunken = Palette.sunken.color63    static let hairline = Palette.hairline.color64    static let textPrimary = Palette.textPrimary.color65    static let textSecondary = Palette.textSecondary.color66    static let textTertiary = Palette.textTertiary.color67    static let onBrand = Palette.onBrand.color68    static let positive = Palette.positive.color69    static let warning = Palette.warning.color70    static let critical = Palette.critical.color7172    // MARK: Type scale (editorial: a serif for titles that name a place or a moment, the system sans for everything you operate)73    enum Typo {74        /// Empty states, sign-in, sheet heroes.75        static let display = Font.system(size: 28, weight: .semibold, design: .serif)76        /// Sheet and section titles.77        static let title = Font.system(size: 20, weight: .semibold, design: .serif)78        /// Card titles, inspector name.79        static let headline = Font.system(size: 15, weight: .semibold)80        static let body = Font.system(size: 13)81        static let bodyStrong = Font.system(size: 13, weight: .medium)82        static let callout = Font.system(size: 12)83        static let caption = Font.system(size: 11)84        /// Small capitals above groups ("SUR CE MAC"): 10 pt medium, tracked +0.6.85        static let eyebrow = Font.system(size: 10, weight: .semibold)86        static let eyebrowTracking: CGFloat = 0.687        static let mono = Font.system(size: 11, design: .monospaced)88        /// Large figures of dashboards (storage, speeds): tabular digits.89        static let figure = Font.system(size: 22, weight: .semibold, design: .rounded).monospacedDigit()90    }9192    // MARK: Spacing (4-pt grid), radii, sizes93    enum Space { static let xxs: CGFloat = 2, xs: CGFloat = 4, sm: CGFloat = 8, md: CGFloat = 12, lg: CGFloat = 16, xl: CGFloat = 24, xxl: CGFloat = 32, xxxl: CGFloat = 48 }94    enum Radius { static let xs: CGFloat = 4, sm: CGFloat = 6, md: CGFloat = 10, lg: CGFloat = 14, xl: CGFloat = 20 }95    enum Size { static let gridCell: CGFloat = 148, gridThumb: CGFloat = 116, rowIcon: CGFloat = 20, palette: CGFloat = 620 }9697    // MARK: Elevation98    struct Elevation: Sendable { let radius: CGFloat, y: CGFloat, opacity: Double99        /// Flat: a hairline only.100        static let e0 = Elevation(radius: 0, y: 0, opacity: 0)101        /// Cards resting on the canvas.102        static let e1 = Elevation(radius: 3, y: 1, opacity: 0.07)103        /// Floating panels: command palette, drop zone, popovers.104        static let e2 = Elevation(radius: 24, y: 8, opacity: 0.16)105        /// Things in the hand: dragged items, the Quick Look lift.106        static let e3 = Elevation(radius: 40, y: 16, opacity: 0.24)107    }108109    // MARK: Motion110    /// A named motion: what plays normally, and what plays when the person asked for reduced motion (a short fade, or nothing).111    struct Motion: Sendable {112        let name: String113        let duration: Double114        let full: Animation115        let reduced: Animation?116        func resolved(reduceMotion: Bool) -> Animation? { reduceMotion ? reduced : full }117        /// For imperative code outside a view (AppKit panels): reads the system setting directly.118        var current: Animation? { resolved(reduceMotion: NSWorkspace.shared.accessibilityDisplayShouldReduceMotion) }119120        enum Duration { static let instant = 0.10, quick = 0.18, standard = 0.28, slow = 0.45 }121        enum Curve {122            /// Things that move from one resting place to another.123            static func standard(_ d: Double) -> Animation { .timingCurve(0.2, 0, 0, 1, duration: d) }124            /// Things arriving: fast start, long settle.125            static func enter(_ d: Double) -> Animation { .timingCurve(0.05, 0.7, 0.1, 1, duration: d) }126            /// Things leaving: no settle.127            static func exit(_ d: Double) -> Animation { .timingCurve(0.3, 0, 0.8, 0.15, duration: d) }128            static let springSnappy = Animation.spring(response: 0.28, dampingFraction: 0.86)129            static let springGentle = Animation.spring(response: 0.45, dampingFraction: 0.82)130        }131        private static let fade = Animation.easeOut(duration: Duration.instant)132        /// Selection and pressed states.133        static let select = Motion(name: "select", duration: Duration.instant, full: Curve.standard(Duration.instant), reduced: nil)134        /// Hover washes, badges appearing.135        static let hover = Motion(name: "hover", duration: Duration.quick, full: Curve.standard(Duration.quick), reduced: nil)136        /// Going into or out of a folder, changing section.137        static let navigate = Motion(name: "navigate", duration: Duration.standard, full: Curve.enter(Duration.standard), reduced: fade)138        /// Panels that slide or grow into view: inspector, command palette, banners.139        static let reveal = Motion(name: "reveal", duration: Duration.standard, full: Curve.springGentle, reduced: fade)140        static let dismiss = Motion(name: "dismiss", duration: Duration.quick, full: Curve.exit(Duration.quick), reduced: fade)141        /// A valid drop target answering the drag.142        static let drop = Motion(name: "drop", duration: Duration.standard, full: Curve.springSnappy, reduced: nil)143        /// Progress bars and figures that follow live data.144        static let progress = Motion(name: "progress", duration: 0.20, full: .linear(duration: 0.20), reduced: .linear(duration: 0.20))145        /// Slow, ambient: the breathing of the sync mark. Removed entirely under Reduce Motion.146        static let ambient = Motion(name: "ambient", duration: Duration.slow * 3, full: .easeInOut(duration: Duration.slow * 3).repeatForever(autoreverses: true), reduced: nil)147        static let all = [select, hover, navigate, reveal, dismiss, drop, progress, ambient]148    }149}150151// MARK: Modifiers152private struct MotionModifier<V: Equatable>: ViewModifier {153    @Environment(\.accessibilityReduceMotion) private var reduce154    let motion: SPDTheme.Motion155    let value: V156    func body(content: Content) -> some View { content.animation(motion.resolved(reduceMotion: reduce), value: value) }157}158private struct ElevationModifier: ViewModifier {159    @Environment(\.colorScheme) private var scheme160    let e: SPDTheme.Elevation161    func body(content: Content) -> some View {162        content.shadow(color: .black.opacity(e.opacity * (scheme == .dark ? 1.7 : 1)), radius: e.radius, y: e.y)163    }164}165extension View {166    /// Animates changes of `value` with a named motion, or its reduced form when Reduce Motion is on.167    func spdMotion<V: Equatable>(_ motion: SPDTheme.Motion, value: V) -> some View { modifier(MotionModifier(motion: motion, value: value)) }168    func spdElevation(_ e: SPDTheme.Elevation) -> some View { modifier(ElevationModifier(e: e)) }169    /// A calm card: raised surface, hairline, resting elevation.170    func spdCard(padding: CGFloat = SPDTheme.Space.lg, radius: CGFloat = SPDTheme.Radius.lg) -> some View {171        self.padding(padding)172            .background(SPDTheme.surfaceRaised, in: RoundedRectangle(cornerRadius: radius, style: .continuous))173            .overlay(RoundedRectangle(cornerRadius: radius, style: .continuous).strokeBorder(SPDTheme.hairline))174            .spdElevation(.e1)175    }176    func spdEyebrow() -> some View { self.font(SPDTheme.Typo.eyebrow).tracking(SPDTheme.Typo.eyebrowTracking).textCase(.uppercase).foregroundStyle(SPDTheme.textTertiary) }177}178/// `withAnimation` with a named motion.179@MainActor func spdAnimate(_ motion: SPDTheme.Motion, _ body: () -> Void) {180    if let a = motion.current { withAnimation(a, body) } else { body() }181}182183/// A small status pill: a symbol and a word, tinted (never colour alone).184struct SPDPill: View {185    let text: String186    var symbol: String?187    var tint: Color = SPDTheme.textSecondary188    var body: some View {189        HStack(spacing: 3) {190            if let symbol { Image(systemName: symbol).font(.system(size: 9, weight: .semibold)) }191            Text(text).font(SPDTheme.Typo.caption.weight(.medium))192        }193        .padding(.horizontal, 7).padding(.vertical, 2)194        .foregroundStyle(tint).background(tint.opacity(0.13), in: Capsule())195    }196}197198/// An empty state that says where you are, why it is empty, and what to do next.199struct SPDEmptyState<Actions: View>: View {200    let symbol: String201    let title: String202    let message: String203    /// Width of the text column (a fixed width: a text that sizes itself from its container inside the split view's detail made AppKit loop on constraints).204    var width: CGFloat = 420205    @ViewBuilder var actions: () -> Actions206    var body: some View {207        VStack(spacing: SPDTheme.Space.md) {208            Image(systemName: symbol).font(.system(size: 34, weight: .light)).foregroundStyle(SPDTheme.brand).symbolRenderingMode(.hierarchical)209                .frame(width: 72, height: 72).background(SPDTheme.brandWash, in: Circle()).accessibilityHidden(true)210            Text(title).font(width < 300 ? SPDTheme.Typo.title : SPDTheme.Typo.display).foregroundStyle(SPDTheme.textPrimary).multilineTextAlignment(.center).lineLimit(2).frame(width: width)211            Text(message).font(SPDTheme.Typo.body).foregroundStyle(SPDTheme.textSecondary).multilineTextAlignment(.center).lineLimit(6).frame(width: width)212            HStack(spacing: SPDTheme.Space.sm) { actions() }.padding(.top, SPDTheme.Space.xs)213        }214        .padding(width < 300 ? SPDTheme.Space.md : SPDTheme.Space.xxl).frame(maxWidth: .infinity, maxHeight: .infinity)215        .accessibilityElement(children: .contain)216    }217}218extension SPDEmptyState where Actions == EmptyView {219    init(symbol: String, title: String, message: String, width: CGFloat = 420) { self.init(symbol: symbol, title: title, message: message, width: width) { EmptyView() } }220}221
Files main
Menu

Theme