Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassForms/Selection/GlassSlider.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/// Arithmetic of a slider. Pure.44public enum GlassSliderMath {45 /// `value` brought inside `bounds` and, with a step, onto the nearest step counted from the lower bound.46 public static func snapped(_ value: Double, in bounds: ClosedRange<Double>, step: Double?) -> Double {47 let clamped = min(max(value, bounds.lowerBound), bounds.upperBound)48 guard let step, step > 0 else { return clamped }49 let index = ((clamped - bounds.lowerBound) / step).rounded()50 return min(GlassStepperMath.value(atStep: index, from: bounds.lowerBound, step: step), bounds.upperBound)51 }5253 /// Position of `value` inside `bounds`, from 0 to 1. An empty range gives 0.54 public static func fraction(_ value: Double, in bounds: ClosedRange<Double>) -> Double {55 let span = bounds.upperBound - bounds.lowerBound56 guard span > 0 else { return 0 }57 return min(max((value - bounds.lowerBound) / span, 0), 1)58 }59}6061/// The system `Slider` with a title, a readable value and bound labels.62///63/// ```swift64/// GlassSlider("Brightness", value: $brightness, in: 0...1, format: .percent.precision(.fractionLength(0)))65/// GlassSlider("Temperature", value: $celsius, in: 16...28, step: 0.5) { value in66/// Measurement(value: value, unit: UnitTemperature.celsius).formatted()67/// }68/// ```69///70/// The system draws the slider and its Liquid Glass thumb, and makes it adjustable with VoiceOver and the71/// keyboard. GlassKit shows the formatted value next to the title and gives the same text to VoiceOver, so72/// that "50 %" is read rather than "0.5".73public struct GlassSlider: View {74 @GlassThemed private var theme75 private let title: Text76 @Binding private var value: Double77 private let bounds: ClosedRange<Double>78 private let step: Double?79 private let showsValue: Bool80 private let showsBoundLabels: Bool81 private let format: (Double) -> String82 private let onEditingChanged: @MainActor (Bool) -> Void8384 /// Creates a slider whose value is formatted by a closure.85 /// - Parameters:86 /// - step: distance between two values; `nil` is continuous.87 /// - showsValue: shows the formatted value next to the title.88 /// - showsBoundLabels: shows the formatted bounds at both ends of the track.89 /// - onEditingChanged: called when a drag starts (`true`) and ends (`false`).90 /// - format: text of a value, built with `FormatStyle` APIs so that it follows the locale.91 public init(92 _ title: LocalizedStringKey,93 value: Binding<Double>,94 in bounds: ClosedRange<Double> = 0...1,95 step: Double? = nil,96 showsValue: Bool = true,97 showsBoundLabels: Bool = false,98 onEditingChanged: @escaping @MainActor (Bool) -> Void = { _ in },99 format: @escaping (Double) -> String100 ) {101 self.title = Text(title)102 self._value = value103 self.bounds = bounds104 self.step = step105 self.showsValue = showsValue106 self.showsBoundLabels = showsBoundLabels107 self.onEditingChanged = onEditingChanged108 self.format = format109 }110111 /// Creates a slider whose value is formatted by a `FormatStyle`.112 public init<F: FormatStyle>(113 _ title: LocalizedStringKey,114 value: Binding<Double>,115 in bounds: ClosedRange<Double> = 0...1,116 step: Double? = nil,117 format: F,118 showsValue: Bool = true,119 showsBoundLabels: Bool = false,120 onEditingChanged: @escaping @MainActor (Bool) -> Void = { _ in }121 ) where F.FormatInput == Double, F.FormatOutput == String {122 self.init(title, value: value, in: bounds, step: step, showsValue: showsValue, showsBoundLabels: showsBoundLabels, onEditingChanged: onEditingChanged) { format.format($0) }123 }124125 /// Creates a slider that shows its value as a plain number with up to two decimals.126 public init(127 _ title: LocalizedStringKey,128 value: Binding<Double>,129 in bounds: ClosedRange<Double> = 0...1,130 step: Double? = nil,131 showsValue: Bool = true,132 showsBoundLabels: Bool = false,133 onEditingChanged: @escaping @MainActor (Bool) -> Void = { _ in }134 ) {135 self.init(title, value: value, in: bounds, step: step, format: FloatingPointFormatStyle<Double>.number.precision(.fractionLength(0...2)), showsValue: showsValue, showsBoundLabels: showsBoundLabels, onEditingChanged: onEditingChanged)136 }137138 public var body: some View {139 VStack(alignment: .leading, spacing: theme.spacing.xxs) {140 ViewThatFits(in: .horizontal) {141 HStack(alignment: .firstTextBaseline) {142 titleText143 Spacer(minLength: theme.spacing.sm)144 valueText145 }146 VStack(alignment: .leading, spacing: theme.spacing.xxs) {147 titleText148 valueText149 }150 }151 .accessibilityHidden(true)152 slider153 .tint(theme.color(.primary))154 .accessibilityValue(Text(verbatim: format(value)))155 }156 }157158 private var titleText: some View {159 title160 .font(theme.font(.body))161 .foregroundStyle(theme.color(.textPrimary))162 }163164 @ViewBuilder165 private var valueText: some View {166 if showsValue {167 Text(verbatim: format(value))168 .font(theme.font(.body).monospacedDigit())169 .foregroundStyle(theme.color(.textSecondary))170 }171 }172173 @ViewBuilder174 private var slider: some View {175 let editing: (Bool) -> Void = { isEditing in onEditingChanged(isEditing) }176 if let step, step > 0 {177 if showsBoundLabels {178 Slider(value: $value, in: bounds, step: step) { title } minimumValueLabel: { boundLabel(bounds.lowerBound) } maximumValueLabel: { boundLabel(bounds.upperBound) } onEditingChanged: { editing($0) }179 } else {180 Slider(value: $value, in: bounds, step: step) { title } onEditingChanged: { editing($0) }181 }182 } else if showsBoundLabels {183 Slider(value: $value, in: bounds) { title } minimumValueLabel: { boundLabel(bounds.lowerBound) } maximumValueLabel: { boundLabel(bounds.upperBound) } onEditingChanged: { editing($0) }184 } else {185 Slider(value: $value, in: bounds) { title } onEditingChanged: { editing($0) }186 }187 }188189 private func boundLabel(_ bound: Double) -> some View {190 Text(verbatim: format(bound))191 .font(theme.font(.caption))192 .foregroundStyle(theme.color(.textSecondary))193 }194}195