SPB Git

Repository Public

Design.swift 12.3 KB 301 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Keyboard9// File             : App/Design.swift10// Description      : Visual vocabulary of the app: the SP mark, the aurora11//                    palette shared with the default keyboard theme, colour12//                    bridging between SwiftUI and theme tokens, and small13//                    reusable rows.14// Module           : App15// Repository       : https://git.spboucher.ai/sp-keyboard16// Created          : 2026-12-0117// Last Modified    : 2026-12-0118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author           : Simon-Pierre Boucher24// Position         : Professor of Finance25// Institution      : Université du Québec en Outaouais (UQO)26// Department       : Département des sciences administratives27// Organization     : SP Boucher28//29// Website          : https://spboucher.ai30// Git              : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General          : info@spboucher.ai37// Professional     : contact@spboucher.ai38// Direct           : simon@spboucher.ai39// Research         : research@spboucher.ai40// Development      : dev@spboucher.ai41// Support          : support@spboucher.ai42// Security/Admin   : admin@spboucher.ai43// Notifications    : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification   : Public70// Distribution     : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License          : None specified (no license file in this repository)84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import SwiftUI92import UIKit93import ThemeEngine9495enum Palette {96    static let cyan = Color(red: 0.13, green: 0.90, blue: 0.94)97    static let violet = Color(red: 0.545, green: 0.361, blue: 0.965)98    static let rose = Color(red: 0.957, green: 0.447, blue: 0.714)99    static let emerald = Color(red: 0.204, green: 0.827, blue: 0.6)100    static let night = Color(red: 0.016, green: 0.063, blue: 0.098)101    static let aurora = LinearGradient(colors: [cyan, violet, rose], startPoint: .topLeading, endPoint: .bottomTrailing)102}103104extension RGBA {105    var color: Color { Color(.sRGB, red: r, green: g, blue: b, opacity: a) }106107    init(_ color: Color) {108        var (r, g, b, a): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 1)109        UIColor(color).getRed(&r, green: &g, blue: &b, alpha: &a)110        self.init(r: r, g: g, b: b, a: a)111    }112}113114extension GradientSpec {115    var linearGradient: LinearGradient {116        let list = colors.isEmpty ? [RGBA.black] : colors117        let points = unitPoints118        return LinearGradient(colors: (list.count == 1 ? [list[0], list[0]] : list).map(\.color),119                              startPoint: UnitPoint(x: points.start.x, y: points.start.y),120                              endPoint: UnitPoint(x: points.end.x, y: points.end.y))121    }122}123124/// Binding that edits one `RGBA` through a SwiftUI `ColorPicker`.125extension Binding where Value == RGBA {126    var asColor: Binding<Color> {127        Binding<Color>(get: { wrappedValue.color }, set: { wrappedValue = RGBA($0) })128    }129}130131/// Four-point star, the SP AI glyph.132struct StarShape: Shape {133    func path(in rect: CGRect) -> Path {134        let center = CGPoint(x: rect.midX, y: rect.midY)135        let outer = min(rect.width, rect.height) / 2136        let inner = outer * 0.28137        var path = Path()138        for index in 0..<8 {139            let radius = index % 2 == 0 ? outer : inner140            let angle = CGFloat(index) * .pi / 4 - .pi / 2141            let point = CGPoint(x: center.x + cos(angle) * radius, y: center.y + sin(angle) * radius)142            if index == 0 { path.move(to: point) } else { path.addLine(to: point) }143        }144        path.closeSubpath()145        return path146    }147}148149/// The SP mark: a night-sky key cap with an aurora band, the letters SP and the AI star.150struct SPLogo: View {151    var size: CGFloat = 72152153    var body: some View {154        ZStack {155            RoundedRectangle(cornerRadius: size * 0.24, style: .continuous)156                .fill(Palette.night)157            RoundedRectangle(cornerRadius: size * 0.24, style: .continuous)158                .fill(LinearGradient(colors: [Palette.cyan.opacity(0.55), Palette.violet.opacity(0.5), Palette.rose.opacity(0.45), Palette.emerald.opacity(0.35)],159                                     startPoint: .topLeading, endPoint: .bottomTrailing))160            RoundedRectangle(cornerRadius: size * 0.24, style: .continuous)161                .strokeBorder(Palette.aurora, lineWidth: max(1.5, size * 0.03))162            Text("SP")163                .font(.system(size: size * 0.44, weight: .heavy, design: .rounded))164                .foregroundStyle(.white)165                .offset(y: size * 0.04)166            StarShape()167                .fill(LinearGradient(colors: [.white, Palette.cyan], startPoint: .top, endPoint: .bottom))168                .frame(width: size * 0.22, height: size * 0.22)169                .offset(x: size * 0.27, y: -size * 0.27)170        }171        .frame(width: size, height: size)172        .accessibilityElement(children: .ignore)173        .accessibilityLabel("Logo SP Keyboard")174    }175}176177/// One verifiable fact with its state.178struct StatusRow: View {179    enum Level { case good, warning, neutral }180    var title: String181    var detail: String182    var level: Level183184    var body: some View {185        HStack(alignment: .firstTextBaseline, spacing: 12) {186            Image(systemName: symbol)187                .foregroundStyle(tint)188                .accessibilityHidden(true)189            VStack(alignment: .leading, spacing: 2) {190                Text(title).font(.subheadline.weight(.semibold))191                Text(detail).font(.footnote).foregroundStyle(.secondary)192            }193        }194        .accessibilityElement(children: .combine)195    }196197    private var symbol: String {198        switch level {199        case .good: "checkmark.circle.fill"200        case .warning: "exclamationmark.triangle.fill"201        case .neutral: "questionmark.circle"202        }203    }204205    private var tint: Color {206        switch level {207        case .good: .green208        case .warning: .orange209        case .neutral: .secondary210        }211    }212}213214/// A slider with its title and formatted value on one line.215struct LabeledSlider: View {216    var title: String217    @Binding var value: Double218    var range: ClosedRange<Double>219    var step: Double? = nil220    var format: (Double) -> String = { String(format: "%.2f", $0) }221222    var body: some View {223        VStack(alignment: .leading, spacing: 2) {224            HStack {225                Text(title)226                Spacer()227                Text(format(value)).foregroundStyle(.secondary).monospacedDigit()228            }229            .font(.subheadline)230            if let step {231                Slider(value: $value, in: range, step: step) { Text(title) }232            } else {233                Slider(value: $value, in: range) { Text(title) }234            }235        }236        .accessibilityElement(children: .combine)237        .accessibilityLabel(title)238        .accessibilityValue(format(value))239        .accessibilityAdjustableAction { direction in240            let delta = step ?? (range.upperBound - range.lowerBound) / 20241            switch direction {242            case .increment: value = min(range.upperBound, value + delta)243            case .decrement: value = max(range.lowerBound, value - delta)244            @unknown default: break245            }246        }247    }248}249250/// Miniature of a theme drawn in SwiftUI (cheap enough for a gallery of dozens).251struct ThemeSwatch: View {252    var theme: Theme253    private static let rows = [10, 10, 8]254255    var body: some View {256        let resolved = theme.legible()257        GeometryReader { proxy in258            let spacing: CGFloat = 3259            let keyWidth = (proxy.size.width - spacing * 11) / 10260            let keyHeight = (proxy.size.height - spacing * 5) / 4261            ZStack {262                resolved.background.linearGradient263                if let first = resolved.effectColors.first, resolved.effect != .none {264                    RadialGradient(colors: [first.color.opacity(0.55), .clear], center: .topLeading, startRadius: 0, endRadius: proxy.size.width * 0.7)265                    if let last = resolved.effectColors.last {266                        RadialGradient(colors: [last.color.opacity(0.45), .clear], center: .bottomTrailing, startRadius: 0, endRadius: proxy.size.width * 0.6)267                    }268                }269                VStack(spacing: spacing) {270                    ForEach(0..<3, id: \.self) { row in271                        HStack(spacing: spacing) {272                            ForEach(0..<Self.rows[row], id: \.self) { _ in273                                key(resolved.letterKey, tint: resolved.tint(forKey: "", row: row, fallback: resolved.letterKey.border), radius: resolved.metrics.cornerRadius, opacity: resolved.metrics.keyOpacity)274                                    .frame(width: keyWidth, height: keyHeight)275                            }276                        }277                    }278                    HStack(spacing: spacing) {279                        key(resolved.functionKey, tint: resolved.functionKey.border, radius: resolved.metrics.cornerRadius, opacity: resolved.metrics.keyOpacity)280                            .frame(width: keyWidth * 1.6, height: keyHeight)281                        key(resolved.letterKey, tint: resolved.letterKey.border, radius: resolved.metrics.cornerRadius, opacity: resolved.metrics.keyOpacity)282                            .frame(height: keyHeight)283                        key(resolved.accentKey, tint: resolved.accentKey.border, radius: resolved.metrics.cornerRadius, opacity: 1)284                            .frame(width: keyWidth * 2.2, height: keyHeight)285                    }286                    .padding(.horizontal, spacing)287                }288            }289        }290        .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))291        .accessibilityHidden(true)292    }293294    private func key(_ style: KeyStyle, tint: RGBA, radius: Double, opacity: Double) -> some View {295        RoundedRectangle(cornerRadius: min(radius, 9) * 0.45, style: .continuous)296            .fill(style.fill.linearGradient)297            .opacity(opacity)298            .overlay(RoundedRectangle(cornerRadius: min(radius, 9) * 0.45, style: .continuous).strokeBorder(tint.color, lineWidth: 0.6))299    }300}301
Files main
Menu

Theme