Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMaps/Maps/GlassCompass.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/// A compass that shows an injected heading and resets the map to north when tapped.49///50/// ```swift51/// GlassCompass(heading: camera.heading) { camera.heading = 0 }52/// GlassCompass(heading: nil) {} // heading unavailable53/// ```54///55/// `heading` is the direction the top of the map points to, in degrees clockwise from north. The needle keeps56/// pointing north, so it turns by the opposite angle along the shortest arc. The dial is drawn: a two-tone57/// needle whose north half takes the theme's destructive colour, and four cardinal ticks that turn with it.58/// With Reduce Motion the needle is set to its new angle without a rotation animation. A `nil` heading shows an59/// outlined, dimmed needle and disables the button.60public struct GlassCompass: View {61 @GlassThemed private var theme62 @GlassAccessibility private var accessibility63 private let heading: Double?64 private let size: GlassControlSize65 private let onResetNorth: @MainActor () -> Void66 @State private var rotation: Double6768 public init(heading: Double?, size: GlassControlSize = .regular, onResetNorth: @escaping @MainActor () -> Void) {69 self.heading = heading.flatMap { $0.isFinite ? $0 : nil }70 self.size = size71 self.onResetNorth = onResetNorth72 _rotation = State(initialValue: GlassHeading.needleRotation(current: 0, heading: heading ?? 0))73 }7475 public var body: some View {76 let side = GlassIconButtonMetrics.hitTarget(size, metrics: theme.metrics)77 Button(action: onResetNorth) {78 dial(side: side)79 .rotationEffect(.degrees(heading == nil ? 0 : rotation))80 .frame(width: side, height: side)81 .glassSurface(.interactive.tinted(.none), in: Circle())82 .contentShape(Circle())83 }84 .buttonStyle(.plain)85 .disabled(heading == nil)86 .accessibilityLabel(Text(GlassL10n.string(.resetNorth)))87 .accessibilityValue(Text(GlassHeading.accessibilityValue(heading)))88 .help(GlassL10n.string(heading == nil ? .compassUnavailable : .resetNorth))89 .onChange(of: heading) { _, newHeading in90 guard let newHeading else { return }91 let target = GlassHeading.needleRotation(current: rotation, heading: newHeading)92 if accessibility.reduceMotion {93 rotation = target94 } else {95 withAnimation(theme.animation(.fast, reduceMotion: false)) { rotation = target }96 }97 }98 }99100 private func dial(side: CGFloat) -> some View {101 let isAvailable = heading != nil102 let north = theme.color(.destructive)103 let south = theme.color(.textSecondary)104 return ZStack {105 GlassCompassTicks()106 .stroke(south.opacity(isAvailable ? 0.7 : 0.35), style: StrokeStyle(lineWidth: 1.25, lineCap: .round))107 if isAvailable {108 GlassCompassNeedle(half: .north).fill(north)109 GlassCompassNeedle(half: .south).fill(south.opacity(0.6))110 } else {111 GlassCompassNeedle(half: .both)112 .stroke(south.opacity(0.7), style: StrokeStyle(lineWidth: 1.25, lineJoin: .round))113 }114 Circle()115 .fill(theme.color(.surface))116 .frame(width: side * 0.07, height: side * 0.07)117 .opacity(isAvailable ? 1 : 0)118 }119 .accessibilityHidden(true)120 }121}122123/// The needle of a ``GlassCompass``: a slim rhombus, drawn whole or one half at a time.124struct GlassCompassNeedle: Shape {125 enum Half { case north, south, both }126 let half: Half127128 func path(in rect: CGRect) -> Path {129 let side = min(rect.width, rect.height)130 let centre = CGPoint(x: rect.midX, y: rect.midY)131 let length = side * 0.29132 let width = side * 0.095133 let top = CGPoint(x: centre.x, y: centre.y - length)134 let bottom = CGPoint(x: centre.x, y: centre.y + length)135 let left = CGPoint(x: centre.x - width, y: centre.y)136 let right = CGPoint(x: centre.x + width, y: centre.y)137 var path = Path()138 switch half {139 case .north: path.addLines([top, right, left])140 case .south: path.addLines([bottom, left, right])141 case .both: path.addLines([top, right, bottom, left])142 }143 path.closeSubpath()144 return path145 }146}147148/// Four short cardinal ticks on the rim of a ``GlassCompass``.149struct GlassCompassTicks: Shape {150 func path(in rect: CGRect) -> Path {151 let side = min(rect.width, rect.height)152 let centre = CGPoint(x: rect.midX, y: rect.midY)153 var path = Path()154 for quarter in 0..<4 {155 let angle = Double(quarter) * .pi / 2156 let outer = side * 0.41157 let inner = side * 0.36158 path.move(to: CGPoint(x: centre.x + CGFloat(sin(angle)) * inner, y: centre.y - CGFloat(cos(angle)) * inner))159 path.addLine(to: CGPoint(x: centre.x + CGFloat(sin(angle)) * outer, y: centre.y - CGFloat(cos(angle)) * outer))160 }161 return path162 }163}164165/// Symbol names used by ``GlassCompass``.166public enum GlassCompassSymbols {167 /// A filled north arrow when the heading is known, an outlined one when it is not. The compass draws its168 /// own needle; the names are for hosts that need a matching symbol elsewhere (a menu, a legend).169 public static func needle(isAvailable: Bool) -> String {170 isAvailable ? "location.north.fill" : "location.north"171 }172}173