SPB Git

Repository Public MIT

GlassToggle.swift 5.4 KB 146 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-0313//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    @ScaledMetric(relativeTo: .body) private var tileSide: CGFloat = 3269    private let title: Text70    private let description: Text?71    private let systemImage: String?72    private let appearance: GlassToggleAppearance73    @Binding private var isOn: Bool7475    /// Creates a toggle with a localised title.76    public init(77        _ title: LocalizedStringKey,78        description: LocalizedStringKey? = nil,79        systemImage: String? = nil,80        appearance: GlassToggleAppearance = .automatic,81        isOn: Binding<Bool>82    ) {83        self.init(title: Text(title), description: description.map { Text($0) }, systemImage: systemImage, appearance: appearance, isOn: isOn)84    }8586    /// Creates a toggle from `Text` values, for titles that are data.87    public init(88        title: Text,89        description: Text? = nil,90        systemImage: String? = nil,91        appearance: GlassToggleAppearance = .automatic,92        isOn: Binding<Bool>93    ) {94        self.title = title95        self.description = description96        self.systemImage = systemImage97        self.appearance = appearance98        self._isOn = isOn99    }100101    public var body: some View {102        styled(103            Toggle(isOn: $isOn) {104                HStack(alignment: .center, spacing: theme.spacing.sm) {105                    if let systemImage {106                        // A tinted tile: the symbol takes the theme's primary colour while the switch is on.107                        Image(systemName: systemImage)108                            .font(theme.font(.callout).weight(.medium))109                            .foregroundStyle(theme.color(isOn ? .primary : .textSecondary))110                            .frame(width: tileSide, height: tileSide)111                            .background(theme.color(isOn ? .primary : .textSecondary).opacity(0.14), in: theme.shape(\.small))112                            .accessibilityHidden(true)113                    }114                    VStack(alignment: .leading, spacing: theme.spacing.xxs) {115                        title116                            .font(theme.font(.body))117                            .foregroundStyle(theme.color(.textPrimary))118                        if let description {119                            description120                                .font(theme.font(.caption))121                                .foregroundStyle(theme.color(.textSecondary))122                        }123                    }124                }125            }126        )127        .tint(theme.color(.primary))128    }129130    @ViewBuilder131    private func styled(_ toggle: some View) -> some View {132        switch appearance.resolved() {133        case .automatic:134            toggle.toggleStyle(.automatic)135        case .switch:136            toggle.toggleStyle(.switch)137        case .checkbox:138            #if os(macOS)139            toggle.toggleStyle(.checkbox)140            #else141            toggle.toggleStyle(.switch)142            #endif143        }144    }145}146
Files main
Menu

Theme