SPB Git

Repository Public

Theme.swift 13.3 KB 244 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/Theme.swift3// Description: The visual language of SP Wallet: calm surfaces, one accent, and semantic colours that mean the same thing everywhere (verified, estimate, warning, danger). No decorative animation: nothing moves to distract from a fee or a risk.4import SwiftUI56enum Theme {7    static let accent = Color("AccentColor")8    static let positive = Color(red: 0.07, green: 0.62, blue: 0.40)9    static let negative = Color(red: 0.86, green: 0.24, blue: 0.24)10    static let warning = Color(red: 0.90, green: 0.56, blue: 0.04)11    static let estimate = Color.secondary12    static let card = Color(nsColor: .controlBackgroundColor)13    static let stroke = Color(nsColor: .separatorColor)14    static let corner: CGFloat = 1615    static let mono = Font.system(.body, design: .monospaced)16    static let monoSmall = Font.system(.callout, design: .monospaced)1718    /// The brand's deep indigo, as on the app icon: the hero surfaces of the app.19    static let ink = Color(red: 0.043, green: 0.067, blue: 0.188)20    static let indigo = Color(red: 0.106, green: 0.165, blue: 0.420)21    static let azure = Color(red: 0.227, green: 0.388, blue: 0.902)22    static let sky = Color(red: 0.498, green: 0.635, blue: 1.0)23    static let hero = LinearGradient(colors: [Color(red: 0.13, green: 0.21, blue: 0.56), indigo, ink], startPoint: .topLeading, endPoint: .bottomTrailing)24    static let accentGradient = LinearGradient(colors: [sky, azure], startPoint: .topLeading, endPoint: .bottomTrailing)2526    /// Categorical palette for allocations and series: distinct in hue AND lightness, readable in light and dark appearance.27    static let palette: [Color] = [28        Color(red: 0.25, green: 0.41, blue: 0.93), Color(red: 0.06, green: 0.64, blue: 0.60), Color(red: 0.95, green: 0.62, blue: 0.11),29        Color(red: 0.86, green: 0.31, blue: 0.47), Color(red: 0.55, green: 0.38, blue: 0.90), Color(red: 0.20, green: 0.66, blue: 0.89),30        Color(red: 0.47, green: 0.70, blue: 0.24), Color(red: 0.55, green: 0.59, blue: 0.67),31    ]32    static func series(_ i: Int) -> Color { palette[i % palette.count] }3334    static func change(_ v: Double?) -> Color { guard let v else { return .secondary }; return v > 0 ? positive : v < 0 ? negative : .secondary }35    /// Hero figures: rounded, heavy, tabular digits.36    static func figure(_ size: CGFloat) -> Font { .system(size: size, weight: .bold, design: .rounded) }37}3839/// The window's backdrop: the system background with a very light brand tint at the top, so cards read as raised surfaces.40struct Backdrop: View {41    @Environment(\.colorScheme) private var scheme42    var body: some View {43        ZStack {44            Color(nsColor: .windowBackgroundColor)45            LinearGradient(colors: [Theme.azure.opacity(scheme == .dark ? 0.16 : 0.07), .clear], startPoint: .top, endPoint: .center)46        }.ignoresSafeArea()47    }48}4950struct Card<Content: View>: View {51    var title: String?52    var trailing: AnyView?53    @ViewBuilder var content: Content54    @Environment(\.colorScheme) private var scheme55    init(_ title: String? = nil, trailing: AnyView? = nil, @ViewBuilder content: () -> Content) { self.title = title; self.trailing = trailing; self.content = content() }56    var body: some View {57        VStack(alignment: .leading, spacing: 12) {58            if title != nil || trailing != nil {59                HStack(alignment: .firstTextBaseline) {60                    if let title { Text(title).font(.system(.headline, design: .rounded).weight(.semibold)) }61                    Spacer()62                    if let trailing { trailing }63                }64            }65            content66        }67        .padding(18)68        .frame(maxWidth: .infinity, alignment: .leading)69        .background(Theme.card.opacity(scheme == .dark ? 0.72 : 1), in: RoundedRectangle(cornerRadius: Theme.corner, style: .continuous))70        .overlay(RoundedRectangle(cornerRadius: Theme.corner, style: .continuous).strokeBorder(Theme.stroke.opacity(scheme == .dark ? 0.55 : 0.35)))71        .shadow(color: .black.opacity(scheme == .dark ? 0.28 : 0.06), radius: 14, x: 0, y: 5)72    }73}7475/// The dark, branded surface used for the most important figure of a screen (total value, an account's equity…).76struct HeroCard<Content: View>: View {77    @ViewBuilder var content: Content78    var body: some View {79        content80            .padding(22).frame(maxWidth: .infinity, alignment: .leading)81            .foregroundStyle(.white)82            .background {83                ZStack {84                    Theme.hero85                    // two soft light sources, as on the icon; static — nothing here moves86                    Circle().fill(Theme.sky.opacity(0.30)).frame(width: 380, height: 380).blur(radius: 90).offset(x: 330, y: -150)87                    Circle().fill(Theme.azure.opacity(0.35)).frame(width: 300, height: 300).blur(radius: 80).offset(x: -260, y: 170)88                }89            }90            .clipShape(RoundedRectangle(cornerRadius: 22, style: .continuous))91            .overlay(RoundedRectangle(cornerRadius: 22, style: .continuous).strokeBorder(.white.opacity(0.12)))92            .shadow(color: Theme.indigo.opacity(0.35), radius: 22, x: 0, y: 10)93            .environment(\.colorScheme, .dark)94    }95}9697/// A small labelled figure with an icon tile: the dashboard's secondary facts.98struct StatTile: View {99    let symbol: String; let label: String; let value: String100    var tint: Color = Theme.azure101    var detail: String?102    var body: some View {103        HStack(spacing: 12) {104            Image(systemName: symbol).font(.system(size: 16, weight: .semibold)).foregroundStyle(tint)105                .frame(width: 38, height: 38).background(tint.opacity(0.14), in: RoundedRectangle(cornerRadius: 11, style: .continuous))106            VStack(alignment: .leading, spacing: 1) {107                Text(label).font(.caption).foregroundStyle(.secondary).lineLimit(1)108                Text(value).font(.system(.title3, design: .rounded).weight(.semibold)).monospacedDigit().lineLimit(1).minimumScaleFactor(0.7)109                if let detail { Text(detail).font(.caption2).foregroundStyle(.secondary).lineLimit(1) }110            }111            Spacer(minLength: 0)112        }113        .padding(14).frame(maxWidth: .infinity, alignment: .leading)114        .background(Theme.card, in: RoundedRectangle(cornerRadius: 14, style: .continuous))115        .overlay(RoundedRectangle(cornerRadius: 14, style: .continuous).strokeBorder(Theme.stroke.opacity(0.35)))116        .shadow(color: .black.opacity(0.05), radius: 10, x: 0, y: 4)117        .accessibilityElement(children: .combine)118    }119}120121/// A prominent action with an icon above its label (dashboard shortcuts). An action, never a status.122struct QuickAction: View {123    let symbol: String; let title: String; var onDark = false124    let action: () -> Void125    @State private var hover = false126    var body: some View {127        Button(action: action) {128            VStack(spacing: 6) {129                Image(systemName: symbol).font(.system(size: 17, weight: .semibold))130                    .frame(width: 46, height: 46)131                    .background(onDark ? AnyShapeStyle(.white.opacity(hover ? 0.26 : 0.16)) : AnyShapeStyle(Theme.azure.opacity(hover ? 0.22 : 0.13)), in: Circle())132                Text(title).font(.caption.weight(.medium))133            }134            .foregroundStyle(onDark ? .white : Theme.azure).frame(minWidth: 70)135            .contentShape(Rectangle())136        }137        .buttonStyle(.plain).onHover { hover = $0 }138    }139}140141/// A label + value row for review sheets and detail panes. The value is selectable text: an address can always be copied and compared.142struct Field: View {143    let label: String144    let value: String145    var mono = false146    var emphasis = false147    var body: some View {148        HStack(alignment: .firstTextBaseline, spacing: 12) {149            Text(label).foregroundStyle(.secondary).frame(width: 170, alignment: .leading)150            Text(value).font(mono ? Theme.monoSmall : .body).fontWeight(emphasis ? .semibold : .regular).textSelection(.enabled)151                .frame(maxWidth: .infinity, alignment: .leading).fixedSize(horizontal: false, vertical: true)152        }153    }154}155156/// How much a figure can be trusted — shown next to it, never hidden in a tooltip only.157enum Provenance { case verified, estimate, stale, unavailable, simulated, ai158    var label: String {159        switch self { case .verified: "Lu sur la chaîne"; case .estimate: "Estimation"; case .stale: "Périmé"; case .unavailable: "Indisponible"; case .simulated: "Simulation"; case .ai: "Analyse IA" }160    }161    var color: Color { switch self { case .verified: Theme.positive; case .estimate, .simulated, .ai: .secondary; case .stale: Theme.warning; case .unavailable: Theme.negative } }162    var symbol: String { switch self { case .verified: "checkmark.seal"; case .estimate: "tilde"; case .stale: "clock.badge.exclamationmark"; case .unavailable: "xmark.octagon"; case .simulated: "testtube.2"; case .ai: "sparkles" } }163}164struct ProvenanceTag: View {165    let p: Provenance; var text: String?166    var body: some View {167        Label(text ?? p.label, systemImage: p.symbol).font(.caption.weight(.medium)).foregroundStyle(p.color).lineLimit(1)168            .padding(.horizontal, 9).padding(.vertical, 4).background(p.color.opacity(0.12), in: Capsule())169    }170}171172struct Banner: View {173    enum Kind { case info, warning, danger, ok174        var color: Color { switch self { case .info: .blue; case .warning: Theme.warning; case .danger: Theme.negative; case .ok: Theme.positive } }175        var symbol: String { switch self { case .info: "info.circle.fill"; case .warning: "exclamationmark.triangle.fill"; case .danger: "exclamationmark.octagon.fill"; case .ok: "checkmark.circle.fill" } }176    }177    let kind: Kind; let text: String178    init(_ kind: Kind, _ text: String) { self.kind = kind; self.text = text }179    var body: some View {180        HStack(alignment: .top, spacing: 8) {181            Image(systemName: kind.symbol).foregroundStyle(kind.color)182            Text(text).fixedSize(horizontal: false, vertical: true).textSelection(.enabled)183            Spacer(minLength: 0)184        }185        .padding(.horizontal, 12).padding(.vertical, 10).background(kind.color.opacity(0.10), in: RoundedRectangle(cornerRadius: 11, style: .continuous))186        .overlay(RoundedRectangle(cornerRadius: 11, style: .continuous).strokeBorder(kind.color.opacity(0.30)))187    }188}189190/// A screen's scaffold: title, optional subtitle, scrollable content with the standard margins.191struct Page<Content: View>: View {192    let title: String193    var subtitle: String?194    @ViewBuilder var content: Content195    init(_ title: String, subtitle: String? = nil, @ViewBuilder content: () -> Content) { self.title = title; self.subtitle = subtitle; self.content = content() }196    var body: some View {197        ScrollView {198            VStack(alignment: .leading, spacing: 18) {199                VStack(alignment: .leading, spacing: 3) {200                    Text(title).font(.system(size: 30, weight: .bold, design: .rounded))201                    if let subtitle { Text(subtitle).font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) }202                }203                content204            }205            .padding(.horizontal, 28).padding(.vertical, 22).frame(maxWidth: 1180, alignment: .leading).frame(maxWidth: .infinity, alignment: .leading)206        }207        .background(Backdrop())208    }209}210211struct EmptyState: View {212    let symbol: String; let title: String; var detail: String?213    var body: some View {214        VStack(spacing: 8) {215            Image(systemName: symbol).font(.system(size: 28, weight: .medium)).foregroundStyle(Theme.azure.opacity(0.75))216                .frame(width: 68, height: 68).background(Theme.azure.opacity(0.10), in: Circle())217            Text(title).font(.system(.headline, design: .rounded))218            if let detail { Text(detail).foregroundStyle(.secondary).multilineTextAlignment(.center).frame(maxWidth: 460) }219        }.frame(maxWidth: .infinity).padding(40)220    }221}222223/// A coloured disc with the asset's symbol initials: the app ships no third-party logo and never treats a logo as proof of identity.224struct AssetBadge: View {225    let symbol: String; var size: CGFloat = 32226    var body: some View {227        let hue = Double(abs(symbol.unicodeScalars.reduce(0) { ($0 &* 31) &+ Int($1.value) }) % 360) / 360228        Text(String(symbol.prefix(symbol.count > 4 ? 3 : 4))).font(.system(size: size * 0.31, weight: .heavy, design: .rounded)).minimumScaleFactor(0.5).lineLimit(1)229            .foregroundStyle(.white).frame(width: size, height: size)230            .background(LinearGradient(colors: [Color(hue: hue, saturation: 0.52, brightness: 0.82), Color(hue: hue, saturation: 0.70, brightness: 0.52)], startPoint: .topLeading, endPoint: .bottomTrailing), in: Circle())231            .overlay(Circle().strokeBorder(.white.opacity(0.18)))232            .shadow(color: Color(hue: hue, saturation: 0.6, brightness: 0.5).opacity(0.28), radius: 4, x: 0, y: 2)233            .accessibilityHidden(true)234    }235}236237extension View {238    /// Destructive or irreversible actions are never the default button and always spelled out.239    func copyable(_ text: String) -> some View {240        contextMenu { Button("Copier") { NSPasteboard.general.clearContents(); NSPasteboard.general.setString(text, forType: .string) } }241    }242}243func copyToPasteboard(_ s: String) { NSPasteboard.general.clearContents(); NSPasteboard.general.setString(s, forType: .string) }244
Files main
Menu

Theme