SPB Git

Repository Public

AutoFillViews.swift 12.6 KB 259 lines swift

1// SP Vault AutoFill — the compact interface the system shows when a credential is requested: an unlock card, a2// searchable list with the destination always visible, the confirmation for anything that does not match the site.3import LocalAuthentication4import SharedVaultKit5import SwiftUI67struct AutoFillRootView: View {8    @Bindable var model: AutoFillModel910    var body: some View {11        VStack(spacing: 0) {12            HStack {13                BrandMark(size: 22)14                Spacer()15                Button("Cancel") { model.cancel() }16                    .buttonStyle(.vaultCompact).keyboardShortcut(.cancelAction)17                    .accessibilityLabel("Cancel and close SP Vault").accessibilityIdentifier("autofill-cancel")18            }19            .padding(.horizontal, 16).padding(.vertical, 11)20            Rectangle().fill(Palette.line).frame(height: 1)21            Group {22                switch model.stage {23                case .idle: Color.clear24                case .prompt, .unlocking: UnlockCard(model: model)25                case .list: CredentialList(model: model)26                case .failed(let message): FailureCard(model: model, message: message)27                }28            }29            .frame(maxWidth: .infinity, maxHeight: .infinity)30        }31        #if os(macOS)32        .frame(minWidth: 400, idealWidth: 440, maxWidth: 560, minHeight: 440, idealHeight: 520, maxHeight: 720)33        #endif34        .background(Palette.bg.ignoresSafeArea())35        .tint(Palette.accent)36        .alert(model.confirmation?.text.title ?? "", isPresented: Binding(get: { model.confirmation != nil }, set: { if !$0 { model.confirmation = nil } }), presenting: model.confirmation) { c in37            Button(c.text.confirmLabel, role: .destructive) { model.confirmPick(c) }38            Button("Cancel", role: .cancel) { model.confirmation = nil }39        } message: { c in40            Text(c.text.message)41        }42    }43}4445/// "Filling on <host>" — shown on every screen so that the destination is never ambiguous.46private struct DestinationBadge: View {47    let model: AutoFillModel48    var body: some View {49        HStack(spacing: 8) {50            Image(systemName: model.hasDestination ? "globe" : "questionmark.app").foregroundStyle(Palette.muted).accessibilityHidden(true)51            VStack(alignment: .leading, spacing: 1) {52                Text(model.isRegistration ? "PASSKEY FOR" : "FILLING ON").font(.eyebrow).kerning(0.9).foregroundStyle(Palette.muted)53                Text(model.destination).font(.system(.callout, design: .monospaced).weight(.semibold)).foregroundStyle(Palette.text).lineLimit(2).truncationMode(.middle)54            }55            Spacer(minLength: 0)56        }57        .padding(.horizontal, 12).padding(.vertical, 9)58        .background(Palette.s2, in: RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous))59        .overlay(RoundedRectangle(cornerRadius: Metrics.radius, style: .continuous).strokeBorder(Palette.line, lineWidth: 1))60        .accessibilityElement(children: .ignore)61        .accessibilityLabel(model.isRegistration ? "Passkey for \(model.destination)" : "Filling on \(model.destination)")62        .accessibilityIdentifier("autofill-destination")63    }64}6566private struct UnlockCard: View {67    let model: AutoFillModel6869    private var biometry: (label: String, symbol: String) {70        let ctx = LAContext()71        _ = ctx.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)72        switch ctx.biometryType {73        case .touchID: return ("Unlock with Touch ID", "touchid")74        case .faceID: return ("Unlock with Face ID", "faceid")75        case .opticID: return ("Unlock with Optic ID", "opticid")76        default: return ("Unlock", "lock.open")77        }78    }7980    var body: some View {81        ScrollView {82            VStack(spacing: 14) {83                Image(systemName: model.isRegistration ? "person.badge.key" : "lock").font(.system(size: 24, weight: .medium)).foregroundStyle(Palette.brass)84                    .frame(width: 56, height: 56)85                    .background(Palette.s2, in: RoundedRectangle(cornerRadius: 15, style: .continuous))86                    .overlay(RoundedRectangle(cornerRadius: 15, style: .continuous).strokeBorder(Palette.line, lineWidth: 1))87                    .accessibilityHidden(true)88                    .padding(.top, 10)89                Text(model.title).font(.vaultTitle).foregroundStyle(Palette.text).accessibilityAddTraits(.isHeader)90                DestinationBadge(model: model)91                if let account = model.accountLabel {92                    HStack(spacing: 10) {93                        ItemGlyph(title: account, size: 30)94                        VStack(alignment: .leading, spacing: 1) {95                            Text("Account").font(.caption).foregroundStyle(Palette.muted)96                            Text(account).font(.callout.weight(.medium)).foregroundStyle(Palette.text).lineLimit(1).truncationMode(.middle)97                        }98                        Spacer(minLength: 0)99                    }100                    .accessibilityElement(children: .combine)101                }102                if model.isRegistration {103                    Callout(kind: .info, text: "The passkey is created on this device and saved encrypted. It joins your vault and your other devices the next time you open SP Vault.")104                }105                if model.stage == .unlocking {106                    HStack(spacing: 8) { ProgressView().controlSize(.small); Text("Waiting for your confirmation…").font(.callout).foregroundStyle(Palette.muted) }107                        .padding(.vertical, 6)108                } else {109                    Button { model.proceed() } label: {110                        Label(model.isRegistration ? "Save passkey in SP Vault" : biometry.label, systemImage: model.isRegistration ? "plus.circle" : biometry.symbol)111                    }112                    .buttonStyle(.vaultPrimaryBlock).keyboardShortcut(.defaultAction)113                    .accessibilityIdentifier("autofill-unlock")114                }115                Text("SP Vault works offline here: nothing is sent, and the vault is opened read-only by this device’s keys.")116                    .font(.caption).foregroundStyle(Palette.muted).multilineTextAlignment(.center).fixedSize(horizontal: false, vertical: true)117            }118            .padding(18)119            .frame(maxWidth: 440)120            .frame(maxWidth: .infinity)121        }122        .scrollBounceBehavior(.basedOnSize)123    }124}125126private struct FailureCard: View {127    let model: AutoFillModel128    let message: String129    var body: some View {130        VStack(spacing: 14) {131            Callout(kind: .danger, title: "Nothing was filled", text: message).accessibilityIdentifier("autofill-error")132            Button("Close") { model.cancel() }.buttonStyle(.vaultSecondaryBlock).accessibilityIdentifier("autofill-close")133            Spacer(minLength: 0)134        }135        .padding(18).frame(maxWidth: 440)136    }137}138139private struct CredentialList: View {140    @Bindable var model: AutoFillModel141142    var body: some View {143        let listing = model.listing ?? AutoFillCore.Listing(suggested: [], others: [], lookalike: nil)144        VStack(spacing: 10) {145            DestinationBadge(model: model)146            HStack(spacing: 8) {147                Image(systemName: "magnifyingglass").foregroundStyle(Palette.muted).accessibilityHidden(true)148                TextField("Search all items", text: $model.query).textFieldStyle(.plain).autocorrectionDisabled()149                    #if os(iOS)150                    .textInputAutocapitalization(.never)151                    #endif152                    .accessibilityLabel("Search all items").accessibilityIdentifier("autofill-search")153                if !model.query.isEmpty {154                    Button { model.query = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(Palette.muted) }155                        .buttonStyle(.plain).accessibilityLabel("Clear search")156                }157            }158            .padding(.horizontal, 10).padding(.vertical, fieldPadding)159            .background(Palette.s1, in: RoundedRectangle(cornerRadius: 9, style: .continuous))160            .overlay(RoundedRectangle(cornerRadius: 9, style: .continuous).strokeBorder(Palette.lineStrong, lineWidth: 1))161            ScrollView {162                LazyVStack(alignment: .leading, spacing: 10) {163                    if let w = listing.lookalike {164                        Callout(kind: .danger, title: "This site looks like \(w.resembles) — but it is not",165                                text: "You are on \(w.page). Nothing in your vault was saved for it. Check the address before filling anything.")166                            .accessibilityIdentifier("autofill-lookalike")167                    }168                    if !listing.suggested.isEmpty {169                        section("Suggested for \(model.destination)", rows: listing.suggested)170                    } else if model.query.isEmpty {171                        Text(model.hasDestination ? "Nothing in your vault matches \(model.destination)." : "The system did not say which app or site is asking.")172                            .font(.callout).foregroundStyle(Palette.muted).fixedSize(horizontal: false, vertical: true)173                            .accessibilityIdentifier("autofill-no-match")174                    }175                    if !listing.others.isEmpty {176                        section("Other items — ask before filling", rows: listing.others)177                    }178                    if listing.isEmpty, !model.query.isEmpty {179                        Text("No item matches “\(model.query)”.").font(.callout).foregroundStyle(Palette.muted)180                    }181                }182                .padding(.bottom, 12)183            }184        }185        .padding(.horizontal, 14).padding(.top, 12)186    }187188    private var fieldPadding: CGFloat {189        #if os(macOS)190        7191        #else192        11193        #endif194    }195196    @ViewBuilder private func section(_ title: String, rows: [AutoFillCore.Row]) -> some View {197        VStack(alignment: .leading, spacing: 6) {198            Eyebrow(title)199            VStack(spacing: 0) {200                ForEach(Array(rows.enumerated()), id: \.element.id) { i, row in201                    CredentialRow(row: row) { model.pick(row) }202                    if i < rows.count - 1 { Rectangle().fill(Palette.line).frame(height: 1) }203                }204            }205            .vaultCard()206        }207    }208}209210private struct CredentialRow: View {211    let row: AutoFillCore.Row212    let action: () -> Void213214    private var title: String { row.view.item.title.isEmpty ? "Untitled" : row.view.item.title }215    private var note: String? {216        if row.isPasskey { return row.isPending ? "Passkey · not yet synced" : "Passkey" }217        switch row.kind {218        case .insecure: return "Not encrypted (http) — asks before filling"219        case .none: return "Does not match this site"220        case .match: return nil221        }222    }223224    var body: some View {225        Button(action: action) {226            HStack(spacing: 11) {227                ItemGlyph(title: title, symbol: row.isPasskey ? "person.badge.key" : nil, size: 34)228                VStack(alignment: .leading, spacing: 2) {229                    Text(title).font(.body.weight(.medium)).foregroundStyle(Palette.text).lineLimit(1)230                    let sub = AutoFillCore.subtitle(row.view.item)231                    if !sub.isEmpty { Text(sub).font(.caption).foregroundStyle(Palette.muted).lineLimit(1).truncationMode(.middle) }232                    if let note {233                        Label(note, systemImage: row.isPasskey ? "checkmark.seal" : "exclamationmark.triangle")234                            .font(.caption2.weight(.medium)).foregroundStyle(row.isPasskey ? Palette.accent : (row.kind == .insecure ? Palette.danger : Palette.brass))235                    }236                }237                Spacer(minLength: 4)238                Image(systemName: "chevron.right").font(.caption.weight(.semibold)).foregroundStyle(Palette.muted).accessibilityHidden(true)239            }240            .padding(.horizontal, 12).padding(.vertical, rowPadding)241            .contentShape(Rectangle())242        }243        .buttonStyle(.plain)244        .accessibilityElement(children: .ignore)245        .accessibilityLabel([title, AutoFillCore.subtitle(row.view.item), note ?? ""].filter { !$0.isEmpty }.joined(separator: ", "))246        .accessibilityHint(row.isPasskey ? "Signs in with this passkey" : (row.kind == .match ? "Fills this item" : "Asks for confirmation before filling"))247        .accessibilityAddTraits(.isButton)248        .accessibilityIdentifier("autofill-row-\(row.id)")249    }250251    private var rowPadding: CGFloat {252        #if os(macOS)253        8254        #else255        12256        #endif257    }258}259
Files main
Menu

Theme