Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassNavigation/Navigation/GlassToolbarAction.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/// How strongly an action resists being moved to the overflow menu.45public enum GlassToolbarActionPriority: Int, Comparable, CaseIterable, Sendable {46 /// Moves to the overflow menu first.47 case low = 048 case normal = 149 /// Stays visible as long as there is room for anything.50 case high = 25152 public static func < (lhs: GlassToolbarActionPriority, rhs: GlassToolbarActionPriority) -> Bool {53 lhs.rawValue < rhs.rawValue54 }55}5657/// One toolbar or navigation bar action, described by values.58///59/// ```swift60/// GlassToolbarAction(id: "share", title: "Share", systemImage: "square.and.arrow.up",61/// shortcut: KeyboardShortcut("s", modifiers: [.command, .shift])) { share() }62/// ```63public struct GlassToolbarAction: Identifiable {64 public let id: String65 /// Title: the accessibility label of the button, its tooltip and its title in the overflow menu.66 public var title: Text67 /// SF Symbol shown in the bar.68 public var systemImage: String69 public var role: ButtonRole?70 public var shortcut: KeyboardShortcut?71 public var isEnabled: Bool72 public var priority: GlassToolbarActionPriority73 public var action: @MainActor () -> Void7475 /// Creates an action with a title looked up in the host application's strings.76 public init(77 id: String,78 title: LocalizedStringKey,79 systemImage: String,80 role: ButtonRole? = nil,81 shortcut: KeyboardShortcut? = nil,82 isEnabled: Bool = true,83 priority: GlassToolbarActionPriority = .normal,84 action: @escaping @MainActor () -> Void85 ) {86 self.init(id: id, text: Text(title), systemImage: systemImage, role: role, shortcut: shortcut, isEnabled: isEnabled, priority: priority, action: action)87 }8889 /// Creates an action with a title that is already a string.90 public init<S: StringProtocol>(91 id: String,92 verbatim title: S,93 systemImage: String,94 role: ButtonRole? = nil,95 shortcut: KeyboardShortcut? = nil,96 isEnabled: Bool = true,97 priority: GlassToolbarActionPriority = .normal,98 action: @escaping @MainActor () -> Void99 ) {100 self.init(id: id, text: Text(verbatim: String(title)), systemImage: systemImage, role: role, shortcut: shortcut, isEnabled: isEnabled, priority: priority, action: action)101 }102103 /// Creates an action with a `Text` title.104 public init(105 id: String,106 text: Text,107 systemImage: String,108 role: ButtonRole? = nil,109 shortcut: KeyboardShortcut? = nil,110 isEnabled: Bool = true,111 priority: GlassToolbarActionPriority = .normal,112 action: @escaping @MainActor () -> Void113 ) {114 self.id = id115 self.title = text116 self.systemImage = systemImage117 self.role = role118 self.shortcut = shortcut119 self.isEnabled = isEnabled120 self.priority = priority121 self.action = action122 }123}124125/// Decides which actions stay in a bar and which move to the overflow menu. Pure.126public enum GlassToolbarLayout {127 /// The result of ``split(_:capacity:)``: indices into the original list, in their original order.128 public struct Split: Equatable, Sendable {129 public let visible: [Int]130 public let overflow: [Int]131132 public var hasOverflow: Bool { !overflow.isEmpty }133 }134135 /// Splits `priorities.count` actions for a bar with room for `capacity` controls.136 ///137 /// When everything fits, everything is visible. Otherwise the menu button takes one of the places and the138 /// remaining `capacity - 1` go to the actions of highest priority, the earliest first among equals.139 /// The order of the original list is kept on both sides. A capacity below one moves everything to the menu.140 public static func split(_ priorities: [GlassToolbarActionPriority], capacity: Int) -> Split {141 let count = priorities.count142 if count <= capacity { return Split(visible: Array(0..<count), overflow: []) }143 let slots = max(capacity - 1, 0)144 let ranked = priorities.indices.sorted { lhs, rhs in145 priorities[lhs] != priorities[rhs] ? priorities[lhs] > priorities[rhs] : lhs < rhs146 }147 let kept = Set(ranked.prefix(slots))148 return Split(149 visible: priorities.indices.filter { kept.contains($0) },150 overflow: priorities.indices.filter { !kept.contains($0) }151 )152 }153154 /// Splits a list of actions with the same rule, returning the actions themselves.155 public static func split(_ actions: [GlassToolbarAction], capacity: Int) -> (visible: [GlassToolbarAction], overflow: [GlassToolbarAction]) {156 let result = split(actions.map(\.priority), capacity: capacity)157 return (result.visible.map { actions[$0] }, result.overflow.map { actions[$0] })158 }159160 /// Number of controls a group shows before overflowing, when the host gives no capacity.161 ///162 /// A compact width or an accessibility text size leaves room for two controls; otherwise five.163 public static func defaultCapacity(isCompactWidth: Bool, isAccessibilitySize: Bool) -> Int {164 isCompactWidth || isAccessibilitySize ? 2 : 5165 }166}167168/// The controls of a list of actions: buttons, then a "More actions" menu for those that do not fit.169struct GlassToolbarActionControls: View {170 let actions: [GlassToolbarAction]171 let capacity: Int172173 var body: some View {174 let parts = GlassToolbarLayout.split(actions, capacity: capacity)175 ForEach(parts.visible) { action in176 GlassToolbarActionButton(action: action)177 .help(action.title)178 }179 if !parts.overflow.isEmpty {180 Menu {181 ForEach(parts.overflow) { action in182 GlassToolbarActionButton(action: action)183 }184 } label: {185 Label(GlassL10n.string(.moreActions), systemImage: "ellipsis")186 }187 }188 }189}190191/// A button for one action. In a bar the system shows the symbol; in a menu, the title and the symbol.192struct GlassToolbarActionButton: View {193 let action: GlassToolbarAction194195 var body: some View {196 Button(role: action.role, action: action.action) {197 Label { action.title } icon: { Image(systemName: action.systemImage) }198 }199 .keyboardShortcut(action.shortcut)200 .disabled(!action.isEnabled)201 }202}203