Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Scanner/QuadCropEditor.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/// Review of a captured photograph with manual crop: the four corners of the44/// card can be dragged, a loupe shows the pixels under the finger.45struct QuadCropEditor: View {46 let image: CGImage47 @Binding var quad: CardQuad4849 @State private var active: Int?50 @State private var dragStart: CGPoint?51 @Environment(\.accessibilityReduceMotion) private var reduceMotion5253 private static let names = ["Top left corner", "Top right corner", "Bottom right corner", "Bottom left corner"]5455 private func corner(_ index: Int) -> CGPoint { quad.corners[index] }5657 private func setCorner(_ index: Int, _ point: CGPoint) {58 var candidate = quad59 let p = CGPoint(x: min(max(point.x, 0), 1), y: min(max(point.y, 0), 1))60 switch index {61 case 0: candidate.topLeft = p62 case 1: candidate.topRight = p63 case 2: candidate.bottomRight = p64 default: candidate.bottomLeft = p65 }66 // A crossed or collapsed outline cannot be corrected: refuse the move.67 if candidate.isConvex, candidate.area > 0.01 { quad = candidate }68 }6970 var body: some View {71 GeometryReader { proxy in72 let imageSize = CGSize(width: image.width, height: image.height)73 let rect = ScanGeometry.rect(for: imageSize, in: proxy.size, fill: false)74 let points = ScanGeometry.points(of: quad, in: rect)75 ZStack {76 Image(decorative: image, scale: 1).resizable().interpolation(.medium)77 .frame(width: rect.width, height: rect.height)78 .position(x: rect.midX, y: rect.midY)79 // Dim everything outside the card.80 Path { path in81 path.addRect(rect)82 path.move(to: points[0])83 points.dropFirst().forEach { path.addLine(to: $0) }84 path.closeSubpath()85 }86 .fill(.black.opacity(0.55), style: FillStyle(eoFill: true))87 .allowsHitTesting(false)88 QuadOutline(corners: points).stroke(SP.Palette.electric, lineWidth: 1.5).allowsHitTesting(false)89 ForEach(0..<4, id: \.self) { index in90 handle(index, at: points[index], rect: rect)91 }92 if let active {93 loupe(for: active, rect: rect, container: proxy.size)94 }95 }96 }97 }9899 private func handle(_ index: Int, at point: CGPoint, rect: CGRect) -> some View {100 ZStack {101 Circle().fill(.black.opacity(0.35)).frame(width: 30, height: 30)102 Circle().strokeBorder(.white, lineWidth: 2).frame(width: 30, height: 30)103 Circle().fill(SP.Palette.electric).frame(width: 10, height: 10)104 }105 .scaleEffect(active == index ? 1.25 : 1)106 .frame(width: 56, height: 56)107 .contentShape(Circle())108 .position(point)109 .gesture(110 DragGesture(minimumDistance: 0, coordinateSpace: .local)111 .onChanged { value in112 if active != index {113 active = index114 dragStart = corner(index)115 Haptics.shared.play(.lift)116 }117 guard let start = dragStart, rect.width > 0, rect.height > 0 else { return }118 setCorner(index, CGPoint(x: start.x + value.translation.width / rect.width, y: start.y + value.translation.height / rect.height))119 }120 .onEnded { _ in121 active = nil122 dragStart = nil123 Haptics.shared.play(.drop)124 }125 )126 .animation(reduceMotion ? nil : SP.Motion.quick, value: active)127 .accessibilityElement()128 .accessibilityLabel(Self.names[index])129 .accessibilityValue("\(Int(corner(index).x * 100)) percent across, \(Int(corner(index).y * 100)) percent down")130 .accessibilityAdjustableAction { direction in131 let delta: CGFloat = direction == .increment ? 0.01 : -0.01132 setCorner(index, CGPoint(x: corner(index).x + delta, y: corner(index).y))133 }134 .accessibilityAction(named: "Move up") { setCorner(index, CGPoint(x: corner(index).x, y: corner(index).y - 0.01)) }135 .accessibilityAction(named: "Move down") { setCorner(index, CGPoint(x: corner(index).x, y: corner(index).y + 0.01)) }136 .accessibilityIdentifier("scanner.corner.\(index)")137 }138139 /// Magnified view of the photograph around the corner being dragged.140 private func loupe(for index: Int, rect: CGRect, container: CGSize) -> some View {141 let diameter: CGFloat = 108, zoom: CGFloat = 2.6142 let p = corner(index)143 let viewPoint = CGPoint(x: rect.minX + p.x * rect.width, y: rect.minY + p.y * rect.height)144 // Keep the loupe on screen and away from the finger.145 let above = viewPoint.y - diameter * 0.95 > diameter / 2146 let centre = CGPoint(x: min(max(viewPoint.x, diameter / 2 + 6), container.width - diameter / 2 - 6),147 y: above ? viewPoint.y - diameter * 0.95 : viewPoint.y + diameter * 0.95)148 return ZStack {149 Image(decorative: image, scale: 1).resizable().interpolation(.high)150 .frame(width: rect.width * zoom, height: rect.height * zoom)151 .offset(x: (0.5 - p.x) * rect.width * zoom, y: (0.5 - p.y) * rect.height * zoom)152 Rectangle().fill(SP.Palette.electric).frame(width: 1, height: 18)153 Rectangle().fill(SP.Palette.electric).frame(width: 18, height: 1)154 }155 .frame(width: diameter, height: diameter)156 .background(.black)157 .clipShape(Circle())158 .overlay(Circle().strokeBorder(.white.opacity(0.9), lineWidth: 2))159 .shadow(color: .black.opacity(0.5), radius: 10, y: 4)160 .position(centre)161 .allowsHitTesting(false)162 .accessibilityHidden(true)163 }164}165