Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassNavigation/Navigation/GlassToolbar.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 SPGlassCore4344/// Where a group of actions goes. Each case maps to the system placement that fits the platform.45public enum GlassToolbarPlacement: String, CaseIterable, Sendable {46 /// The system decides.47 case automatic48 /// Leading edge of the top bar (iOS) or the navigation area of the window toolbar (macOS).49 case leading50 /// Trailing edge of the top bar (iOS) or the primary action area (macOS).51 case trailing52 /// The primary action of the view.53 case primary54 /// Secondary actions, which the system itself collects in an overflow menu when space is short.55 case secondary56 /// The bottom bar on iOS; the system's automatic placement on macOS, which has no bottom bar.57 case bottom58 /// A cancellation action of a modal view.59 case cancellation60 /// A confirmation action of a modal view.61 case confirmation6263 /// The system placement on the current platform.64 public var system: ToolbarItemPlacement {65 switch self {66 case .automatic: return .automatic67 case .primary: return .primaryAction68 case .secondary: return .secondaryAction69 case .cancellation: return .cancellationAction70 case .confirmation: return .confirmationAction71 case .leading:72 #if os(macOS)73 return .navigation74 #else75 return .topBarLeading76 #endif77 case .trailing:78 #if os(macOS)79 return .primaryAction80 #else81 return .topBarTrailing82 #endif83 case .bottom:84 #if os(macOS)85 return .automatic86 #else87 return .bottomBar88 #endif89 }90 }9192 /// Whether the system manages overflow itself for this placement, so GlassKit adds no menu of its own.93 public var systemHandlesOverflow: Bool { self == .secondary }94}9596/// A group of actions sharing one placement.97public struct GlassToolbarGroup: Identifiable {98 public let id: String99 public var placement: GlassToolbarPlacement100 public var actions: [GlassToolbarAction]101 /// Controls shown before the rest moves to a "More actions" menu. `nil` adapts to width and text size.102 public var capacity: Int?103104 public init(id: String, placement: GlassToolbarPlacement = .automatic, capacity: Int? = nil, actions: [GlassToolbarAction]) {105 self.id = id106 self.placement = placement107 self.capacity = capacity108 self.actions = actions109 }110}111112/// Toolbar content built from groups of actions: system placements, overflow menu and keyboard shortcuts.113///114/// ```swift115/// content.toolbar {116/// GlassToolbar([117/// GlassToolbarGroup(id: "edit", placement: .trailing, actions: [share, duplicate, archive, delete]),118/// GlassToolbarGroup(id: "more", placement: .secondary, actions: [export, print]),119/// ])120/// }121/// ```122///123/// The system draws the toolbar and its Liquid Glass. Actions that do not fit a group's capacity move to a124/// "More actions" menu, lowest priority first; their keyboard shortcuts stay active.125public struct GlassToolbar: ToolbarContent {126 private let groups: [GlassToolbarGroup]127128 public init(_ groups: [GlassToolbarGroup]) {129 self.groups = groups130 }131132 public var body: some ToolbarContent {133 ForEach(groups) { group in134 ToolbarItemGroup(placement: group.placement.system) {135 GlassToolbarGroupControls(group: group)136 }137 }138 }139}140141extension View {142 /// Adds groups of toolbar actions to this view. Shorthand for `.toolbar { GlassToolbar(groups) }`.143 public func glassToolbar(_ groups: [GlassToolbarGroup]) -> some View {144 toolbar { GlassToolbar(groups) }145 }146}147148private struct GlassToolbarGroupControls: View {149 @Environment(\.horizontalSizeClass) private var horizontalSizeClass150 @Environment(\.dynamicTypeSize) private var dynamicTypeSize151 let group: GlassToolbarGroup152153 var body: some View {154 GlassToolbarActionControls(actions: group.actions, capacity: capacity)155 }156157 private var capacity: Int {158 if group.placement.systemHandlesOverflow { return group.actions.count }159 return group.capacity ?? GlassToolbarLayout.defaultCapacity(160 isCompactWidth: horizontalSizeClass == .compact,161 isAccessibilitySize: dynamicTypeSize.isAccessibilitySize162 )163 }164}165