Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMaps/Maps/GlassMapControls.swift8// Module : SPGlassMaps9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0313//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 SPGlassTokens44import SPGlassThemes45import SPGlassEffects46import SPGlassComponents4748/// Zoom buttons, and any extra map action, grouped in one glass cluster.49///50/// ```swift51/// GlassMapControls(canZoomIn: zoom < 20, canZoomOut: zoom > 2) {52/// zoom += 153/// } onZoomOut: {54/// zoom -= 155/// } extra: {56/// GlassCompass(heading: heading) { heading = 0 }57/// GlassLocationButton(state: locationState) { locate() }58/// }59/// ```60///61/// The component knows nothing about the map: it calls the closures it is given. The two zoom buttons share one62/// glass pill, split by a hairline; the extra controls keep their own round surfaces, and everything sits in a63/// ``SPGlassEffects/GlassGroup`` so that the system renders the glass together.64public struct GlassMapControls<Extra: View>: View {65 @GlassThemed private var theme66 private let canZoomIn: Bool67 private let canZoomOut: Bool68 private let axis: Axis69 private let size: GlassControlSize70 private let onZoomIn: @MainActor () -> Void71 private let onZoomOut: @MainActor () -> Void72 private let extra: Extra7374 /// - Parameters:75 /// - canZoomIn: false at the closest zoom; the button is then disabled.76 /// - canZoomOut: false at the farthest zoom.77 /// - axis: `.vertical` (default, along a map edge) or `.horizontal`.78 /// - extra: further controls placed before the zoom buttons, typically a compass and a location button.79 public init(80 canZoomIn: Bool = true,81 canZoomOut: Bool = true,82 axis: Axis = .vertical,83 size: GlassControlSize = .regular,84 onZoomIn: @escaping @MainActor () -> Void,85 onZoomOut: @escaping @MainActor () -> Void,86 @ViewBuilder extra: () -> Extra87 ) {88 self.canZoomIn = canZoomIn89 self.canZoomOut = canZoomOut90 self.axis = axis91 self.size = size92 self.onZoomIn = onZoomIn93 self.onZoomOut = onZoomOut94 self.extra = extra()95 }9697 public var body: some View {98 let layout = axis == .vertical99 ? AnyLayout(VStackLayout(spacing: theme.spacing.xs))100 : AnyLayout(HStackLayout(spacing: theme.spacing.xs))101 GlassGroup(spacing: theme.spacing.xs) {102 layout {103 extra104 zoomPill105 }106 }107 .accessibilityElement(children: .contain)108 .accessibilityLabel(Text(GlassL10n.string(.mapControls)))109 }110111 private var zoomPill: some View {112 let side = GlassIconButtonMetrics.hitTarget(size, metrics: theme.metrics)113 let pair = axis == .vertical114 ? AnyLayout(VStackLayout(spacing: 0))115 : AnyLayout(HStackLayout(spacing: 0))116 return pair {117 zoomButton("plus", label: .zoomIn, side: side, isEnabled: canZoomIn, action: onZoomIn)118 Rectangle()119 .fill(theme.color(.separator))120 .frame(width: axis == .vertical ? side * 0.5 : 1, height: axis == .vertical ? 1 : side * 0.5)121 .accessibilityHidden(true)122 zoomButton("minus", label: .zoomOut, side: side, isEnabled: canZoomOut, action: onZoomOut)123 }124 .glassSurface(.regular.tinted(.none), in: RoundedRectangle(cornerRadius: side / 2, style: .continuous))125 }126127 private func zoomButton(_ systemImage: String, label: GlassL10n.Key, side: CGFloat, isEnabled: Bool, action: @escaping @MainActor () -> Void) -> some View {128 Button(action: action) {129 Image(systemName: systemImage)130 .font(.system(size: theme.metrics.iconSize(size), weight: .medium))131 .foregroundStyle(theme.color(isEnabled ? .textPrimary : .textSecondary).opacity(isEnabled ? 1 : 0.45))132 .frame(width: side, height: side)133 .contentShape(Rectangle())134 }135 .buttonStyle(GlassMapPressStyle())136 .disabled(!isEnabled)137 .accessibilityLabel(Text(GlassL10n.string(label)))138 .help(GlassL10n.string(label))139 }140}141142/// Dims a symbol while it is pressed. Used by the controls that share a glass surface.143struct GlassMapPressStyle: ButtonStyle {144 func makeBody(configuration: Configuration) -> some View {145 configuration.label.opacity(configuration.isPressed ? 0.45 : 1)146 }147}148149extension GlassMapControls where Extra == EmptyView {150 /// Zoom buttons only.151 public init(152 canZoomIn: Bool = true,153 canZoomOut: Bool = true,154 axis: Axis = .vertical,155 size: GlassControlSize = .regular,156 onZoomIn: @escaping @MainActor () -> Void,157 onZoomOut: @escaping @MainActor () -> Void158 ) {159 self.init(canZoomIn: canZoomIn, canZoomOut: canZoomOut, axis: axis, size: size, onZoomIn: onZoomIn, onZoomOut: onZoomOut) { EmptyView() }160 }161}162163extension GlassMapControls {164 /// Zoom buttons wired to a ``GlassMapEngine``, with extra controls.165 public init(166 engine: some GlassMapEngine,167 axis: Axis = .vertical,168 size: GlassControlSize = .regular,169 @ViewBuilder extra: () -> Extra170 ) {171 self.init(172 canZoomIn: engine.canZoomIn,173 canZoomOut: engine.canZoomOut,174 axis: axis,175 size: size,176 onZoomIn: { engine.zoomIn() },177 onZoomOut: { engine.zoomOut() },178 extra: extra179 )180 }181}182183extension GlassMapControls where Extra == GlassCompass {184 /// Zoom buttons and a compass, all wired to a ``GlassMapEngine``.185 public init(engine: some GlassMapEngine, axis: Axis = .vertical, size: GlassControlSize = .regular) {186 self.init(engine: engine, axis: axis, size: size) {187 GlassCompass(heading: engine.heading, size: size) { engine.resetNorth() }188 }189 }190}191