SPB Git

Repository Public MIT

GlassFieldChrome.swift 8.0 KB 232 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassForms/Forms/GlassFieldChrome.swift8// Module           : SPGlassForms9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//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          : MIT33//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 SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// The frame shared by GlassKit's form fields: a label above, the field, then an error or a helper text.48struct GlassFieldScaffold<Field: View, Accessory: View>: View {49    @GlassThemed private var theme50    let label: Text?51    let isRequired: Bool52    let helper: Text?53    let error: String?54    @ViewBuilder let field: Field55    @ViewBuilder let accessory: Accessory5657    var body: some View {58        VStack(alignment: .leading, spacing: theme.spacing.xxs) {59            if let label {60                HStack(alignment: .firstTextBaseline, spacing: theme.spacing.xxs) {61                    label62                        .font(theme.font(.callout))63                        .foregroundStyle(theme.color(.textPrimary))64                    if isRequired {65                        Text(verbatim: "*")66                            .font(theme.font(.callout))67                            .foregroundStyle(theme.color(.destructive))68                            .accessibilityHidden(true)69                    }70                }71                .accessibilityHidden(true)72            }73            field74            HStack(alignment: .firstTextBaseline, spacing: theme.spacing.xs) {75                message76                Spacer(minLength: 0)77                accessory78            }79        }80    }8182    @ViewBuilder83    private var message: some View {84        if let error {85            Label {86                Text(verbatim: error)87            } icon: {88                Image(systemName: "exclamationmark.circle.fill").accessibilityHidden(true)89            }90            .font(theme.font(.caption))91            .foregroundStyle(theme.color(.destructive))92            .accessibilityElement(children: .ignore)93            .accessibilityLabel(Text(verbatim: "\(GlassL10n.string(.error)): \(error)"))94        } else if let helper {95            helper96                .font(theme.font(.caption))97                .foregroundStyle(theme.color(.textSecondary))98        }99    }100}101102extension GlassFieldScaffold where Accessory == EmptyView {103    init(label: Text?, isRequired: Bool, helper: Text?, error: String?, @ViewBuilder field: () -> Field) {104        self.init(label: label, isRequired: isRequired, helper: helper, error: error, field: field, accessory: { EmptyView() })105    }106}107108/// Words describing a field to assistive technologies.109enum GlassFieldAccessibility {110    /// The accessibility label of a field: its visible label, followed by "Required" when it is.111    static func label(_ label: Text?, isRequired: Bool) -> Text {112        let base = label ?? Text(verbatim: "")113        return isRequired ? Text("\(base), \(Text(verbatim: GlassL10n.string(.required)))") : base114    }115}116117extension View {118    /// The opaque surface of a text field, outlined according to focus and error.119    func glassFieldSurface(isFocused: Bool, hasError: Bool) -> some View {120        modifier(GlassFieldSurfaceModifier(isFocused: isFocused, hasError: hasError))121    }122123    /// Applies text input settings. Keyboard and capitalisation exist only on iOS and iPadOS.124    func glassTextInput(_ configuration: GlassTextInputConfiguration) -> some View {125        modifier(GlassTextInputModifier(configuration: configuration))126    }127}128129private struct GlassFieldSurfaceModifier: ViewModifier {130    @GlassThemed private var theme131    @Environment(\.isEnabled) private var isEnabled132    let isFocused: Bool133    let hasError: Bool134135    func body(content: Content) -> some View {136        let shape = theme.shape(\.small)137        content138            .padding(.horizontal, theme.spacing.sm)139            .padding(.vertical, theme.spacing.xs)140            .frame(minHeight: theme.metrics.height(.regular))141            .background(theme.color(.surface), in: shape)142            .glassFieldOutline(isFocused: isFocused, hasError: hasError, in: shape)143            .opacity(isEnabled ? 1 : 0.6)144    }145}146147private struct GlassTextInputModifier: ViewModifier {148    let configuration: GlassTextInputConfiguration149150    func body(content: Content) -> some View {151        #if os(iOS)152        content153            .autocorrectionDisabled(configuration.autocorrectionDisabled)154            .textInputAutocapitalization(configuration.capitalization.system)155            .keyboardType(configuration.keyboard.system)156            .textContentType(configuration.contentKind?.system)157        #else158        content159            .autocorrectionDisabled(configuration.autocorrectionDisabled)160            .textContentType(configuration.contentKind?.system)161        #endif162    }163}164165#if os(iOS)166extension GlassTextCapitalization {167    var system: TextInputAutocapitalization {168        switch self {169        case .never: .never170        case .words: .words171        case .sentences: .sentences172        case .characters: .characters173        }174    }175}176177extension GlassKeyboardKind {178    var system: UIKeyboardType {179        switch self {180        case .standard: .default181        case .asciiCapable: .asciiCapable182        case .emailAddress: .emailAddress183        case .url: .URL184        case .numberPad: .numberPad185        case .decimalPad: .decimalPad186        case .phonePad: .phonePad187        }188    }189}190191extension GlassTextContentKind {192    var system: UITextContentType {193        switch self {194        case .name: .name195        case .givenName: .givenName196        case .familyName: .familyName197        case .emailAddress: .emailAddress198        case .username: .username199        case .password: .password200        case .newPassword: .newPassword201        case .oneTimeCode: .oneTimeCode202        case .telephoneNumber: .telephoneNumber203        case .url: .URL204        case .postalCode: .postalCode205        case .fullStreetAddress: .fullStreetAddress206        case .organizationName: .organizationName207        }208    }209}210#else211extension GlassTextContentKind {212    /// macOS knows fewer content types; the others leave AutoFill to the system's own guess.213    var system: NSTextContentType? {214        switch self {215        case .username: .username216        case .password: .password217        case .newPassword: .newPassword218        case .oneTimeCode: .oneTimeCode219        case .emailAddress: .emailAddress220        case .name: .name221        case .givenName: .givenName222        case .familyName: .familyName223        case .telephoneNumber: .telephoneNumber224        case .url: .URL225        case .postalCode: .postalCode226        case .fullStreetAddress: .fullStreetAddress227        case .organizationName: .organizationName228        }229    }230}231#endif232
Files main
Menu

Theme