Commit 3ae217cf9d
Verified · cmc
Sources/OrgApp/CryptSheet.swift +57 −5 · view
| @@ -84,7 +84,8 @@ public struct CryptSheet: View { | ||
| 84 | 84 | self.done = done |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | public var body: some View { | |
| 87 | #if os(macOS) | |
| 88 | private var layout: some View { | |
| 88 | 89 | VStack(alignment: .leading, spacing: 12) { |
| 89 | 90 | if editing != nil { |
| 90 | 91 | Text("Encrypted Entry").font(.headline) |
| @@ -114,11 +115,62 @@ public struct CryptSheet: View { | ||
| 114 | 115 | } |
| 115 | 116 | } |
| 116 | 117 | .padding(20) |
| 117 | #if os(macOS) | |
| 118 | 118 | .frame(minWidth: 460) |
| 119 | #else | |
| 120 | .frame(maxHeight: .infinity, alignment: .top) | |
| 121 | #endif | |
| 119 | } | |
| 120 | #else | |
| 121 | @FocusState private var fieldFocused: Bool | |
| 122 | ||
| 123 | /// The editor's sheet: a navigation bar with Cancel and the action, the fields in a form. | |
| 124 | private var layout: some View { | |
| 125 | Form { | |
| 126 | if editing != nil { | |
| 127 | Section { | |
| 128 | TextEditor(text: $plaintext) | |
| 129 | .font(.body.monospaced()) | |
| 130 | .frame(minHeight: 220) | |
| 131 | } footer: { | |
| 132 | if let problem { Text(problem).foregroundStyle(.red) } | |
| 133 | } | |
| 134 | Section { | |
| 135 | Button("Decrypt in File") { decryptEdited() } | |
| 136 | } | |
| 137 | } else { | |
| 138 | Section { | |
| 139 | SecureField("Passphrase", text: $passphrase) | |
| 140 | .focused($fieldFocused) | |
| 141 | .submitLabel(.go) | |
| 142 | .onSubmit(submit) | |
| 143 | if asksTwice { SecureField("Repeat passphrase", text: $repeated).submitLabel(.go).onSubmit(submit) } | |
| 144 | } footer: { | |
| 145 | VStack(alignment: .leading, spacing: 6) { | |
| 146 | if let explanation { Text(explanation) } | |
| 147 | if let problem { Text(problem).foregroundStyle(.red) } | |
| 148 | } | |
| 149 | } | |
| 150 | } | |
| 151 | } | |
| 152 | .navigationTitle(editing != nil ? String(localized: "Encrypted Entry") : title) | |
| 153 | .toolbar { | |
| 154 | ToolbarItem(placement: .cancellationAction) { | |
| 155 | Button("Cancel", role: .cancel) { if editing != nil { done(nil) } else { cancel() } } | |
| 156 | } | |
| 157 | ToolbarItem(placement: .confirmationAction) { | |
| 158 | if editing != nil { | |
| 159 | Button("Save") { save() } | |
| 160 | } else { | |
| 161 | Button(request.encrypting ? "Encrypt" : "Decrypt") { submit() }.disabled(passphrase.isEmpty) | |
| 162 | } | |
| 163 | } | |
| 164 | } | |
| 165 | .presentationDetents(editing != nil ? [.large] : [.medium, .large]) | |
| 166 | .onAppear { fieldFocused = true } | |
| 167 | // A wrong passphrase: the field again, to type it over. | |
| 168 | .onChange(of: problem) { if problem != nil, editing == nil { fieldFocused = true } } | |
| 169 | } | |
| 170 | #endif | |
| 171 | ||
| 172 | public var body: some View { | |
| 173 | layout | |
| 122 | 174 | .task { |
| 123 | 175 | if let stored = await passphrases.stored(for: request.path) { run(stored, remembered: true) } |
| 124 | 176 | } |