Sources/Orgstar/CryptHoldBar.swift
cmc Replace in Files keeps no plaintext of tagged entries; crypt settings in their own modifier
60cc4d87d6 10 hours ago45 lines · 1744 bytes
1import OrgApp
2import OrgDocument
3import SwiftUI
4
5/// Above the mode line while the focused file isn't saved because it would put text on disk
6/// unencrypted: why, and the button that asks for the passphrase. Stays while it applies.
7struct CryptHoldBar: View {
8 let session: DocumentSession
9 let encrypt: () -> Void
10
11 var body: some View {
12 if let path = session.path, let hold = session.held[path] {
13 HStack(spacing: 8) {
14 Image(systemName: "lock.trianglebadge.exclamationmark").foregroundStyle(.orange)
15 Text(hold.notice(for: path)).lineLimit(3)
16 Spacer(minLength: 8)
17 Button(hold == .file ? "Set Passphrase…" : "Encrypt…", action: encrypt)
18 }
19 .font(.callout)
20 .padding(.horizontal, 10)
21 .padding(.vertical, 5)
22 .frame(maxWidth: .infinity, alignment: .leading)
23 .background(Color.orange.opacity(0.12))
24 .accessibilityElement(children: .combine)
25 }
26 }
27}
28
29/// The session follows the org-crypt settings, and asks for the passphrase of an encrypted file
30/// it was asked to open.
31struct CryptSettings: ViewModifier {
32 let session: DocumentSession
33 let cryptBeforeSave: Bool
34 let cryptTagMatcher: String
35 let unlock: (String) -> Void
36
37 func body(content: Content) -> some View {
38 content
39 .onChange(of: cryptBeforeSave, initial: true) { _, on in session.encryptsTaggedEntries = on }
40 .onChange(of: cryptTagMatcher, initial: true) { session.cryptTag = OrgPreferences.cryptTag() }
41 .onChange(of: session.unlockRequest, initial: true) { _, path in
42 if let path { unlock(path) }
43 }
44 }
45}