Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassForms/Selection/GlassPicker.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/// Presentation of a ``GlassPicker``.47public enum GlassPickerPresentation: String, CaseIterable, Sendable {48 /// A pop-up menu. The default on every platform.49 case menu50 /// A wheel. iOS and iPadOS only; macOS uses a menu.51 case wheel52 /// Every option visible: radio buttons on macOS, an inline list on iOS and iPadOS.53 case inline5455 /// The presentation actually used on a platform.56 public func resolved(for platform: GlassPlatform = .current) -> Resolved {57 switch (self, platform) {58 case (.menu, _): .menu59 case (.wheel, .macOS): .menu60 case (.wheel, _): .wheel61 case (.inline, .macOS): .radioGroup62 case (.inline, _): .inline63 }64 }6566 /// A system picker style.67 public enum Resolved: String, CaseIterable, Sendable {68 case menu, wheel, inline, radioGroup69 }70}7172/// The system `Picker` over any `Hashable` options.73///74/// ```swift75/// GlassPicker("Currency", selection: $currency, options: Currency.allCases, title: \.name)76/// GlassPicker("Priority", selection: $priority, options: Priority.allCases, presentation: .inline) { priority in77/// Label(priority.title, systemImage: priority.symbol)78/// }79/// ```80///81/// The system draws the control. The presentation adapts to the platform: a wheel exists only on iOS and82/// iPadOS, radio buttons only on macOS.83public struct GlassPicker<Option: Hashable, OptionLabel: View>: View {84 @GlassThemed private var theme85 private let title: Text86 @Binding private var selection: Option87 private let options: [Option]88 private let presentation: GlassPickerPresentation89 private let optionLabel: (Option) -> OptionLabel9091 /// Creates a picker with a custom label for each option.92 public init(93 _ title: LocalizedStringKey,94 selection: Binding<Option>,95 options: [Option],96 presentation: GlassPickerPresentation = .menu,97 @ViewBuilder optionLabel: @escaping (Option) -> OptionLabel98 ) {99 self.title = Text(title)100 self._selection = selection101 self.options = options102 self.presentation = presentation103 self.optionLabel = optionLabel104 }105106 public var body: some View {107 styled(108 Picker(selection: $selection) {109 ForEach(options, id: \.self) { option in110 optionLabel(option).tag(option)111 }112 } label: {113 title114 }115 )116 .tint(theme.color(.primary))117 }118119 @ViewBuilder120 private func styled(_ picker: some View) -> some View {121 switch presentation.resolved() {122 case .menu:123 picker.pickerStyle(.menu)124 case .inline:125 picker.pickerStyle(.inline)126 case .wheel:127 #if os(iOS)128 picker.pickerStyle(.wheel)129 #else130 picker.pickerStyle(.menu)131 #endif132 case .radioGroup:133 #if os(macOS)134 picker.pickerStyle(.radioGroup)135 #else136 picker.pickerStyle(.inline)137 #endif138 }139 }140}141142extension GlassPicker where OptionLabel == Text {143 /// Creates a picker whose options are named by a closure or a key path (`title: \\.name`).144 public init(145 _ title: LocalizedStringKey,146 selection: Binding<Option>,147 options: [Option],148 presentation: GlassPickerPresentation = .menu,149 title optionTitle: @escaping (Option) -> String150 ) {151 self.init(title, selection: selection, options: options, presentation: presentation) { option in152 Text(verbatim: optionTitle(option))153 }154 }155}156157extension GlassPicker where Option: CustomStringConvertible, OptionLabel == Text {158 /// Creates a picker over values that describe themselves (`String`, `Int`…).159 public init(160 _ title: LocalizedStringKey,161 selection: Binding<Option>,162 options: [Option],163 presentation: GlassPickerPresentation = .menu164 ) {165 self.init(title, selection: selection, options: options, presentation: presentation) { option in166 Text(verbatim: option.description)167 }168 }169}170