SPB Git

Repository Public

Components.swift 12.7 KB 330 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraUI/Components.swift8// Module           : Qyvora UI9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0513//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// Surface card: graphite fill, hairline border, no shadow.44public struct QCard<Content: View>: View {45    let padding: CGFloat46    let content: Content4748    public init(padding: CGFloat = QMetric.padding, @ViewBuilder content: () -> Content) {49        self.padding = padding50        self.content = content()51    }5253    public var body: some View {54        content55            .padding(padding)56            .frame(maxWidth: .infinity, alignment: .leading)57            .background(QColor.surface, in: RoundedRectangle(cornerRadius: QMetric.radius, style: .continuous))58            .overlay(RoundedRectangle(cornerRadius: QMetric.radius, style: .continuous).strokeBorder(QColor.stroke, lineWidth: 1))59    }60}6162public struct QPrimaryButtonStyle: ButtonStyle {63    @Environment(\.isEnabled) private var enabled64    public init() {}6566    public func makeBody(configuration: Configuration) -> some View {67        configuration.label68            .font(QFont.headline)69            .foregroundStyle(QColor.onAccent)70            .frame(maxWidth: .infinity, minHeight: 52)71            .background(QColor.accentGradient, in: RoundedRectangle(cornerRadius: QMetric.radiusSmall + 2, style: .continuous))72            .opacity(enabled ? (configuration.isPressed ? 0.82 : 1) : 0.38)73            .scaleEffect(configuration.isPressed ? 0.985 : 1)74            .animation(.snappy(duration: 0.18), value: configuration.isPressed)75    }76}7778public struct QSecondaryButtonStyle: ButtonStyle {79    @Environment(\.isEnabled) private var enabled80    public init() {}8182    public func makeBody(configuration: Configuration) -> some View {83        configuration.label84            .font(QFont.headline)85            .foregroundStyle(QColor.textPrimary)86            .frame(maxWidth: .infinity, minHeight: 52)87            .background(QColor.surfaceRaised, in: RoundedRectangle(cornerRadius: QMetric.radiusSmall + 2, style: .continuous))88            .overlay(RoundedRectangle(cornerRadius: QMetric.radiusSmall + 2, style: .continuous).strokeBorder(QColor.stroke, lineWidth: 1))89            .opacity(enabled ? (configuration.isPressed ? 0.7 : 1) : 0.4)90    }91}9293public extension ButtonStyle where Self == QPrimaryButtonStyle {94    static var qPrimary: QPrimaryButtonStyle { QPrimaryButtonStyle() }95}9697public extension ButtonStyle where Self == QSecondaryButtonStyle {98    static var qSecondary: QSecondaryButtonStyle { QSecondaryButtonStyle() }99}100101/// Small upper-case label above a section.102public struct QEyebrow: View {103    let text: String104    public init(_ text: String) { self.text = text }105    public var body: some View {106        Text(text.uppercased())107            .font(QFont.eyebrow)108            .kerning(1.1)109            .foregroundStyle(QColor.textSecondary)110            .accessibilityAddTraits(.isHeader)111    }112}113114/// Status pill with an icon and a label; the state is never conveyed by colour alone.115public struct QBadge: View {116    public enum Tone: Sendable { case neutral, positive, warning, negative, accent }117    let text: String118    let systemImage: String?119    let tone: Tone120121    public init(_ text: String, systemImage: String? = nil, tone: Tone = .neutral) {122        self.text = text123        self.systemImage = systemImage124        self.tone = tone125    }126127    var color: Color {128        switch tone {129        case .neutral: return QColor.textSecondary130        case .positive: return QColor.positive131        case .warning: return QColor.warning132        case .negative: return QColor.negative133        case .accent: return QColor.violet134        }135    }136137    public var body: some View {138        HStack(spacing: 5) {139            if let systemImage { Image(systemName: systemImage).imageScale(.small) }140            Text(text).lineLimit(2)141        }142        .fixedSize(horizontal: false, vertical: true)143        .font(.caption.weight(.semibold))144        .foregroundStyle(color)145        .padding(.horizontal, 9)146        .padding(.vertical, 5)147        // a neutral fill and a hairline in the tone: the text keeps its 4.5:1 contrast on every surface148        .background(QColor.surfaceRaised, in: Capsule())149        .overlay(Capsule().strokeBorder(color.opacity(0.45), lineWidth: 1))150    }151}152153/// Label and value on one line, the value in monospaced digits. A long unbroken value (an address, a hash) wraps at154/// any character and never shows a hyphen; it is then copied, exactly as given, from its context menu.155public struct QKeyValueRow: View {156    let label: String157    let value: String158    let mono: Bool159160    public init(_ label: String, _ value: String, mono: Bool = false) {161        self.label = label162        self.value = value163        self.mono = mono164    }165166    @Environment(\.dynamicTypeSize) private var typeSize167168    /// What is drawn: the value itself, or its wrappable form when it holds a long unbroken token.169    var displayed: String { mono ? UnbrokenValue.wrappable(value) : UnbrokenValue.wrappingLongTokens(value) }170171    @ViewBuilder var valueText: some View {172        let text = Text(displayed)173            .font(mono ? QFont.mono : QFont.callout.monospacedDigit())174            .foregroundStyle(QColor.textPrimary)175        if displayed == value {176            text.textSelection(.enabled)177        } else {178            // a selection would carry the invisible break opportunities: the exact value is copied from the menu instead179            text.contextMenu {180                Button {181                    QClipboard.copy(value, sensitive: false)182                } label: {183                    Label("Copy", systemImage: "doc.on.doc")184                }185            }186            .accessibilityLabel(MiddleTruncation.grouped(value))187        }188    }189190    public var body: some View {191        Group {192            if typeSize.isAccessibilitySize {193                // At accessibility sizes the pair stacks so that neither side is squeezed.194                VStack(alignment: .leading, spacing: 2) {195                    Text(label).font(QFont.callout).foregroundStyle(QColor.textSecondary)196                    valueText197                }198                .frame(maxWidth: .infinity, alignment: .leading)199            } else {200                HStack(alignment: .firstTextBaseline, spacing: 12) {201                    Text(label).font(QFont.callout).foregroundStyle(QColor.textSecondary)202                    Spacer(minLength: 8)203                    valueText.multilineTextAlignment(.trailing)204                }205            }206        }207        .accessibilityElement(children: .combine)208    }209}210211/// The QYVORA mark drawn in vectors: a closed ring of twelve lattice nodes crossed by one diagonal stroke.212public struct QMark: View {213    public init() {}214215    public var body: some View {216        Canvas { context, size in217            let s = min(size.width, size.height)218            let c = CGPoint(x: size.width / 2 - s * 0.03, y: size.height / 2 - s * 0.03)219            let r = s * 0.36220            let nodes = (0..<12).map { i -> CGPoint in221                let a = Double(i) * .pi / 6 - .pi / 2222                return CGPoint(x: c.x + r * cos(a), y: c.y + r * sin(a))223            }224            var ring = Path()225            ring.addLines(nodes + [nodes[0]])226            context.stroke(ring, with: .color(QColor.textPrimary), style: StrokeStyle(lineWidth: s * 0.034, lineCap: .round, lineJoin: .round))227            for p in nodes {228                let d = s * 0.075229                let dot = Path(ellipseIn: CGRect(x: p.x - d / 2, y: p.y - d / 2, width: d, height: d))230                context.fill(dot, with: .color(QColor.background))231                context.stroke(dot, with: .color(QColor.textPrimary), lineWidth: s * 0.018)232            }233            var tail = Path()234            tail.move(to: CGPoint(x: c.x + s * 0.11, y: c.y + s * 0.11))235            tail.addLine(to: CGPoint(x: c.x + s * 0.43, y: c.y + s * 0.43))236            context.stroke(tail, with: .color(QColor.background), style: StrokeStyle(lineWidth: s * 0.16, lineCap: .round))237            context.stroke(tail, with: .linearGradient(Gradient(colors: [QColor.violet, QColor.blue]), startPoint: CGPoint(x: c.x, y: c.y), endPoint: CGPoint(x: c.x + s * 0.45, y: c.y + s * 0.45)),238                           style: StrokeStyle(lineWidth: s * 0.085, lineCap: .round))239        }240        .aspectRatio(1, contentMode: .fit)241        .accessibilityLabel("QYVORA")242    }243}244245/// Screen background used by every root view.246public struct QScreenBackground: ViewModifier {247    public func body(content: Content) -> some View {248        content.background(QColor.background.ignoresSafeArea())249    }250}251252public extension View {253    func qScreen() -> some View { modifier(QScreenBackground()) }254255    /// Native grouped list on the QYVORA background (use with `qRow()` on the rows or sections).256    func qListStyle() -> some View {257        self258            #if os(iOS)259            .listStyle(.insetGrouped)260            #endif261            .scrollContentBackground(.hidden)262            .background(QColor.background.ignoresSafeArea())263    }264265    /// Graphite row background and hairline separator for rows of a `qListStyle()` list.266    func qRow() -> some View {267        self268            .listRowBackground(QColor.surface)269            .listRowSeparatorTint(QColor.stroke)270    }271}272273/// Square glyph tile used at the start of navigation rows.274public struct QIconTile: View {275    let systemImage: String276    let tint: Color277    @ScaledMetric(relativeTo: .body) private var size: CGFloat = 30278279    public init(_ systemImage: String, tint: Color = QColor.textPrimary) {280        self.systemImage = systemImage281        self.tint = tint282    }283284    public var body: some View {285        Image(systemName: systemImage)286            .font(.system(size: size * 0.5, weight: .semibold))287            .foregroundStyle(tint)288            .frame(width: size, height: size)289            .background(QColor.surfaceRaised, in: RoundedRectangle(cornerRadius: size * 0.27, style: .continuous))290            .overlay(RoundedRectangle(cornerRadius: size * 0.27, style: .continuous).strokeBorder(QColor.stroke, lineWidth: 1))291            .accessibilityHidden(true)292    }293}294295/// Navigation row content: glyph tile, title, optional subtitle and optional trailing note.296public struct QNavigationLabel: View {297    let title: String298    let subtitle: String?299    let systemImage: String300    let tint: Color301    let trailing: String?302303    public init(_ title: String, subtitle: String? = nil, systemImage: String, tint: Color = QColor.textPrimary, trailing: String? = nil) {304        self.title = title305        self.subtitle = subtitle306        self.systemImage = systemImage307        self.tint = tint308        self.trailing = trailing309    }310311    public var body: some View {312        HStack(spacing: 12) {313            QIconTile(systemImage, tint: tint)314            VStack(alignment: .leading, spacing: 2) {315                Text(title).font(QFont.body).foregroundStyle(QColor.textPrimary)316                if let subtitle {317                    Text(subtitle).font(QFont.caption).foregroundStyle(QColor.textSecondary)318                }319            }320            .fixedSize(horizontal: false, vertical: true)321            Spacer(minLength: 8)322            if let trailing {323                Text(trailing).font(QFont.callout).foregroundStyle(QColor.textSecondary).lineLimit(1)324            }325        }326        .padding(.vertical, 3)327        .accessibilityElement(children: .combine)328    }329}330
Files main
Menu

Theme