Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassNavigation/Navigation/GlassTabBar.swift8// Module : SPGlassNavigation9// 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 SPGlassThemes4445/// When the system tab bar gets out of the way of the content. Only iPhone minimises its tab bar.46public enum GlassTabBarMinimizeBehavior: String, CaseIterable, Sendable {47 case automatic, onScrollDown, onScrollUp, never48}4950/// The system `TabView`, driven by values: typed selection, badges, tabs from a collection, one navigation51/// stack per tab and deep links.52///53/// ```swift54/// @State private var state = GlassTabNavigationState<AppTab, AppRoute>(selection: .home)55///56/// GlassTabBar(tabs: tabs, state: $state) { tab in57/// RootView(tab: tab)58/// } destination: { route in59/// DetailView(route: route)60/// }61/// .onOpenURL { url in62/// if let destination = router.destination(for: url) { state.open(destination) }63/// }64/// ```65///66/// The system draws the bar: a floating Liquid Glass tab bar on iPhone, a tab bar that turns into a sidebar on67/// iPad and a sidebar or tab strip on macOS (`.sidebarAdaptable`). GlassKit never draws a bar of its own.68public struct GlassTabBar<ID: Hashable & Sendable, Route: Hashable & Sendable, Root: View, Destination: View, Accessory: View>: View {69 @GlassThemed private var theme70 private let tabs: [GlassTabItem<ID>]71 @Binding private var state: GlassTabNavigationState<ID, Route>72 private let minimizeBehavior: GlassTabBarMinimizeBehavior73 private let root: (ID) -> Root74 private let destination: (Route) -> Destination75 private let accessory: Accessory7677 /// - Parameters:78 /// - tabs: the tabs, in order. The list may change while the view is displayed.79 /// - state: selection and per-tab navigation paths.80 /// - minimizeBehavior: tab bar minimisation on iPhone; ignored elsewhere.81 /// - root: root view of a tab, placed in that tab's own `NavigationStack`.82 /// - destination: view of a route pushed on a tab's stack.83 /// - bottomAccessory: view the system places above the tab bar on iOS (a mini player, for example).84 public init(85 tabs: [GlassTabItem<ID>],86 state: Binding<GlassTabNavigationState<ID, Route>>,87 minimizeBehavior: GlassTabBarMinimizeBehavior = .automatic,88 @ViewBuilder root: @escaping (ID) -> Root,89 @ViewBuilder destination: @escaping (Route) -> Destination,90 @ViewBuilder bottomAccessory: () -> Accessory91 ) {92 self.tabs = tabs93 self._state = state94 self.minimizeBehavior = minimizeBehavior95 self.root = root96 self.destination = destination97 self.accessory = bottomAccessory()98 }99100 public var body: some View {101 TabView(selection: $state.selection) {102 ForEach(tabs) { tab in103 Tab(tab.title, systemImage: tab.systemImage, value: tab.id, role: tab.isSearch ? .search : nil) {104 NavigationStack(path: pathBinding(for: tab.id)) {105 root(tab.id)106 .navigationDestination(for: Route.self) { route in107 destination(route)108 }109 }110 }111 .badge(GlassTabBadgeFormatter.string(tab.badge).map { Text(verbatim: $0) })112 }113 }114 .tabViewStyle(.sidebarAdaptable)115 .tint(theme.color(.primary))116 .modifier(GlassTabBarPlatformModifier(minimizeBehavior: minimizeBehavior, accessory: accessory))117 }118119 private func pathBinding(for tab: ID) -> Binding<[Route]> {120 Binding {121 state.path(for: tab)122 } set: { newValue in123 state.setPath(newValue, for: tab)124 }125 }126}127128extension GlassTabBar where Accessory == EmptyView {129 /// Creates a tab bar without a bottom accessory.130 public init(131 tabs: [GlassTabItem<ID>],132 state: Binding<GlassTabNavigationState<ID, Route>>,133 minimizeBehavior: GlassTabBarMinimizeBehavior = .automatic,134 @ViewBuilder root: @escaping (ID) -> Root,135 @ViewBuilder destination: @escaping (Route) -> Destination136 ) {137 self.init(tabs: tabs, state: state, minimizeBehavior: minimizeBehavior, root: root, destination: destination) {138 EmptyView()139 }140 }141}142143/// Text of a tab badge, separated from the view so that it can be tested.144public enum GlassTabBadgeFormatter {145 /// The badge as displayed: a localised number, a trimmed text, or `nil` for no badge.146 public static func string(_ badge: GlassTabBadge?, locale: Locale = .current) -> String? {147 switch badge {148 case .none:149 return nil150 case .count(let count):151 return count > 0 ? count.formatted(.number.locale(locale)) : nil152 case .text(let text):153 let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)154 return trimmed.isEmpty ? nil : trimmed155 }156 }157}158159private struct GlassTabBarPlatformModifier<Accessory: View>: ViewModifier {160 let minimizeBehavior: GlassTabBarMinimizeBehavior161 let accessory: Accessory162163 func body(content: Content) -> some View {164 #if os(iOS)165 if Accessory.self == EmptyView.self {166 content.tabBarMinimizeBehavior(systemBehavior)167 } else {168 content169 .tabBarMinimizeBehavior(systemBehavior)170 .tabViewBottomAccessory { accessory }171 }172 #else173 content174 #endif175 }176177 #if os(iOS)178 private var systemBehavior: TabBarMinimizeBehavior {179 switch minimizeBehavior {180 case .automatic: .automatic181 case .onScrollDown: .onScrollDown182 case .onScrollUp: .onScrollUp183 case .never: .never184 }185 }186 #endif187}188