Sources/Orgstar/CaptureView.swift
279 lines · 12234 bytes
1import AppKit
2import Carbon.HIToolbox
3import OrgApp
4import OrgCore
5import SwiftUI
6
7/// The capture window: pick a template by its key, answer its prompts, edit the entry, and
8/// ⌘Return files it where the template says, as `org-capture` does.
9struct CaptureView: View {
10 let workspace: WorkspaceModel
11 let session: DocumentSession
12 let commands: AppCommands
13 let clock: ClockModel
14 @State private var templates = CaptureTemplates.defaults
15 @State private var problem: String?
16 @State private var template: CaptureTemplate?
17 @State private var questions: [Capture.Question] = []
18 @State private var answers: [String] = []
19 @State private var text = ""
20 @State private var selection: TextSelection?
21 @State private var context = CaptureContext()
22 @State private var editing = false
23 /// The template keys typed so far, for multi-key templates such as "wb".
24 @State private var keys = ""
25 /// The agenda day the entry is captured for, as `Days` numbers it.
26 @State private var day: Int?
27 @FocusState private var focused: Bool
28 @Environment(\.dismissWindow) private var dismissWindow
29
30 var body: some View {
31 VStack(alignment: .leading, spacing: 12) {
32 if let template, editing {
33 Text("\(template.name) → \(Self.target(template))").font(.headline)
34 TextEditor(text: $text, selection: $selection)
35 .font(.body.monospaced())
36 .focused($focused)
37 .frame(minHeight: 140)
38 HStack {
39 Spacer()
40 Button("Cancel", role: .cancel) { close() }
41 .keyboardShortcut(.cancelAction)
42 Button("File It") { finish() }
43 .keyboardShortcut(.return, modifiers: .command)
44 }
45 } else if let template, !questions.isEmpty {
46 Text(template.name).font(.headline)
47 Form {
48 ForEach(questions.indices, id: \.self) { index in
49 let question = questions[index]
50 TextField(question.prompt, text: $answers[index], prompt: Text(placeholder(question)))
51 if !question.choices.isEmpty {
52 Text(question.choices.prefix(12).joined(separator: " ")).font(.caption).foregroundStyle(.secondary)
53 }
54 }
55 }
56 HStack {
57 Spacer()
58 Button("Cancel", role: .cancel) { close() }
59 .keyboardShortcut(.cancelAction)
60 Button("Continue") { expand() }
61 .keyboardShortcut(.defaultAction)
62 }
63 } else {
64 Text((keys.isEmpty ? "Capture" : "Capture: \(keys)") + (day == nil ? "" : ", scheduled " + captureDate(Date()).formatted(.dateTime.weekday(.abbreviated).day().month(.abbreviated))))
65 .font(.headline)
66 ForEach(templates.filter { $0.key.hasPrefix(keys) }) { template in
67 Button {
68 choose(template)
69 } label: {
70 HStack {
71 Text("[\(template.key)]").monospaced().foregroundStyle(.secondary)
72 Text(template.name)
73 Spacer()
74 Text(Self.target(template)).foregroundStyle(.secondary).font(.caption)
75 }
76 }
77 .buttonStyle(.plain)
78 }
79 }
80 if let problem { Text(problem).font(.caption).foregroundStyle(.red) }
81 }
82 .padding(16)
83 .frame(width: 520)
84 .onKeyPress(phases: .down) { press in
85 guard template == nil else { return .ignored }
86 if press.key == .delete, !keys.isEmpty {
87 keys.removeLast()
88 return .handled
89 }
90 // Keys narrow the list until they name a template, as `org-capture` selects one.
91 let typed = keys + press.characters
92 if let match = templates.first(where: { $0.key == typed }) {
93 choose(match)
94 } else if !press.characters.isEmpty, templates.contains(where: { $0.key.hasPrefix(typed) }) {
95 keys = typed
96 } else {
97 return .ignored
98 }
99 return .handled
100 }
101 .onAppear {
102 reset()
103 takeRequest()
104 }
105 .onChange(of: commands.captureRequest?.serial) {
106 guard commands.captureRequest != nil else { return }
107 reset()
108 takeRequest()
109 }
110 }
111
112 /// What an empty answer means.
113 private func placeholder(_ question: Capture.Question) -> String {
114 switch question.kind {
115 case .text, .property, .clipboard: question.defaultValue
116 case .tags: "tag:tag"
117 case .date(let withTime, _): withTime ? "now; or +2d, fri 14:00" : "today; or +2d, fri"
118 }
119 }
120
121 static func target(_ template: CaptureTemplate) -> String {
122 let file = (template.target.file as NSString).lastPathComponent
123 switch template.target {
124 case .file, .heading: return file
125 case .headline(_, let headline): return "\(file) / \(headline)"
126 case .outline(_, let path): return ([file] + path).joined(separator: " / ")
127 case .datetree(_, let path, _): return ([file] + path + ["date tree"]).joined(separator: " / ")
128 case .id(let id): return "ID \(id)"
129 case .clock: return "clocked entry"
130 }
131 }
132
133 private func reset() {
134 let loaded = CaptureTemplates.load()
135 templates = loaded.templates
136 problem = loaded.problems.first
137 template = nil
138 editing = false
139 keys = ""
140 day = nil
141 let clocked = clock.running
142 let clockFile = clocked.map { Links.abbreviate($0.path, home: NSHomeDirectory()) }
143 context = CaptureContext(
144 initial: commands.editor?.selectedText ?? "",
145 annotation: session.captureAnnotation(),
146 clipboard: NSPasteboard.general.string(forType: .string) ?? "",
147 file: session.path ?? "",
148 userName: NSFullUserName(),
149 clockHeading: clocked?.heading ?? "",
150 clockLink: clocked.map { "[[file:\(clockFile!)::*\($0.heading)][\($0.heading)]]" } ?? "",
151 allTags: (try? workspace.index.tags()) ?? [],
152 tagsColumn: UserDefaults.standard.object(forKey: "tagsColumn") as? Int ?? -77
153 )
154 context.readsTemplateFiles()
155 }
156
157 /// An org-protocol capture: its link properties fill `%a`, `%i` and `%:…`, and its
158 /// template, when it names one, is chosen.
159 private func takeRequest() {
160 guard let request = commands.captureRequest else { return }
161 commands.captureRequest = nil
162 day = request.day
163 context.initial = request.properties["initial"] ?? ""
164 context.annotation = request.properties["annotation"] ?? ""
165 context.linkProperties = request.properties
166 if let key = request.template {
167 if let match = templates.first(where: { $0.key == key }) { choose(match) } else { problem = "No capture template \"\(key)\"" }
168 }
169 }
170
171 private func choose(_ chosen: CaptureTemplate) {
172 template = chosen
173 keys = ""
174 // `%^g` offers the target file's tags.
175 let path = DocumentSession.resolve(chosen.target.file, root: workspace.rootPaths.first)
176 if !chosen.target.file.isEmpty, let text = session.openDocuments.first(where: { $0.path == path })?.document.text ?? (try? String(contentsOfFile: path, encoding: .utf8)) {
177 var tags: [String] = []
178 for heading in DocumentModel(tree: OrgParser.parse(text)).headings { for tag in heading.localTags where !tags.contains(tag) { tags.append(tag) } }
179 context.fileTags = tags
180 }
181 questions = Capture.questions(chosen.template, context: context)
182 answers = questions.map { _ in "" }
183 if questions.isEmpty { expand() }
184 }
185
186 private func expand() {
187 guard let template else { return }
188 context.now = captureDate(Date())
189 switch Capture.fill(template.template, context: context, answers: answers) {
190 case .failure(.message(let message)):
191 problem = message
192 self.template = nil
193 case .success(var filled):
194 if let day { filled = Capture.scheduling(filled.text, cursor: filled.cursor, on: day) }
195 text = filled.text
196 if template.options.immediateFinish {
197 finish()
198 return
199 }
200 let index = String.Index(utf16Offset: filled.cursor, in: text)
201 selection = TextSelection(insertionPoint: index)
202 editing = true
203 focused = true
204 }
205 }
206
207 /// `now` on the agenda day captured for, as `org-overriding-default-time` sets `%t`, `%U`
208 /// and the date tree's day.
209 private func captureDate(_ now: Date) -> Date {
210 guard let day else { return now }
211 let d = Days.date(day)
212 let time = Calendar.current.dateComponents([.hour, .minute, .second], from: now)
213 return Calendar.current.date(from: DateComponents(year: d.year, month: d.month, day: d.day, hour: time.hour, minute: time.minute, second: time.second)) ?? now
214 }
215
216 private func finish() {
217 guard let template else { return }
218 let opened = context.now
219 let now = Date()
220 let placed = template.resolved(workspace: workspace, session: session, clock: clock).flatMap { resolved in
221 session.capture(text, template: resolved.template, root: workspace.rootPaths.first, file: resolved.file, now: captureDate(now))
222 }
223 switch placed {
224 case .failure(let error):
225 problem = error.message
226 if template.options.immediateFinish { editing = true }
227 return
228 case .success(let entry):
229 commands.show("Captured to \(Self.target(template))")
230 let options = template.options
231 let paths = workspace.files.filter { $0.kind == .org }.map(\.path)
232 Task { [commands, clock, session] in
233 let outcome = await clock.captured(entry, options: options, session: session, paths: paths, ask: commands.askClock, opened: opened, now: now)
234 for message in outcome.messages { commands.show(message) }
235 if let jump = outcome.jump {
236 commands.jump(path: jump.path, offset: jump.offset)
237 } else if options.jumpToCaptured {
238 commands.jump(path: entry.path, offset: entry.offset)
239 }
240 }
241 }
242 close()
243 }
244
245 private func close() {
246 reset()
247 dismissWindow(id: "capture")
248 }
249}
250
251/// A system-wide shortcut, registered with Carbon so it needs no accessibility permission.
252@MainActor
253final class GlobalHotKey {
254 private var reference: EventHotKeyRef?
255 private var handler: EventHandlerRef?
256 private let action: () -> Void
257
258 /// ⌃⌥Space.
259 init(action: @escaping () -> Void) {
260 self.action = action
261 var spec = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed))
262 let me = Unmanaged.passUnretained(self).toOpaque()
263 InstallEventHandler(GetApplicationEventTarget(), { _, _, data in
264 guard let data else { return noErr }
265 let hotKey = Unmanaged<GlobalHotKey>.fromOpaque(data).takeUnretainedValue()
266 MainActor.assumeIsolated { hotKey.action() }
267 return noErr
268 }, 1, &spec, me, &handler)
269 let id = EventHotKeyID(signature: OSType(0x4F52_4753), id: 1)
270 RegisterEventHotKey(UInt32(kVK_Space), UInt32(controlKey | optionKey), id, GetApplicationEventTarget(), 0, &reference)
271 }
272
273 func unregister() {
274 if let reference { UnregisterEventHotKey(reference) }
275 if let handler { RemoveEventHandler(handler) }
276 reference = nil
277 handler = nil
278 }
279}