Repository Public MIT
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 SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes4546/// Appearance of a ``GlassToggle``.47public enum GlassToggleAppearance: String, CaseIterable, Sendable {48 /// The platform's default.49 case automatic50 /// A switch.51 case `switch`52 /// A checkbox. Only macOS has one; elsewhere a switch is used.53 case checkbox5455 /// The appearance actually used on a platform.56 public func resolved(for platform: GlassPlatform = .current) -> GlassToggleAppearance {57 self == .checkbox && platform != .macOS ? .switch : self58 }59}6061/// The system `Toggle` with a title, an optional description and the theme's tint.62///63/// ```swift64/// GlassToggle("Notifications", description: "Alerts for new messages and mentions", isOn: $notifications)65/// ```66///67/// The system draws the switch, including its Liquid Glass thumb. The description is part of the label, so68/// VoiceOver reads it with the title. Disable it with `.disabled(_:)`.69public struct GlassToggle: View {70 @GlassThemed private var theme71 private let title: Text72 private let description: Text?73 private let systemImage: String?74 private let appearance: GlassToggleAppearance75 @Binding private var isOn: Bool7677 /// Creates a toggle with a localised title.78 public init(79 _ title: LocalizedStringKey,80 description: LocalizedStringKey? = nil,81 systemImage: String? = nil,82 appearance: GlassToggleAppearance = .automatic,83 isOn: Binding<Bool>84 ) {85 self.init(title: Text(title), description: description.map { Text($0) }, systemImage: systemImage, appearance: appearance, isOn: isOn)86 }8788 /// Creates a toggle from `Text` values, for titles that are data.89 public init(90 title: Text,91 description: Text? = nil,92 systemImage: String? = nil,93 appearance: GlassToggleAppearance = .automatic,94 isOn: Binding<Bool>95 ) {96 self.title = title97 self.description = description98 self.systemImage = systemImage99 self.appearance = appearance100 self._isOn = isOn101 }102103 public var body: some View {104 styled(105 Toggle(isOn: $isOn) {106 HStack(alignment: .firstTextBaseline, spacing: theme.spacing.sm) {107 if let systemImage {108 Image(systemName: systemImage)109 .foregroundStyle(theme.color(.textSecondary))110 .accessibilityHidden(true)111 }112 VStack(alignment: .leading, spacing: theme.spacing.xxs) {113 title114 .font(theme.font(.body))115 .foregroundStyle(theme.color(.textPrimary))116 if let description {117 description118 .font(theme.font(.caption))119 .foregroundStyle(theme.color(.textSecondary))120 }121 }122 }123 }124 )125 .tint(theme.color(.primary))126 }127128 @ViewBuilder129 private func styled(_ toggle: some View) -> some View {130 switch appearance.resolved() {131 case .automatic:132 toggle.toggleStyle(.automatic)133 case .switch:134 toggle.toggleStyle(.switch)135 case .checkbox:136 #if os(macOS)137 toggle.toggleStyle(.checkbox)138 #else139 toggle.toggleStyle(.switch)140 #endif141 }142 }143}144