Sources/Orgstar/VersionViews.swift
225 lines · 9161 bytes
9 symbols in this file
1import AppKit
2import OrgApp
3import OrgDocument
4import OrgPresentation
5#if !APPSTORE
6import OrgPrograms
7#endif
8import SwiftUI
9
10/// A diff in the theme's colours: removed lines in its TODO red, added ones in its DONE green.
11struct ThemedDiff: View {
12 let old: String
13 let new: String
14 @Environment(\.orgTheme) private var theme
15
16 var body: some View {
17 DiffView(
18 old: old, new: new, removed: theme.color("todo") ?? .red, added: theme.color("done") ?? .green,
19 foreground: theme.color("foreground"), background: theme.color("background")
20 )
21 }
22}
23
24/// The buffer and the file on disk changed differently: what differs, and how to settle it.
25struct ConflictSheet: View {
26 let session: DocumentSession
27 @Environment(\.dismiss) private var dismiss
28
29 var body: some View {
30 VStack(alignment: .leading, spacing: 10) {
31 Text("Conflicting Changes").font(.headline)
32 Text((session.conflict ?? "") + " Lines marked - are on disk, + in your version. The version you don't keep is saved in the recovery folder.")
33 .foregroundStyle(.secondary)
34 .fixedSize(horizontal: false, vertical: true)
35 if let versions = session.conflictVersions() {
36 ThemedDiff(old: versions.disk, new: versions.mine)
37 }
38 HStack {
39 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
40 Spacer()
41 Button("Use Disk Version") { session.useDiskVersion(); dismiss() }
42 Button("Merge with Markers") { session.mergeConflictWithMarkers(); dismiss() }
43 .help("Both sets of changes in the buffer, conflicting lines between <<<<<<< and >>>>>>>")
44 Button("Keep Mine") { session.keepMine(); dismiss() }.keyboardShortcut(.defaultAction)
45 }
46 }
47 .padding()
48 .frame(minWidth: 640, minHeight: 440)
49 }
50}
51
52/// Syncthing's conflict copies of the open file: each against the file, and what to keep.
53struct SyncConflictSheet: View {
54 let session: DocumentSession
55 let report: (String) -> Void
56 @State private var copies: [URL] = []
57 @State private var selected: URL?
58 @Environment(\.dismiss) private var dismiss
59
60 var body: some View {
61 VStack(alignment: .leading, spacing: 10) {
62 Text("Sync Conflict Copies").font(.headline)
63 if copies.isEmpty {
64 Text("No conflict copies of this file.").foregroundStyle(.secondary)
65 } else {
66 Picker("Copy", selection: $selected) {
67 ForEach(copies, id: \.self) { Text($0.lastPathComponent).tag(Optional($0)) }
68 }
69 Text("Lines marked - are in the file, + in the copy. The copy goes to the recovery folder when it's resolved.")
70 .foregroundStyle(.secondary)
71 if let selected, let copy = session.recoveryText(selected), let mine = session.document?.text {
72 ThemedDiff(old: mine, new: copy)
73 }
74 }
75 HStack {
76 if copies.isEmpty {
77 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
78 } else {
79 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
80 }
81 Spacer()
82 if let selected {
83 Button("Keep File") { resolve(selected, .keepFile) }
84 Button("Merge with Markers") { resolve(selected, .merge) }
85 Button("Use Copy") { resolve(selected, .useCopy) }
86 }
87 }
88 }
89 .padding()
90 .frame(minWidth: 640, minHeight: 440)
91 .onAppear(perform: reload)
92 }
93
94 private func reload() {
95 copies = session.syncConflictCopies()
96 if selected.map({ !copies.contains($0) }) ?? true { selected = copies.first }
97 }
98
99 private func resolve(_ copy: URL, _ resolution: DocumentSession.CopyResolution) {
100 if let message = session.resolve(copy: copy, resolution) { report(message) }
101 reload()
102 if copies.isEmpty { dismiss() }
103 }
104}
105
106/// The open file's history, newest first, each version against the buffer: the versions kept
107/// in the recovery folder and, in the download version where the folder is a git repository,
108/// the commits that changed the file (`git log --follow`).
109struct HistorySheet: View {
110 let session: DocumentSession
111 let report: (String) -> Void
112 @State private var entries: [FileHistory.Entry] = []
113 @State private var selected: FileHistory.Entry?
114 /// The selected version's text, once read.
115 @State private var text: String?
116 @Environment(\.dismiss) private var dismiss
117
118 var body: some View {
119 Group {
120 if entries.isEmpty {
121 VStack {
122 ContentUnavailableView(
123 "No History", systemImage: "clock.arrow.circlepath",
124 description: Text("orgstar keeps a copy of this file here when it replaces a version, and a snapshot before a save at most once an hour.")
125 )
126 HStack {
127 Spacer()
128 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
129 }
130 }
131 .padding()
132 } else {
133 versionList
134 }
135 }
136 .frame(minWidth: 760, minHeight: 460)
137 .task {
138 let recovery = session.recoveryVersions()
139 entries = FileHistory.merged(recovery: recovery, commits: [])
140 selected = entries.first
141 #if !APPSTORE
142 guard let path = session.path else { return }
143 let commits = await GitHistory.commits(for: URL(fileURLWithPath: path))
144 guard !commits.isEmpty else { return }
145 entries = FileHistory.merged(recovery: recovery, commits: commits)
146 if selected == nil { selected = entries.first }
147 #endif
148 }
149 .task(id: selected) {
150 text = nil
151 if let selected { text = await load(selected) }
152 }
153 }
154
155 /// The text of `entry`: a kept file, or the file in a commit.
156 private func load(_ entry: FileHistory.Entry) async -> String? {
157 switch entry {
158 case .recovery(let url, _, _): return session.recoveryText(url)
159 case .commit(let commit):
160 #if APPSTORE
161 return nil
162 #else
163 return await GitHistory.text(of: commit)
164 #endif
165 }
166 }
167
168 private var versionList: some View {
169 HSplitView {
170 List(entries, id: \.self, selection: $selected) { entry in
171 row(entry).tag(entry)
172 }
173 .frame(minWidth: 220, idealWidth: 260)
174 VStack(alignment: .leading, spacing: 10) {
175 Text("History").font(.headline)
176 Text("Lines marked - are in the buffer, + in the selected version.").foregroundStyle(.secondary)
177 if let text, let mine = session.document?.text {
178 ThemedDiff(old: mine, new: text)
179 } else {
180 Spacer()
181 }
182 HStack {
183 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
184 if case .recovery(let url, _, _) = selected {
185 Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([url]) }
186 }
187 Spacer()
188 if let text {
189 Button("Restore") {
190 if let message = session.restore(text: text) { report(message) } else { report("Restored; undo brings back what was there.") }
191 dismiss()
192 }
193 .keyboardShortcut(.defaultAction)
194 }
195 }
196 }
197 .padding()
198 }
199 }
200
201 @ViewBuilder
202 private func row(_ entry: FileHistory.Entry) -> some View {
203 VStack(alignment: .leading) {
204 Text(entry.date.formatted(date: .abbreviated, time: .standard))
205 switch entry {
206 case .recovery(_, _, let label):
207 Text(Self.describe(label)).font(.caption).foregroundStyle(.secondary)
208 case .commit(let commit):
209 Text(verbatim: commit.subject).font(.caption).lineLimit(1)
210 Text(verbatim: "\(commit.hash.prefix(7)) · \(commit.author)").font(.caption.monospaced()).foregroundStyle(.secondary)
211 }
212 }
213 }
214
215 static func describe(_ label: String) -> String {
216 switch label {
217 case "local": "Your version, replaced"
218 case "external": "Disk version, replaced"
219 case "sync-conflict": "Sync conflict copy"
220 case "snapshot": "Snapshot, before a save"
221 case "replace": String(localized: "Before a change across files", comment: "Recovery version kept before Replace in Files or a rename changed the file")
222 default: label
223 }
224 }
225}