Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMaps/Maps/GlassMapPin.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 SPGlassThemes4546/// What a ``GlassMapPin`` shows inside its marker.47public enum GlassMapPinContent: Equatable, Sendable {48 /// An SF Symbol.49 case symbol(String)50 /// A short text (a price, a letter, a rank). Longer texts are cut to ``GlassMapPinFormatting/textLimit`` characters.51 case text(String)52 /// The number of places a cluster stands for.53 case cluster(count: Int, cap: Int)54}5556/// Text rules of ``GlassMapPin``.57public enum GlassMapPinFormatting {58 /// Largest number of characters shown inside a marker.59 public static let textLimit = 46061 /// The text of a cluster marker: the localised count, or the cap followed by "+" above it ("99+").62 public static func clusterText(count: Int, cap: Int = 99, locale: Locale = .autoupdatingCurrent) -> String {63 let limit = max(cap, 1)64 let value = max(count, 0)65 let style = IntegerFormatStyle<Int>.number.locale(locale)66 return value > limit ? "\(limit.formatted(style))+" : value.formatted(style)67 }6869 /// A text short enough for a marker: trimmed, and cut after `textLimit` characters (grapheme clusters).70 public static func markerText(_ text: String) -> String {71 String(text.trimmingCharacters(in: .whitespacesAndNewlines).prefix(textLimit))72 }7374 /// The accessibility label: the title when there is one, otherwise what the marker shows.75 public static func accessibilityLabel(content: GlassMapPinContent, title: String?) -> String {76 let trimmedTitle = title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""77 switch content {78 case .cluster(let count, _):79 let places = GlassL10n.string(.clusterCount, max(count, 0))80 return trimmedTitle.isEmpty ? places : "\(trimmedTitle), \(places)"81 case .text(let text):82 return trimmedTitle.isEmpty ? text : trimmedTitle83 case .symbol:84 return trimmedTitle85 }86 }87}8889/// Geometry of a ``GlassMapPin``.90public enum GlassMapPinLayout {91 /// Height of the tail under the marker, for a marker `side` points high.92 public static func tailHeight(side: CGFloat) -> CGFloat {93 max(side * 0.24, 5)94 }9596 /// Scale of a selected pin.97 public static let selectedScale: CGFloat = 1.398}99100/// The outline of a pin: a capsule with a short tail whose tip is the bottom centre of the rectangle.101public struct GlassMapPinShape: Shape {102 /// Height of the tail. The capsule takes the rest of the height.103 public var tailHeight: CGFloat104105 public init(tailHeight: CGFloat) {106 self.tailHeight = tailHeight107 }108109 public func path(in rect: CGRect) -> Path {110 let tail = min(max(tailHeight, 0), rect.height / 2)111 let body = CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: rect.height - tail)112 let capsule = Path(roundedRect: body, cornerRadius: min(body.width, body.height) / 2, style: .continuous)113 guard tail > 0 else { return capsule }114 let half = min(tail * 0.9, body.width / 4)115 var pointer = Path()116 // The tail starts inside the capsule so that the union has no seam.117 pointer.move(to: CGPoint(x: rect.midX - half, y: body.maxY - tail * 0.45))118 pointer.addLine(to: CGPoint(x: rect.midX + half, y: body.maxY - tail * 0.45))119 pointer.addLine(to: CGPoint(x: rect.midX + half * 0.9, y: body.maxY - tail * 0.1))120 pointer.addQuadCurve(to: CGPoint(x: rect.midX - half * 0.9, y: body.maxY - tail * 0.1), control: CGPoint(x: rect.midX, y: rect.maxY + tail * 0.75))121 pointer.closeSubpath()122 return capsule.union(pointer)123 }124}125126/// A map marker: a tinted pin with a tail, holding a symbol or a short text, and a title when selected.127///128/// ```swift129/// GlassMapPin(.symbol("fork.knife"), title: "Chez Sample", isSelected: selection == place.id)130/// GlassMapPin(.text("$$"), title: "Mid-range", tint: .accent)131/// GlassMapPin(clusterCount: 132) // shows "99+"132/// ```133///134/// The pin is a plain view: use it as the content of an annotation in any map engine (`Annotation` in MapKit,135/// a hosted view in another renderer). The tip of the tail is the bottom centre of the view, whatever the136/// state: anchor the annotation there (`anchor: .bottom`). The title of a selected pin is drawn under the tip137/// without taking part in the layout. A cluster is a disc with a halo and no tail: anchor it at its centre.138/// Selection is a parameter; the map engine owns the tap and the clustering — it decides which places merge139/// and passes the count to ``init(clusterCount:cap:title:tint:isSelected:)``.140public struct GlassMapPin: View {141 @GlassThemed private var theme142 @ScaledMetric(relativeTo: .caption) private var baseSide: CGFloat = 32143 private let content: GlassMapPinContent144 private let title: String?145 private let tint: GlassColorToken146 private let isSelected: Bool147148 /// - Parameters:149 /// - content: symbol or short text shown in the marker.150 /// - title: name of the place. Read by VoiceOver, and shown under the marker when it is selected.151 /// Give one whenever the content is a symbol.152 /// - tint: colour role of the marker.153 /// - isSelected: a selected pin is larger and shows its title.154 public init(_ content: GlassMapPinContent, title: String? = nil, tint: GlassColorToken = .primary, isSelected: Bool = false) {155 self.content = content156 self.title = title157 self.tint = tint158 self.isSelected = isSelected159 }160161 /// A cluster marker showing how many places it stands for, capped ("99+").162 public init(clusterCount: Int, cap: Int = 99, title: String? = nil, tint: GlassColorToken = .secondary, isSelected: Bool = false) {163 self.init(.cluster(count: clusterCount, cap: cap), title: title, tint: tint, isSelected: isSelected)164 }165166 private var isCluster: Bool {167 if case .cluster = content { true } else { false }168 }169170 public var body: some View {171 let side = baseSide * (isSelected ? GlassMapPinLayout.selectedScale : 1)172 Group {173 if isCluster {174 cluster(side: side)175 } else {176 pin(side: side)177 }178 }179 .overlay(alignment: .bottom) {180 if isSelected, let title, !title.isEmpty {181 // A zero-sized anchor at the tip: the title hangs under it without changing the size of the pin.182 Color.clear183 .frame(width: 0, height: 0)184 .overlay(alignment: .top) {185 Text(verbatim: title)186 .font(theme.font(.caption).weight(.semibold))187 .foregroundStyle(theme.color(.textPrimary))188 .multilineTextAlignment(.center)189 .lineLimit(2)190 .padding(.horizontal, theme.spacing.xs)191 .padding(.vertical, theme.spacing.xxs)192 .glassOpaqueSurface(in: Capsule(), elevated: true)193 .fixedSize()194 .padding(.top, 6)195 }196 }197 }198 .accessibilityElement(children: .ignore)199 .accessibilityLabel(Text(verbatim: GlassMapPinFormatting.accessibilityLabel(content: content, title: title)))200 .accessibilityAddTraits(isSelected ? .isSelected : [])201 }202203 private func pin(side: CGFloat) -> some View {204 let tail = GlassMapPinLayout.tailHeight(side: side)205 let shape = GlassMapPinShape(tailHeight: tail)206 let border: CGFloat = isSelected ? 2.5 : 2207 return marker208 .font(isSelected ? theme.font(.headline) : theme.font(.caption).weight(.semibold))209 .foregroundStyle(theme.onColor(tint))210 .padding(.horizontal, side * 0.28)211 .frame(minWidth: side, minHeight: side)212 .padding(.bottom, tail)213 .background {214 // Ground shadow under the tip, then the pin with a light border that detaches it from the map.215 Ellipse()216 .fill(.black.opacity(0.22))217 .frame(width: side * 0.42, height: side * 0.14)218 .blur(radius: 1.5)219 .frame(maxHeight: .infinity, alignment: .bottom)220 .offset(y: side * 0.06)221 shape222 .fill(theme.color(tint))223 .shadow(color: .black.opacity(isSelected ? 0.3 : 0.2), radius: isSelected ? 5 : 3, y: isSelected ? 3 : 2)224 shape225 .stroke(theme.onColor(tint), lineWidth: border)226 shape227 .fill(theme.color(tint))228 }229 }230231 private func cluster(side: CGFloat) -> some View {232 marker233 .font(isSelected ? theme.font(.headline) : theme.font(.caption).weight(.bold))234 .foregroundStyle(theme.onColor(tint))235 .padding(.horizontal, side * 0.22)236 .frame(minWidth: side, minHeight: side)237 .background {238 Capsule()239 .fill(theme.color(tint))240 .shadow(color: .black.opacity(0.2), radius: 3, y: 2)241 Capsule()242 .strokeBorder(theme.onColor(tint), lineWidth: 2)243 }244 .padding(side * 0.18)245 .background(theme.color(tint).opacity(0.3), in: Capsule())246 }247248 @ViewBuilder249 private var marker: some View {250 switch content {251 case .symbol(let name):252 Image(systemName: name)253 case .text(let text):254 Text(verbatim: GlassMapPinFormatting.markerText(text))255 .lineLimit(1)256 case .cluster(let count, let cap):257 Text(verbatim: GlassMapPinFormatting.clusterText(count: count, cap: cap))258 .lineLimit(1)259 .monospacedDigit()260 }261 }262}263