Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Scanner/MaskEditorView.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/// One black-out rectangle, normalised to the card image (origin top-left).44struct MaskRect: Identifiable, Equatable {45 let id = UUID()46 var rect: CGRect4748 static let minimumSize: CGFloat = 0.044950 static func clamped(_ rect: CGRect) -> CGRect {51 var r = rect.standardized52 r.size.width = min(max(r.width, minimumSize), 1)53 r.size.height = min(max(r.height, minimumSize), 1)54 r.origin.x = min(max(r.minX, 0), 1 - r.width)55 r.origin.y = min(max(r.minY, 0), 1 - r.height)56 return r57 }58}5960/// Validation of the security-code mask on a bank card. The proposed61/// rectangles can be moved, resized, removed, and new ones added. Nothing62/// continues until the owner either validates the mask or states that this63/// side shows no security code.64struct MaskEditorView: View {65 let image: CGImage66 let format: CardFormat67 var side: CardSide68 @Binding var masks: [MaskRect]69 @Binding var selection: UUID?7071 var body: some View {72 GeometryReader { proxy in73 let size = proxy.size74 ZStack(alignment: .topLeading) {75 Image(decorative: image, scale: 1).resizable().interpolation(.high)76 .frame(width: size.width, height: size.height)77 .contentShape(Rectangle())78 .onTapGesture { selection = nil }79 ForEach($masks) { $mask in80 MaskHandle(mask: $mask, isSelected: selection == mask.id, container: size,81 onSelect: { selection = mask.id },82 onDelete: {83 masks.removeAll { $0.id == mask.id }84 selection = nil85 Haptics.shared.play(.drop)86 })87 }88 }89 .clipShape(RoundedRectangle(cornerRadius: size.width * format.cornerRadiusFraction, style: .continuous))90 .overlay(RoundedRectangle(cornerRadius: size.width * format.cornerRadiusFraction, style: .continuous).strokeBorder(SP.Palette.hairlineStrong, lineWidth: 0.75))91 }92 .aspectRatio(CGFloat(image.width) / CGFloat(max(image.height, 1)), contentMode: .fit)93 .accessibilityElement(children: .contain)94 .accessibilityLabel("\(side == .back ? "Back" : "Front") of the card with \(masks.count) mask\(masks.count == 1 ? "" : "s")")95 }96}9798private struct MaskHandle: View {99 @Binding var mask: MaskRect100 var isSelected: Bool101 var container: CGSize102 var onSelect: () -> Void103 var onDelete: () -> Void104105 @State private var moveStart: CGRect?106 @State private var resizeStart: CGRect?107108 var body: some View {109 let frame = CGRect(x: mask.rect.minX * container.width, y: mask.rect.minY * container.height,110 width: mask.rect.width * container.width, height: mask.rect.height * container.height)111 ZStack {112 Rectangle().fill(.black)113 .overlay(Rectangle().strokeBorder(isSelected ? SP.Palette.electric : .white.opacity(0.6),114 style: StrokeStyle(lineWidth: isSelected ? 2 : 1, dash: isSelected ? [] : [4, 3])))115 .frame(width: frame.width, height: frame.height)116 .contentShape(Rectangle().inset(by: -10))117 .position(x: frame.midX, y: frame.midY)118 .gesture(119 DragGesture(minimumDistance: 0)120 .onChanged { value in121 if moveStart == nil { moveStart = mask.rect; onSelect() }122 guard let start = moveStart else { return }123 mask.rect = MaskRect.clamped(start.offsetBy(dx: value.translation.width / container.width,124 dy: value.translation.height / container.height))125 }126 .onEnded { _ in moveStart = nil }127 )128 .accessibilityElement()129 .accessibilityLabel("Security code mask")130 .accessibilityHint("Drag to move. It will be permanently blacked out.")131 .accessibilityAddTraits(isSelected ? [.isSelected] : [])132 .accessibilityAction(named: "Remove mask", onDelete)133 .accessibilityIdentifier("scanner.mask")134 if isSelected {135 // Resize from the bottom-right corner.136 Circle().fill(SP.Palette.electric).frame(width: 20, height: 20)137 .overlay(Circle().strokeBorder(.white, lineWidth: 2))138 .frame(width: 44, height: 44).contentShape(Circle())139 .position(x: frame.maxX, y: frame.maxY)140 .gesture(141 DragGesture(minimumDistance: 0)142 .onChanged { value in143 if resizeStart == nil { resizeStart = mask.rect }144 guard let start = resizeStart else { return }145 var r = start146 r.size.width = max(MaskRect.minimumSize, start.width + value.translation.width / container.width)147 r.size.height = max(MaskRect.minimumSize, start.height + value.translation.height / container.height)148 r.size.width = min(r.width, 1 - r.minX)149 r.size.height = min(r.height, 1 - r.minY)150 mask.rect = r151 }152 .onEnded { _ in resizeStart = nil }153 )154 .accessibilityLabel("Resize mask")155 Button(action: onDelete) {156 Image(systemName: "xmark").font(.system(size: 11, weight: .heavy)).foregroundStyle(.white)157 .frame(width: 24, height: 24).background(SP.Palette.critical, in: Circle())158 .overlay(Circle().strokeBorder(.white, lineWidth: 1.5))159 .frame(width: 44, height: 44).contentShape(Circle())160 }161 .buttonStyle(.plain)162 .position(x: frame.minX, y: frame.minY)163 .accessibilityLabel("Remove mask")164 .accessibilityIdentifier("scanner.mask.remove")165 }166 }167 }168}169