// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassMaps/Maps/GlassMapPin.swift // Module : SPGlassMaps // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-03 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// What a ``GlassMapPin`` shows inside its marker. public enum GlassMapPinContent: Equatable, Sendable { /// An SF Symbol. case symbol(String) /// A short text (a price, a letter, a rank). Longer texts are cut to ``GlassMapPinFormatting/textLimit`` characters. case text(String) /// The number of places a cluster stands for. case cluster(count: Int, cap: Int) } /// Text rules of ``GlassMapPin``. public enum GlassMapPinFormatting { /// Largest number of characters shown inside a marker. public static let textLimit = 4 /// The text of a cluster marker: the localised count, or the cap followed by "+" above it ("99+"). public static func clusterText(count: Int, cap: Int = 99, locale: Locale = .autoupdatingCurrent) -> String { let limit = max(cap, 1) let value = max(count, 0) let style = IntegerFormatStyle.number.locale(locale) return value > limit ? "\(limit.formatted(style))+" : value.formatted(style) } /// A text short enough for a marker: trimmed, and cut after `textLimit` characters (grapheme clusters). public static func markerText(_ text: String) -> String { String(text.trimmingCharacters(in: .whitespacesAndNewlines).prefix(textLimit)) } /// The accessibility label: the title when there is one, otherwise what the marker shows. public static func accessibilityLabel(content: GlassMapPinContent, title: String?) -> String { let trimmedTitle = title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" switch content { case .cluster(let count, _): let places = GlassL10n.string(.clusterCount, max(count, 0)) return trimmedTitle.isEmpty ? places : "\(trimmedTitle), \(places)" case .text(let text): return trimmedTitle.isEmpty ? text : trimmedTitle case .symbol: return trimmedTitle } } } /// Geometry of a ``GlassMapPin``. public enum GlassMapPinLayout { /// Height of the tail under the marker, for a marker `side` points high. public static func tailHeight(side: CGFloat) -> CGFloat { max(side * 0.24, 5) } /// Scale of a selected pin. public static let selectedScale: CGFloat = 1.3 } /// The outline of a pin: a capsule with a short tail whose tip is the bottom centre of the rectangle. public struct GlassMapPinShape: Shape { /// Height of the tail. The capsule takes the rest of the height. public var tailHeight: CGFloat public init(tailHeight: CGFloat) { self.tailHeight = tailHeight } public func path(in rect: CGRect) -> Path { let tail = min(max(tailHeight, 0), rect.height / 2) let body = CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: rect.height - tail) let capsule = Path(roundedRect: body, cornerRadius: min(body.width, body.height) / 2, style: .continuous) guard tail > 0 else { return capsule } let half = min(tail * 0.9, body.width / 4) var pointer = Path() // The tail starts inside the capsule so that the union has no seam. pointer.move(to: CGPoint(x: rect.midX - half, y: body.maxY - tail * 0.45)) pointer.addLine(to: CGPoint(x: rect.midX + half, y: body.maxY - tail * 0.45)) pointer.addLine(to: CGPoint(x: rect.midX + half * 0.9, y: body.maxY - tail * 0.1)) 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)) pointer.closeSubpath() return capsule.union(pointer) } } /// A map marker: a tinted pin with a tail, holding a symbol or a short text, and a title when selected. /// /// ```swift /// GlassMapPin(.symbol("fork.knife"), title: "Chez Sample", isSelected: selection == place.id) /// GlassMapPin(.text("$$"), title: "Mid-range", tint: .accent) /// GlassMapPin(clusterCount: 132) // shows "99+" /// ``` /// /// The pin is a plain view: use it as the content of an annotation in any map engine (`Annotation` in MapKit, /// a hosted view in another renderer). The tip of the tail is the bottom centre of the view, whatever the /// state: anchor the annotation there (`anchor: .bottom`). The title of a selected pin is drawn under the tip /// without taking part in the layout. A cluster is a disc with a halo and no tail: anchor it at its centre. /// Selection is a parameter; the map engine owns the tap and the clustering — it decides which places merge /// and passes the count to ``init(clusterCount:cap:title:tint:isSelected:)``. public struct GlassMapPin: View { @GlassThemed private var theme @ScaledMetric(relativeTo: .caption) private var baseSide: CGFloat = 32 private let content: GlassMapPinContent private let title: String? private let tint: GlassColorToken private let isSelected: Bool /// - Parameters: /// - content: symbol or short text shown in the marker. /// - title: name of the place. Read by VoiceOver, and shown under the marker when it is selected. /// Give one whenever the content is a symbol. /// - tint: colour role of the marker. /// - isSelected: a selected pin is larger and shows its title. public init(_ content: GlassMapPinContent, title: String? = nil, tint: GlassColorToken = .primary, isSelected: Bool = false) { self.content = content self.title = title self.tint = tint self.isSelected = isSelected } /// A cluster marker showing how many places it stands for, capped ("99+"). public init(clusterCount: Int, cap: Int = 99, title: String? = nil, tint: GlassColorToken = .secondary, isSelected: Bool = false) { self.init(.cluster(count: clusterCount, cap: cap), title: title, tint: tint, isSelected: isSelected) } private var isCluster: Bool { if case .cluster = content { true } else { false } } public var body: some View { let side = baseSide * (isSelected ? GlassMapPinLayout.selectedScale : 1) Group { if isCluster { cluster(side: side) } else { pin(side: side) } } .overlay(alignment: .bottom) { if isSelected, let title, !title.isEmpty { // A zero-sized anchor at the tip: the title hangs under it without changing the size of the pin. Color.clear .frame(width: 0, height: 0) .overlay(alignment: .top) { Text(verbatim: title) .font(theme.font(.caption).weight(.semibold)) .foregroundStyle(theme.color(.textPrimary)) .multilineTextAlignment(.center) .lineLimit(2) .padding(.horizontal, theme.spacing.xs) .padding(.vertical, theme.spacing.xxs) .glassOpaqueSurface(in: Capsule(), elevated: true) .fixedSize() .padding(.top, 6) } } } .accessibilityElement(children: .ignore) .accessibilityLabel(Text(verbatim: GlassMapPinFormatting.accessibilityLabel(content: content, title: title))) .accessibilityAddTraits(isSelected ? .isSelected : []) } private func pin(side: CGFloat) -> some View { let tail = GlassMapPinLayout.tailHeight(side: side) let shape = GlassMapPinShape(tailHeight: tail) let border: CGFloat = isSelected ? 2.5 : 2 return marker .font(isSelected ? theme.font(.headline) : theme.font(.caption).weight(.semibold)) .foregroundStyle(theme.onColor(tint)) .padding(.horizontal, side * 0.28) .frame(minWidth: side, minHeight: side) .padding(.bottom, tail) .background { // Ground shadow under the tip, then the pin with a light border that detaches it from the map. Ellipse() .fill(.black.opacity(0.22)) .frame(width: side * 0.42, height: side * 0.14) .blur(radius: 1.5) .frame(maxHeight: .infinity, alignment: .bottom) .offset(y: side * 0.06) shape .fill(theme.color(tint)) .shadow(color: .black.opacity(isSelected ? 0.3 : 0.2), radius: isSelected ? 5 : 3, y: isSelected ? 3 : 2) shape .stroke(theme.onColor(tint), lineWidth: border) shape .fill(theme.color(tint)) } } private func cluster(side: CGFloat) -> some View { marker .font(isSelected ? theme.font(.headline) : theme.font(.caption).weight(.bold)) .foregroundStyle(theme.onColor(tint)) .padding(.horizontal, side * 0.22) .frame(minWidth: side, minHeight: side) .background { Capsule() .fill(theme.color(tint)) .shadow(color: .black.opacity(0.2), radius: 3, y: 2) Capsule() .strokeBorder(theme.onColor(tint), lineWidth: 2) } .padding(side * 0.18) .background(theme.color(tint).opacity(0.3), in: Capsule()) } @ViewBuilder private var marker: some View { switch content { case .symbol(let name): Image(systemName: name) case .text(let text): Text(verbatim: GlassMapPinFormatting.markerText(text)) .lineLimit(1) case .cluster(let count, let cap): Text(verbatim: GlassMapPinFormatting.clusterText(count: count, cap: cap)) .lineLimit(1) .monospacedDigit() } } }