// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassForms/Selection/GlassSegmentedControl.swift // Module : SPGlassForms // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-02 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// Decides whether a set of options is shown as segments or falls back to a menu. Pure. public enum GlassSegmentedLayout { public enum Presentation: String, Sendable { /// Segments when they fit the width, a menu when they do not. case segmentedIfFits /// Always a menu. case menu } /// Most options a segmented control shows before the segments become too small to read or touch. public static let defaultMaximumSegments = 5 /// The presentation for a set of options. /// /// A menu is used straight away when the text size is an accessibility size (segments cannot wrap, so /// their titles would be truncated), when there are more options than `maximumSegments`, or when there /// is at most one option. Otherwise the segments are tried and the available width decides. public static func presentation(optionCount: Int, isAccessibilitySize: Bool, maximumSegments: Int = defaultMaximumSegments) -> Presentation { if isAccessibilitySize || optionCount > maximumSegments || optionCount < 2 { return .menu } return .segmentedIfFits } } /// The system segmented `Picker`, which turns into a menu when its titles do not fit. /// /// ```swift /// GlassSegmentedControl("View", selection: $mode, options: Mode.allCases, title: \.title) /// ``` /// /// The system draws the segments and their Liquid Glass selection. The title is the accessibility label of /// the control; segmented pickers do not display it. public struct GlassSegmentedControl: View { @GlassThemed private var theme @Environment(\.dynamicTypeSize) private var dynamicTypeSize private let title: Text @Binding private var selection: Option private let options: [Option] private let maximumSegments: Int private let optionTitle: (Option) -> String /// - Parameters: /// - title: name of the control, read by VoiceOver and shown by the menu fallback on macOS. /// - maximumSegments: above this number of options the control is a menu. /// - optionTitle: title of an option, already localised; a closure or a key path (`title: \\.name`). public init( _ title: LocalizedStringKey, selection: Binding