Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Scanner/ScannerChrome.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// 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 : None specified (no license file in this repository)33//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 SwiftUI4243/// Where the owner is in the scan: Front · Back · Review.44struct ScanStepIndicator: View {45 enum Step: Int, CaseIterable {46 case front, back, review47 var title: String {48 switch self {49 case .front: "Front"50 case .back: "Back"51 case .review: "Review"52 }53 }54 }5556 var current: Step57 /// Steps shown (a single-side retake shows only its own side).58 var steps: [Step] = Step.allCases5960 var body: some View {61 HStack(spacing: 6) {62 ForEach(steps, id: \.self) { step in63 let state: (done: Bool, active: Bool) = (step.rawValue < current.rawValue, step == current)64 HStack(spacing: 5) {65 ZStack {66 Circle().strokeBorder(state.active ? SP.Palette.electric : Color.white.opacity(state.done ? 0.7 : 0.3), lineWidth: 1)67 if state.done {68 Image(systemName: "checkmark").font(.system(size: 8, weight: .heavy))69 } else if state.active {70 Circle().fill(SP.Palette.electric).padding(4)71 }72 }73 .frame(width: 14, height: 14)74 Text(step.title).font(.system(size: 10, weight: .semibold).width(.expanded)).tracking(0.8).textCase(.uppercase)75 .lineLimit(1).fixedSize()76 }77 .foregroundStyle(.white.opacity(state.active ? 1 : (state.done ? 0.75 : 0.4)))78 if step != steps.last {79 Rectangle().fill(.white.opacity(0.22)).frame(width: 8, height: 0.75)80 }81 }82 }83 .padding(.horizontal, 11).frame(height: 44)84 .fixedSize()85 .spGlass(interactive: false)86 .accessibilityElement(children: .ignore)87 .accessibilityLabel("Step: \(current.title)")88 }89}9091/// Outline of the card in the viewfinder: thin titanium corner brackets that92/// lock onto the detected card and turn electric when the frame is ready.93struct QuadBrackets: Shape {94 /// Corners in view coordinates: top-left, top-right, bottom-right, bottom-left.95 var corners: [CGPoint]96 /// Bracket arm length as a fraction of each side.97 var arm: CGFloat = 0.169899 var animatableData: AnimatablePair<AnimatablePair<AnimatablePair<CGFloat, CGFloat>, AnimatablePair<CGFloat, CGFloat>>,100 AnimatablePair<AnimatablePair<CGFloat, CGFloat>, AnimatablePair<CGFloat, CGFloat>>> {101 get {102 AnimatablePair(AnimatablePair(AnimatablePair(corners[0].x, corners[0].y), AnimatablePair(corners[1].x, corners[1].y)),103 AnimatablePair(AnimatablePair(corners[2].x, corners[2].y), AnimatablePair(corners[3].x, corners[3].y)))104 }105 set {106 corners = [CGPoint(x: newValue.first.first.first, y: newValue.first.first.second),107 CGPoint(x: newValue.first.second.first, y: newValue.first.second.second),108 CGPoint(x: newValue.second.first.first, y: newValue.second.first.second),109 CGPoint(x: newValue.second.second.first, y: newValue.second.second.second)]110 }111 }112113 func path(in rect: CGRect) -> Path {114 var path = Path()115 guard corners.count == 4 else { return path }116 for index in 0..<4 {117 let corner = corners[index], previous = corners[(index + 3) % 4], next = corners[(index + 1) % 4]118 func toward(_ other: CGPoint) -> CGPoint {119 CGPoint(x: corner.x + (other.x - corner.x) * arm, y: corner.y + (other.y - corner.y) * arm)120 }121 path.move(to: toward(previous))122 path.addLine(to: corner)123 path.addLine(to: toward(next))124 }125 return path126 }127}128129/// Full outline, used under the brackets as a hairline.130struct QuadOutline: Shape {131 var corners: [CGPoint]132133 func path(in rect: CGRect) -> Path {134 var path = Path()135 guard corners.count == 4 else { return path }136 path.move(to: corners[0])137 corners.dropFirst().forEach { path.addLine(to: $0) }138 path.closeSubpath()139 return path140 }141}142143/// Brackets + hairline, coloured by readiness.144struct ViewfinderFrame: View {145 var corners: [CGPoint]146 var isLocked: Bool147 var isReady: Bool148 @Environment(\.accessibilityReduceMotion) private var reduceMotion149150 var body: some View {151 let color = isReady ? SP.Palette.electric : (isLocked ? Color.white : SP.Palette.titanium)152 ZStack {153 QuadOutline(corners: corners).stroke(color.opacity(isLocked ? 0.35 : 0.18), lineWidth: 0.75)154 QuadBrackets(corners: corners).stroke(color, style: StrokeStyle(lineWidth: isReady ? 3 : 2, lineCap: .round, lineJoin: .round))155 .shadow(color: isReady ? SP.Palette.electric.opacity(0.6) : .clear, radius: 6)156 }157 .animation(reduceMotion ? nil : .spring(response: 0.28, dampingFraction: 0.85), value: corners)158 .animation(SP.Motion.quick, value: isReady)159 .allowsHitTesting(false)160 .accessibilityHidden(true)161 }162}163164/// The compact instruction under the viewfinder.165struct ScanHintLine: View {166 var text: String167 var tone: StatusTone = .neutral168 var symbol: String?169170 var body: some View {171 HStack(spacing: 7) {172 if let symbol { Image(systemName: symbol).font(.system(size: 12, weight: .bold)).foregroundStyle(tone.color) }173 else { Circle().fill(tone.color).frame(width: 7, height: 7) }174 Text(text).font(.system(size: 14, weight: .semibold)).foregroundStyle(.white)175 }176 .padding(.horizontal, SP.Space.m).padding(.vertical, 10)177 .spGlass(interactive: false)178 .contentTransition(.opacity)179 .animation(SP.Motion.quick, value: text)180 .accessibilityElement(children: .combine)181 .accessibilityAddTraits(.updatesFrequently)182 }183}184185/// The shutter: a titanium ring that fills in electric as the frame becomes ready.186struct ShutterButton: View {187 /// 0…1 progress towards automatic capture.188 var readiness: Double189 var isBusy = false190 var action: () -> Void191192 var body: some View {193 Button(action: action) {194 ZStack {195 Circle().strokeBorder(.white.opacity(0.35), lineWidth: 3)196 Circle().trim(from: 0, to: readiness).stroke(SP.Palette.electric, style: StrokeStyle(lineWidth: 3, lineCap: .round))197 .rotationEffect(.degrees(-90)).padding(1.5)198 Circle().fill(.white).padding(7).opacity(isBusy ? 0.35 : 1)199 if isBusy { ProgressView().tint(.black) }200 }201 .frame(width: 76, height: 76)202 .contentShape(Circle())203 }204 .buttonStyle(.plain)205 .disabled(isBusy)206 .animation(SP.Motion.quick, value: readiness)207 .accessibilityLabel("Take photo")208 .accessibilityIdentifier("scanner.shutter")209 }210}211212/// Top bar shared by the scanner screens: close, step, one trailing control.213struct ScannerTopBar<Trailing: View>: View {214 var step: ScanStepIndicator.Step215 var steps: [ScanStepIndicator.Step] = ScanStepIndicator.Step.allCases216 var closeSymbol = "xmark"217 var closeLabel = "Cancel scan"218 var onClose: () -> Void219 @ViewBuilder var trailing: Trailing220221 var body: some View {222 HStack {223 GlassIconButton(symbol: closeSymbol, label: closeLabel, action: onClose)224 .accessibilityIdentifier("scanner.close")225 Spacer(minLength: SP.Space.xs)226 ScanStepIndicator(current: step, steps: steps)227 Spacer(minLength: SP.Space.xs)228 trailing.frame(minWidth: 44, minHeight: 44)229 }230 .padding(.horizontal, SP.Space.m)231 .padding(.top, SP.Space.xs)232 }233}234235enum ScanGeometry {236 /// Where an image of `content` size lands inside `container` for the given fit.237 static func rect(for content: CGSize, in container: CGSize, fill: Bool) -> CGRect {238 guard content.width > 0, content.height > 0, container.width > 0, container.height > 0 else { return .zero }239 let scale = fill ? max(container.width / content.width, container.height / content.height)240 : min(container.width / content.width, container.height / content.height)241 let size = CGSize(width: content.width * scale, height: content.height * scale)242 return CGRect(x: (container.width - size.width) / 2, y: (container.height - size.height) / 2, width: size.width, height: size.height)243 }244245 static func points(of quad: CardQuad, in rect: CGRect) -> [CGPoint] {246 quad.corners.map { CGPoint(x: rect.minX + $0.x * rect.width, y: rect.minY + $0.y * rect.height) }247 }248}249