SPB Git

Repository Public MIT

GlassPicker.swift 5.7 KB 167 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-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/// 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            picker.pickerStyle(.menu)121        case .inline:122            picker.pickerStyle(.inline)123        case .wheel:124            #if os(iOS)125            picker.pickerStyle(.wheel)126            #else127            picker.pickerStyle(.menu)128            #endif129        case .radioGroup:130            #if os(macOS)131            picker.pickerStyle(.radioGroup)132            #else133            picker.pickerStyle(.inline)134            #endif135        }136    }137}138139extension GlassPicker where OptionLabel == Text {140    /// Creates a picker whose options are named by a closure or a key path (`title: \\.name`).141    public init(142        _ title: LocalizedStringKey,143        selection: Binding<Option>,144        options: [Option],145        presentation: GlassPickerPresentation = .menu,146        title optionTitle: @escaping (Option) -> String147    ) {148        self.init(title, selection: selection, options: options, presentation: presentation) { option in149            Text(verbatim: optionTitle(option))150        }151    }152}153154extension GlassPicker where Option: CustomStringConvertible, OptionLabel == Text {155    /// Creates a picker over values that describe themselves (`String`, `Int`…).156    public init(157        _ title: LocalizedStringKey,158        selection: Binding<Option>,159        options: [Option],160        presentation: GlassPickerPresentation = .menu161    ) {162        self.init(title, selection: selection, options: options, presentation: presentation) { option in163            Text(verbatim: option.description)164        }165    }166}167
Files main
Menu

Theme