SPB Git

Repository Public MIT

GlassSegmentedControl.swift 5.1 KB 129 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassForms/Selection/GlassSegmentedControl.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/// Decides whether a set of options is shown as segments or falls back to a menu. Pure.47public enum GlassSegmentedLayout {48    public enum Presentation: String, Sendable {49        /// Segments when they fit the width, a menu when they do not.50        case segmentedIfFits51        /// Always a menu.52        case menu53    }5455    /// Most options a segmented control shows before the segments become too small to read or touch.56    public static let defaultMaximumSegments = 55758    /// The presentation for a set of options.59    ///60    /// A menu is used straight away when the text size is an accessibility size (segments cannot wrap, so61    /// their titles would be truncated), when there are more options than `maximumSegments`, or when there62    /// is at most one option. Otherwise the segments are tried and the available width decides.63    public static func presentation(optionCount: Int, isAccessibilitySize: Bool, maximumSegments: Int = defaultMaximumSegments) -> Presentation {64        if isAccessibilitySize || optionCount > maximumSegments || optionCount < 2 { return .menu }65        return .segmentedIfFits66    }67}6869/// The system segmented `Picker`, which turns into a menu when its titles do not fit.70///71/// ```swift72/// GlassSegmentedControl("View", selection: $mode, options: Mode.allCases, title: \.title)73/// ```74///75/// The system draws the segments and their Liquid Glass selection. The title is the accessibility label of76/// the control; segmented pickers do not display it.77public struct GlassSegmentedControl<Option: Hashable>: View {78    @GlassThemed private var theme79    @Environment(\.dynamicTypeSize) private var dynamicTypeSize80    private let title: Text81    @Binding private var selection: Option82    private let options: [Option]83    private let maximumSegments: Int84    private let optionTitle: (Option) -> String8586    /// - Parameters:87    ///   - title: name of the control, read by VoiceOver and shown by the menu fallback on macOS.88    ///   - maximumSegments: above this number of options the control is a menu.89    ///   - optionTitle: title of an option, already localised; a closure or a key path (`title: \\.name`).90    public init(91        _ title: LocalizedStringKey,92        selection: Binding<Option>,93        options: [Option],94        maximumSegments: Int = GlassSegmentedLayout.defaultMaximumSegments,95        title optionTitle: @escaping (Option) -> String96    ) {97        self.title = Text(title)98        self._selection = selection99        self.options = options100        self.maximumSegments = maximumSegments101        self.optionTitle = optionTitle102    }103104    public var body: some View {105        Group {106            switch GlassSegmentedLayout.presentation(optionCount: options.count, isAccessibilitySize: dynamicTypeSize.isAccessibilitySize, maximumSegments: maximumSegments) {107            case .segmentedIfFits:108                ViewThatFits(in: .horizontal) {109                    picker.pickerStyle(.segmented).fixedSize(horizontal: true, vertical: false)110                    picker.pickerStyle(.menu)111                }112            case .menu:113                picker.pickerStyle(.menu)114            }115        }116        .tint(theme.color(.primary))117    }118119    private var picker: some View {120        Picker(selection: $selection) {121            ForEach(options, id: \.self) { option in122                Text(verbatim: optionTitle(option)).tag(option)123            }124        } label: {125            title126        }127    }128}129
Files main
Menu

Theme