Repository Public MIT
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 SwiftUI4243/// Decides whether a set of options is shown as segments or falls back to a menu. Pure.44public enum GlassSegmentedLayout {45 public enum Presentation: String, Sendable {46 /// Segments when they fit the width, a menu when they do not.47 case segmentedIfFits48 /// Always a menu.49 case menu50 }5152 /// Most options a segmented control shows before the segments become too small to read or touch.53 public static let defaultMaximumSegments = 55455 /// The presentation for a set of options.56 ///57 /// A menu is used straight away when the text size is an accessibility size (segments cannot wrap, so58 /// their titles would be truncated), when there are more options than `maximumSegments`, or when there59 /// is at most one option. Otherwise the segments are tried and the available width decides.60 public static func presentation(optionCount: Int, isAccessibilitySize: Bool, maximumSegments: Int = defaultMaximumSegments) -> Presentation {61 if isAccessibilitySize || optionCount > maximumSegments || optionCount < 2 { return .menu }62 return .segmentedIfFits63 }64}6566/// The system segmented `Picker`, which turns into a menu when its titles do not fit.67///68/// ```swift69/// GlassSegmentedControl("View", selection: $mode, options: Mode.allCases, title: \.title)70/// ```71///72/// The system draws the segments and their Liquid Glass selection. The title is the accessibility label of73/// the control; segmented pickers do not display it.74public struct GlassSegmentedControl<Option: Hashable>: View {75 @GlassThemed private var theme76 @Environment(\.dynamicTypeSize) private var dynamicTypeSize77 private let title: Text78 @Binding private var selection: Option79 private let options: [Option]80 private let maximumSegments: Int81 private let optionTitle: (Option) -> String8283 /// - Parameters:84 /// - title: name of the control, read by VoiceOver and shown by the menu fallback on macOS.85 /// - maximumSegments: above this number of options the control is a menu.86 /// - optionTitle: title of an option, already localised; a closure or a key path (`title: \\.name`).87 public init(88 _ title: LocalizedStringKey,89 selection: Binding<Option>,90 options: [Option],91 maximumSegments: Int = GlassSegmentedLayout.defaultMaximumSegments,92 title optionTitle: @escaping (Option) -> String93 ) {94 self.title = Text(title)95 self._selection = selection96 self.options = options97 self.maximumSegments = maximumSegments98 self.optionTitle = optionTitle99 }100101 public var body: some View {102 Group {103 switch GlassSegmentedLayout.presentation(optionCount: options.count, isAccessibilitySize: dynamicTypeSize.isAccessibilitySize, maximumSegments: maximumSegments) {104 case .segmentedIfFits:105 ViewThatFits(in: .horizontal) {106 picker.pickerStyle(.segmented).fixedSize(horizontal: true, vertical: false)107 picker.pickerStyle(.menu)108 }109 case .menu:110 picker.pickerStyle(.menu)111 }112 }113 .tint(theme.color(.primary))114 }115116 private var picker: some View {117 Picker(selection: $selection) {118 ForEach(options, id: \.self) { option in119 Text(verbatim: optionTitle(option)).tag(option)120 }121 } label: {122 title123 }124 }125}126