SPB Git

Repository Public

SPColor.swift 7.7 KB 151 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Design/SPColor.swift3// Description: SP Mail Premium colour tokens (light / dark / midnight) as environment-resolved shape styles, the theme preference and the modifier that applies it to a scene.4import SwiftUI56// MARK: - theme78/// The theme preference of this device. Midnight is an app-level theme layered over the system dark appearance.9enum SPTheme: String, CaseIterable, Identifiable, Sendable {10    case system, light, dark, midnight11    var id: String { rawValue }12    var colorScheme: ColorScheme? { self == .system ? nil : self == .light ? .light : .dark }13    var title: String {14        switch self { case .system: L("Automatique"); case .light: L("Clair"); case .dark: L("Sombre"); case .midnight: L("Minuit") }15    }16}1718enum SPVariant: Sendable { case light, dark, midnight }1920extension EnvironmentValues {21    /// True when the midnight theme is active (the colour scheme is then dark).22    @Entry var spMidnight = false23    /// The user asked for opaque chrome in the app (in addition to the system setting).24    @Entry var spOpaqueChrome = false2526    var spVariant: SPVariant { colorScheme == .light ? .light : spMidnight ? .midnight : .dark }27    var spReduceTransparency: Bool { spOpaqueChrome || accessibilityReduceTransparency }28}2930// MARK: - tokens3132/// One colour of the design system: three values, resolved in the environment (theme, Increase Contrast).33struct SPToken: ShapeStyle, Sendable, Hashable {34    struct Value: Sendable, Hashable {35        let rgb: UInt3236        let alpha: Double37        init(_ rgb: UInt32, _ alpha: Double = 1) { self.rgb = rgb; self.alpha = alpha }38        var color: Color {39            Color(.sRGB, red: Double((rgb >> 16) & 0xff) / 255, green: Double((rgb >> 8) & 0xff) / 255, blue: Double(rgb & 0xff) / 255, opacity: alpha)40        }41    }42    let light: Value, dark: Value, midnight: Value43    /// The values used when the system asks for increased contrast (same shape, stronger token).44    var contrast: [Value]?4546    init(_ light: UInt32, _ dark: UInt32, _ midnight: UInt32) { self.light = Value(light); self.dark = Value(dark); self.midnight = Value(midnight) }47    init(_ light: Value, _ dark: Value, _ midnight: Value) { self.light = light; self.dark = dark; self.midnight = midnight }48    func strengthened(by other: SPToken) -> SPToken { var t = self; t.contrast = [other.light, other.dark, other.midnight]; return t }4950    func value(_ variant: SPVariant, increasedContrast: Bool = false) -> Value {51        if increasedContrast, let c = contrast { return variant == .light ? c[0] : variant == .dark ? c[1] : c[2] }52        return variant == .light ? light : variant == .dark ? dark : midnight53    }54    func color(_ variant: SPVariant, increasedContrast: Bool = false) -> Color { value(variant, increasedContrast: increasedContrast).color }55    func resolve(in environment: EnvironmentValues) -> Color {56        color(environment.spVariant, increasedContrast: environment.colorSchemeContrast == .increased)57    }58}5960/// Token names are shared with the web client (`web/src/styles/tokens.css`); see docs/next/DESIGN-SYSTEM-NEXT.md §2.61enum SPColor {62    static let bg = SPToken(0xf6f5f1, 0x141513, 0x000000)63    static let surface = SPToken(0xffffff, 0x1c1d1b, 0x0b0c0b)64    static let surface2 = SPToken(0xf0eee8, 0x242523, 0x131412)65    static let surface3 = SPToken(0xe6e3db, 0x2e2f2c, 0x1c1d1b)66    static let borderStrong = SPToken(0xc4c0b4, 0x4a4b47, 0x3a3b38)67    static let border = SPToken(0xe2dfd6, 0x2f302d, 0x1f201e).strengthened(by: borderStrong)68    static let text = SPToken(0x1c1b19, 0xeceae4, 0xe6e4de)69    static let text2 = SPToken(0x4d4a44, 0xb9b6ae, 0xa9a69e).strengthened(by: text)70    static let text3 = SPToken(0x66625b, 0x98958d, 0x8f8c84).strengthened(by: SPToken(0x4d4a44, 0xb9b6ae, 0xa9a69e))71    static let accentStrong = SPToken(0x1f4439, 0x9ad6c2, 0xa2dcc9)72    static let accent = SPToken(0x2c5b4d, 0x7cc4ae, 0x86cdb7).strengthened(by: accentStrong)73    static let accentSoft = SPToken(0xe0ece7, 0x1f332d, 0x12231e)74    static let onAccent = SPToken(0xffffff, 0x0c1f19, 0x06140f)75    static let danger = SPToken(0xa8231c, 0xff8a80, 0xff8a80)76    static let dangerSoft = SPToken(0xfbe8e6, 0x3a1b18, 0x2a100e)77    static let warn = SPToken(0x7a4d00, 0xf0c36a, 0xf0c36a)78    static let warnSoft = SPToken(0xfcefd2, 0x33290f, 0x241c08)79    static let ok = SPToken(0x1b6236, 0x7fd39a, 0x7fd39a)80    static let okSoft = SPToken(0xe0f2e6, 0x16301f, 0x0e2114)81    static let info = SPToken(0x1d4f91, 0x8fb8f0, 0x8fb8f0)82    static let infoSoft = SPToken(0xe6eef9, 0x172941, 0x0f1c2e)83    static let star = SPToken(0xb07a00, 0xe6b84d, 0xe6b84d)84    static let focus = SPToken(0x1a6dd6, 0x6aa9ff, 0x6aa9ff)85    static let glass = SPToken(.init(0xf6f5f1, 0.72), .init(0x1c1d1b, 0.68), .init(0x080908, 0.72))86    static let overlay = SPToken(.init(0x141311, 0.42), .init(0x000000, 0.55), .init(0x000000, 0.66))87    /// Shadow tint of the elevation levels (light theme only; dark themes use a hairline instead).88    static let shadow = SPToken(.init(0x141311, 1), .init(0x000000, 0), .init(0x000000, 0))89}9091// MARK: - applying the theme9293struct SPThemeModifier: ViewModifier {94    let settings: AppSettings95    func body(content: Content) -> some View {96        content97            .environment(\.spMidnight, settings.theme == .midnight)98            .environment(\.spOpaqueChrome, settings.reduceTransparency)99            .tint(SPColor.accent)100            .preferredColorScheme(settings.theme.colorScheme)101    }102}103104extension View {105    /// Applies the theme preference (colour scheme, midnight layer, accent) to the root of a scene, a sheet or a popover.106    func spTheme(_ settings: AppSettings) -> some View { modifier(SPThemeModifier(settings: settings)) }107}108109// MARK: - surfaces110111/// Translucent chrome that floats above content (palette, toasts, popovers). Opaque `surface-2` when transparency is reduced.112struct SPGlassModifier<S: Shape>: ViewModifier {113    let shape: S114    let level: Int115    @Environment(\.spReduceTransparency) private var opaque116    @Environment(\.colorScheme) private var scheme117    func body(content: Content) -> some View {118        content119            .background { if opaque { shape.fill(SPColor.surface2) } else { shape.fill(.regularMaterial).overlay(shape.fill(SPColor.glass).opacity(0.35)) } }120            .overlay(shape.stroke(SPColor.border, lineWidth: scheme == .dark ? 1 : 0.5))121            .spElevation(level)122    }123}124125struct SPElevationModifier: ViewModifier {126    let level: Int127    @Environment(\.colorScheme) private var scheme128    func body(content: Content) -> some View {129        // dark themes replace shadows by a hairline and a lighter surface (drawn by the caller)130        if scheme == .dark || level <= 0 { content } else {131            let s: (opacity: Double, radius: CGFloat, y: CGFloat) = level == 1 ? (0.06, 1, 1) : level == 2 ? (0.14, 14, 8) : (0.22, 32, 24)132            content.shadow(color: Color(.sRGB, red: 20 / 255, green: 19 / 255, blue: 17 / 255, opacity: s.opacity), radius: s.radius, y: s.y)133        }134    }135}136137extension View {138    func spGlass<S: Shape>(_ shape: S, level: Int = 2) -> some View { modifier(SPGlassModifier(shape: shape, level: level)) }139    func spElevation(_ level: Int) -> some View { modifier(SPElevationModifier(level: level)) }140    /// A level-1 card: `surface` fill, hairline, radius 10.141    func spCard(radius: CGFloat = SPRadius.card, fill: SPToken = SPColor.surface) -> some View {142        let shape = RoundedRectangle(cornerRadius: radius, style: .continuous)143        return background(shape.fill(fill)).overlay(shape.stroke(SPColor.border, lineWidth: 0.5)).spElevation(1)144    }145}146147/// Like `Color`, a token can be used directly as a view (a filled rectangle).148extension SPToken: View {149    var body: some View { Rectangle().fill(self) }150}151
Files main
Menu

Theme