SPB Git

Repository Public MIT

GlassPicker.swift 6.2 KB 179 lines swift

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-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/// Presentation of a ``GlassPicker``.44public enum GlassPickerPresentation: String, CaseIterable, Sendable {45    /// A pop-up menu. The default on every platform.46    case menu47    /// A wheel. iOS and iPadOS only; macOS uses a menu.48    case wheel49    /// Every option visible: radio buttons on macOS, an inline list on iOS and iPadOS.50    case inline5152    /// The presentation actually used on a platform.53    public func resolved(for platform: GlassPlatform = .current) -> Resolved {54        switch (self, platform) {55        case (.menu, _): .menu56        case (.wheel, .macOS): .menu57        case (.wheel, _): .wheel58        case (.inline, .macOS): .radioGroup59        case (.inline, _): .inline60        }61    }6263    /// A system picker style.64    public enum Resolved: String, CaseIterable, Sendable {65        case menu, wheel, inline, radioGroup66    }67}6869/// The system `Picker` over any `Hashable` options.70///71/// ```swift72/// GlassPicker("Currency", selection: $currency, options: Currency.allCases, title: \.name)73/// GlassPicker("Priority", selection: $priority, options: Priority.allCases, presentation: .inline) { priority in74///     Label(priority.title, systemImage: priority.symbol)75/// }76/// ```77///78/// The system draws the control. The presentation adapts to the platform: a wheel exists only on iOS and79/// iPadOS, radio buttons only on macOS.80public struct GlassPicker<Option: Hashable, OptionLabel: View>: View {81    @GlassThemed private var theme82    private let title: Text83    @Binding private var selection: Option84    private let options: [Option]85    private let presentation: GlassPickerPresentation86    private let optionLabel: (Option) -> OptionLabel8788    /// Creates a picker with a custom label for each option.89    public init(90        _ title: LocalizedStringKey,91        selection: Binding<Option>,92        options: [Option],93        presentation: GlassPickerPresentation = .menu,94        @ViewBuilder optionLabel: @escaping (Option) -> OptionLabel95    ) {96        self.title = Text(title)97        self._selection = selection98        self.options = options99        self.presentation = presentation100        self.optionLabel = optionLabel101    }102103    public var body: some View {104        styled(105            Picker(selection: $selection) {106                ForEach(options, id: \.self) { option in107                    optionLabel(option).tag(option)108                }109            } label: {110                title111            }112        )113        .tint(theme.color(.primary))114    }115116    @ViewBuilder117    private func styled(_ picker: some View) -> some View {118        switch presentation.resolved() {119        case .menu:120            #if os(macOS)121            picker.pickerStyle(.menu)122            #else123            // Outside a form, the system menu picker of iOS shows its value only. The title is laid out124            // here so that the row reads "Plan … Starter" wherever the picker is used.125            LabeledContent {126                picker.pickerStyle(.menu).labelsHidden()127            } label: {128                title129                    .font(theme.font(.body))130                    .foregroundStyle(theme.color(.textPrimary))131            }132            #endif133        case .inline:134            picker.pickerStyle(.inline)135        case .wheel:136            #if os(iOS)137            picker.pickerStyle(.wheel)138            #else139            picker.pickerStyle(.menu)140            #endif141        case .radioGroup:142            #if os(macOS)143            picker.pickerStyle(.radioGroup)144            #else145            picker.pickerStyle(.inline)146            #endif147        }148    }149}150151extension GlassPicker where OptionLabel == Text {152    /// Creates a picker whose options are named by a closure or a key path (`title: \\.name`).153    public init(154        _ title: LocalizedStringKey,155        selection: Binding<Option>,156        options: [Option],157        presentation: GlassPickerPresentation = .menu,158        title optionTitle: @escaping (Option) -> String159    ) {160        self.init(title, selection: selection, options: options, presentation: presentation) { option in161            Text(verbatim: optionTitle(option))162        }163    }164}165166extension GlassPicker where Option: CustomStringConvertible, OptionLabel == Text {167    /// Creates a picker over values that describe themselves (`String`, `Int`…).168    public init(169        _ title: LocalizedStringKey,170        selection: Binding<Option>,171        options: [Option],172        presentation: GlassPickerPresentation = .menu173    ) {174        self.init(title, selection: selection, options: options, presentation: presentation) { option in175            Text(verbatim: option.description)176        }177    }178}179
Files main
Menu

Theme