SPB Git

Repository Public

Theme.swift 9.4 KB 250 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : MacLustr Identity9// File             : sdk/swift/Sources/MacLustrIdentityUI/Theme.swift10// Module           : MacLustrIdentityUI11// Repository       : https://git.spboucher.ai/maclustr-identity12// Project Website  : https://identity.spboucher.ai13// Created          : 2026-12-0114// Last Modified    : 2026-12-0115//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: admin@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788// Shared look: system colours and typography, one accent, no gradients.89import SwiftUI9091extension Color {92    /// Parses `#RRGGBB` or `#RGB`. Returns nil for anything else.93    init?(identityHex: String?) {94        guard var hex = identityHex?.trimmingCharacters(in: .whitespaces), hex.hasPrefix("#") else { return nil }95        hex.removeFirst()96        if hex.count == 3 { hex = hex.map { "\($0)\($0)" }.joined() }97        guard hex.count == 6, let v = UInt32(hex, radix: 16) else { return nil }98        self.init(.sRGB, red: Double((v >> 16) & 0xFF) / 255, green: Double((v >> 8) & 0xFF) / 255, blue: Double(v & 0xFF) / 255, opacity: 1)99    }100101    static var identityCard: Color {102        #if os(iOS)103        Color(uiColor: .secondarySystemGroupedBackground)104        #else105        Color(nsColor: .controlBackgroundColor)106        #endif107    }108109    static var identityBackground: Color {110        #if os(iOS)111        Color(uiColor: .systemGroupedBackground)112        #else113        Color(nsColor: .windowBackgroundColor)114        #endif115    }116}117118/// Language of the flow, available to every screen.119private struct IdentityLanguageKey: EnvironmentKey {120    static let defaultValue: IdentityLanguage = .en121}122123extension EnvironmentValues {124    var identityLanguage: IdentityLanguage {125        get { self[IdentityLanguageKey.self] }126        set { self[IdentityLanguageKey.self] = newValue }127    }128}129130/// Off-screen rendering (ImageRenderer) cannot draw scroll views; the rendering tests lay the content out flat.131private struct IdentityFlatLayoutKey: EnvironmentKey {132    static let defaultValue = false133}134135extension EnvironmentValues {136    var identityFlatLayout: Bool {137        get { self[IdentityFlatLayoutKey.self] }138        set { self[IdentityFlatLayoutKey.self] = newValue }139    }140}141142/// Full-width primary action.143struct PrimaryButtonStyle: ButtonStyle {144    @Environment(\.isEnabled) private var isEnabled145146    func makeBody(configuration: Configuration) -> some View {147        configuration.label148            .font(.headline)149            .multilineTextAlignment(.center)150            .frame(maxWidth: .infinity, minHeight: 28)151            .padding(.vertical, 12)152            .padding(.horizontal, 16)153            .foregroundStyle(.white)154            .background(RoundedRectangle(cornerRadius: 12, style: .continuous).fill(isEnabled ? Color.accentColor : Color.gray.opacity(0.5)))155            .opacity(configuration.isPressed ? 0.8 : 1)156            .contentShape(Rectangle())157    }158}159160/// Full-width secondary action.161struct SecondaryButtonStyle: ButtonStyle {162    func makeBody(configuration: Configuration) -> some View {163        configuration.label164            .font(.body.weight(.medium))165            .multilineTextAlignment(.center)166            .frame(maxWidth: .infinity, minHeight: 28)167            .padding(.vertical, 10)168            .padding(.horizontal, 16)169            .foregroundStyle(Color.accentColor)170            .background(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(Color.accentColor.opacity(0.4), lineWidth: 1))171            .opacity(configuration.isPressed ? 0.6 : 1)172            .contentShape(Rectangle())173    }174}175176/// Scrollable content with the actions pinned below it; both adapt to large text sizes.177struct StepLayout<Content: View, Actions: View>: View {178    @ViewBuilder var content: Content179    @ViewBuilder var actions: Actions180    @Environment(\.identityFlatLayout) private var flat181182    private var column: some View {183        VStack(alignment: .leading, spacing: 16) { content }184            .frame(maxWidth: 560, alignment: .leading)185            .padding(20)186            .frame(maxWidth: .infinity)187    }188189    var body: some View {190        VStack(spacing: 0) {191            if flat {192                column193                Spacer(minLength: 0)194            } else {195                ScrollView { column }196            }197            VStack(spacing: 10) { actions }198                .frame(maxWidth: 560)199                .padding(.horizontal, 20)200                .padding(.vertical, 12)201        }202    }203}204205/// Inline message for a recoverable problem.206struct NoticeView: View {207    enum Kind { case warning, info }208    var kind: Kind = .warning209    var text: String210211    var body: some View {212        HStack(alignment: .firstTextBaseline, spacing: 8) {213            Image(systemName: kind == .warning ? "exclamationmark.triangle" : "info.circle")214                .foregroundStyle(kind == .warning ? Color.orange : Color.secondary)215                .accessibilityHidden(true)216            Text(text).font(.callout).fixedSize(horizontal: false, vertical: true)217        }218        .padding(12)219        .frame(maxWidth: .infinity, alignment: .leading)220        .background(RoundedRectangle(cornerRadius: 10, style: .continuous).fill(Color.identityCard))221        .accessibilityElement(children: .combine)222    }223}224225enum Accessibility {226    @MainActor private static var lastAnnouncement = Date.distantPast227228    /// Speaks a guidance message with VoiceOver.229    /// - Parameter minimumGap: announcements closer than this to the previous one are dropped, so that fast-changing230    ///   live guidance does not flood the speech queue.231    @MainActor232    static func announce(_ text: String, minimumGap: TimeInterval = 0) {233        let now = Date()234        guard now.timeIntervalSince(lastAnnouncement) >= minimumGap else { return }235        lastAnnouncement = now236        if #available(iOS 17.0, macOS 14.0, *) {237            AccessibilityNotification.Announcement(text).post()238        } else {239            #if os(iOS)240            UIAccessibility.post(notification: .announcement, argument: text)241            #else242            if let window = NSApp.keyWindow {243                NSAccessibility.post(element: window, notification: .announcementRequested,244                                     userInfo: [.announcement: text, .priority: NSAccessibilityPriorityLevel.high.rawValue])245            }246            #endif247        }248    }249}250
Files main
Menu

Theme