SPB Git

Repository Public MIT

GlassToggle.swift 5.0 KB 141 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassForms/Selection/GlassToggle.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 SwiftUI4243/// Appearance of a ``GlassToggle``.44public enum GlassToggleAppearance: String, CaseIterable, Sendable {45    /// The platform's default.46    case automatic47    /// A switch.48    case `switch`49    /// A checkbox. Only macOS has one; elsewhere a switch is used.50    case checkbox5152    /// The appearance actually used on a platform.53    public func resolved(for platform: GlassPlatform = .current) -> GlassToggleAppearance {54        self == .checkbox && platform != .macOS ? .switch : self55    }56}5758/// The system `Toggle` with a title, an optional description and the theme's tint.59///60/// ```swift61/// GlassToggle("Notifications", description: "Alerts for new messages and mentions", isOn: $notifications)62/// ```63///64/// The system draws the switch, including its Liquid Glass thumb. The description is part of the label, so65/// VoiceOver reads it with the title. Disable it with `.disabled(_:)`.66public struct GlassToggle: View {67    @GlassThemed private var theme68    private let title: Text69    private let description: Text?70    private let systemImage: String?71    private let appearance: GlassToggleAppearance72    @Binding private var isOn: Bool7374    /// Creates a toggle with a localised title.75    public init(76        _ title: LocalizedStringKey,77        description: LocalizedStringKey? = nil,78        systemImage: String? = nil,79        appearance: GlassToggleAppearance = .automatic,80        isOn: Binding<Bool>81    ) {82        self.init(title: Text(title), description: description.map { Text($0) }, systemImage: systemImage, appearance: appearance, isOn: isOn)83    }8485    /// Creates a toggle from `Text` values, for titles that are data.86    public init(87        title: Text,88        description: Text? = nil,89        systemImage: String? = nil,90        appearance: GlassToggleAppearance = .automatic,91        isOn: Binding<Bool>92    ) {93        self.title = title94        self.description = description95        self.systemImage = systemImage96        self.appearance = appearance97        self._isOn = isOn98    }99100    public var body: some View {101        styled(102            Toggle(isOn: $isOn) {103                HStack(alignment: .firstTextBaseline, spacing: theme.spacing.sm) {104                    if let systemImage {105                        Image(systemName: systemImage)106                            .foregroundStyle(theme.color(.textSecondary))107                            .accessibilityHidden(true)108                    }109                    VStack(alignment: .leading, spacing: theme.spacing.xxs) {110                        title111                            .font(theme.font(.body))112                            .foregroundStyle(theme.color(.textPrimary))113                        if let description {114                            description115                                .font(theme.font(.caption))116                                .foregroundStyle(theme.color(.textSecondary))117                        }118                    }119                }120            }121        )122        .tint(theme.color(.primary))123    }124125    @ViewBuilder126    private func styled(_ toggle: some View) -> some View {127        switch appearance.resolved() {128        case .automatic:129            toggle.toggleStyle(.automatic)130        case .switch:131            toggle.toggleStyle(.switch)132        case .checkbox:133            #if os(macOS)134            toggle.toggleStyle(.checkbox)135            #else136            toggle.toggleStyle(.switch)137            #endif138        }139    }140}141
Files main
Menu

Theme