// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassNavigation/Navigation/GlassTabBar.swift // Module : SPGlassNavigation // 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 /// When the system tab bar gets out of the way of the content. Only iPhone minimises its tab bar. public enum GlassTabBarMinimizeBehavior: String, CaseIterable, Sendable { case automatic, onScrollDown, onScrollUp, never } /// The system `TabView`, driven by values: typed selection, badges, tabs from a collection, one navigation /// stack per tab and deep links. /// /// ```swift /// @State private var state = GlassTabNavigationState(selection: .home) /// /// GlassTabBar(tabs: tabs, state: $state) { tab in /// RootView(tab: tab) /// } destination: { route in /// DetailView(route: route) /// } /// .onOpenURL { url in /// if let destination = router.destination(for: url) { state.open(destination) } /// } /// ``` /// /// The system draws the bar: a floating Liquid Glass tab bar on iPhone, a tab bar that turns into a sidebar on /// iPad and a sidebar or tab strip on macOS (`.sidebarAdaptable`). GlassKit never draws a bar of its own. public struct GlassTabBar: View { @GlassThemed private var theme private let tabs: [GlassTabItem] @Binding private var state: GlassTabNavigationState private let minimizeBehavior: GlassTabBarMinimizeBehavior private let root: (ID) -> Root private let destination: (Route) -> Destination private let accessory: Accessory /// - Parameters: /// - tabs: the tabs, in order. The list may change while the view is displayed. /// - state: selection and per-tab navigation paths. /// - minimizeBehavior: tab bar minimisation on iPhone; ignored elsewhere. /// - root: root view of a tab, placed in that tab's own `NavigationStack`. /// - destination: view of a route pushed on a tab's stack. /// - bottomAccessory: view the system places above the tab bar on iOS (a mini player, for example). public init( tabs: [GlassTabItem], state: Binding>, minimizeBehavior: GlassTabBarMinimizeBehavior = .automatic, @ViewBuilder root: @escaping (ID) -> Root, @ViewBuilder destination: @escaping (Route) -> Destination, @ViewBuilder bottomAccessory: () -> Accessory ) { self.tabs = tabs self._state = state self.minimizeBehavior = minimizeBehavior self.root = root self.destination = destination self.accessory = bottomAccessory() } public var body: some View { TabView(selection: $state.selection) { ForEach(tabs) { tab in Tab(tab.title, systemImage: tab.systemImage, value: tab.id, role: tab.isSearch ? .search : nil) { NavigationStack(path: pathBinding(for: tab.id)) { root(tab.id) .navigationDestination(for: Route.self) { route in destination(route) } } } .badge(GlassTabBadgeFormatter.string(tab.badge).map { Text(verbatim: $0) }) } } .tabViewStyle(.sidebarAdaptable) .tint(theme.color(.primary)) .modifier(GlassTabBarPlatformModifier(minimizeBehavior: minimizeBehavior, accessory: accessory)) } private func pathBinding(for tab: ID) -> Binding<[Route]> { Binding { state.path(for: tab) } set: { newValue in state.setPath(newValue, for: tab) } } } extension GlassTabBar where Accessory == EmptyView { /// Creates a tab bar without a bottom accessory. public init( tabs: [GlassTabItem], state: Binding>, minimizeBehavior: GlassTabBarMinimizeBehavior = .automatic, @ViewBuilder root: @escaping (ID) -> Root, @ViewBuilder destination: @escaping (Route) -> Destination ) { self.init(tabs: tabs, state: state, minimizeBehavior: minimizeBehavior, root: root, destination: destination) { EmptyView() } } } /// Text of a tab badge, separated from the view so that it can be tested. public enum GlassTabBadgeFormatter { /// The badge as displayed: a localised number, a trimmed text, or `nil` for no badge. public static func string(_ badge: GlassTabBadge?, locale: Locale = .current) -> String? { switch badge { case .none: return nil case .count(let count): return count > 0 ? count.formatted(.number.locale(locale)) : nil case .text(let text): let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines) return trimmed.isEmpty ? nil : trimmed } } } private struct GlassTabBarPlatformModifier: ViewModifier { let minimizeBehavior: GlassTabBarMinimizeBehavior let accessory: Accessory func body(content: Content) -> some View { #if os(iOS) if Accessory.self == EmptyView.self { content.tabBarMinimizeBehavior(systemBehavior) } else { content .tabBarMinimizeBehavior(systemBehavior) .tabViewBottomAccessory { accessory } } #else content #endif } #if os(iOS) private var systemBehavior: TabBarMinimizeBehavior { switch minimizeBehavior { case .automatic: .automatic case .onScrollDown: .onScrollDown case .onScrollUp: .onScrollUp case .never: .never } } #endif }