// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassForms/Selection/GlassToggle.swift // Module : SPGlassForms // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-03 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// Appearance of a ``GlassToggle``. public enum GlassToggleAppearance: String, CaseIterable, Sendable { /// The platform's default. case automatic /// A switch. case `switch` /// A checkbox. Only macOS has one; elsewhere a switch is used. case checkbox /// The appearance actually used on a platform. public func resolved(for platform: GlassPlatform = .current) -> GlassToggleAppearance { self == .checkbox && platform != .macOS ? .switch : self } } /// The system `Toggle` with a title, an optional description and the theme's tint. /// /// ```swift /// GlassToggle("Notifications", description: "Alerts for new messages and mentions", isOn: $notifications) /// ``` /// /// The system draws the switch, including its Liquid Glass thumb. The description is part of the label, so /// VoiceOver reads it with the title. Disable it with `.disabled(_:)`. public struct GlassToggle: View { @GlassThemed private var theme @ScaledMetric(relativeTo: .body) private var tileSide: CGFloat = 32 private let title: Text private let description: Text? private let systemImage: String? private let appearance: GlassToggleAppearance @Binding private var isOn: Bool /// Creates a toggle with a localised title. public init( _ title: LocalizedStringKey, description: LocalizedStringKey? = nil, systemImage: String? = nil, appearance: GlassToggleAppearance = .automatic, isOn: Binding ) { self.init(title: Text(title), description: description.map { Text($0) }, systemImage: systemImage, appearance: appearance, isOn: isOn) } /// Creates a toggle from `Text` values, for titles that are data. public init( title: Text, description: Text? = nil, systemImage: String? = nil, appearance: GlassToggleAppearance = .automatic, isOn: Binding ) { self.title = title self.description = description self.systemImage = systemImage self.appearance = appearance self._isOn = isOn } public var body: some View { styled( Toggle(isOn: $isOn) { HStack(alignment: .center, spacing: theme.spacing.sm) { if let systemImage { // A tinted tile: the symbol takes the theme's primary colour while the switch is on. Image(systemName: systemImage) .font(theme.font(.callout).weight(.medium)) .foregroundStyle(theme.color(isOn ? .primary : .textSecondary)) .frame(width: tileSide, height: tileSide) .background(theme.color(isOn ? .primary : .textSecondary).opacity(0.14), in: theme.shape(\.small)) .accessibilityHidden(true) } VStack(alignment: .leading, spacing: theme.spacing.xxs) { title .font(theme.font(.body)) .foregroundStyle(theme.color(.textPrimary)) if let description { description .font(theme.font(.caption)) .foregroundStyle(theme.color(.textSecondary)) } } } } ) .tint(theme.color(.primary)) } @ViewBuilder private func styled(_ toggle: some View) -> some View { switch appearance.resolved() { case .automatic: toggle.toggleStyle(.automatic) case .switch: toggle.toggleStyle(.switch) case .checkbox: #if os(macOS) toggle.toggleStyle(.checkbox) #else toggle.toggleStyle(.switch) #endif } } }