Sources/Orgstar/VersionViews.swift
182 lines · 7397 bytes
1import AppKit
2import OrgApp
3import OrgDocument
4import OrgPresentation
5import SwiftUI
6
7/// A diff in the theme's colours: removed lines in its TODO red, added ones in its DONE green.
8struct ThemedDiff: View {
9 let old: String
10 let new: String
11 @Environment(\.orgTheme) private var theme
12
13 var body: some View {
14 DiffView(
15 old: old, new: new, removed: theme.color("todo") ?? .red, added: theme.color("done") ?? .green,
16 foreground: theme.color("foreground"), background: theme.color("background")
17 )
18 }
19}
20
21/// The buffer and the file on disk changed differently: what differs, and how to settle it.
22struct ConflictSheet: View {
23 let session: DocumentSession
24 @Environment(\.dismiss) private var dismiss
25
26 var body: some View {
27 VStack(alignment: .leading, spacing: 10) {
28 Text("Conflicting Changes").font(.headline)
29 Text((session.conflict ?? "") + " Lines marked - are on disk, + in your version. The version you don't keep is saved in the recovery folder.")
30 .foregroundStyle(.secondary)
31 .fixedSize(horizontal: false, vertical: true)
32 if let versions = session.conflictVersions() {
33 ThemedDiff(old: versions.disk, new: versions.mine)
34 }
35 HStack {
36 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
37 Spacer()
38 Button("Use Disk Version") { session.useDiskVersion(); dismiss() }
39 Button("Merge with Markers") { session.mergeConflictWithMarkers(); dismiss() }
40 .help("Both sets of changes in the buffer, conflicting lines between <<<<<<< and >>>>>>>")
41 Button("Keep Mine") { session.keepMine(); dismiss() }.keyboardShortcut(.defaultAction)
42 }
43 }
44 .padding()
45 .frame(minWidth: 640, minHeight: 440)
46 }
47}
48
49/// Syncthing's conflict copies of the open file: each against the file, and what to keep.
50struct SyncConflictSheet: View {
51 let session: DocumentSession
52 let report: (String) -> Void
53 @State private var copies: [URL] = []
54 @State private var selected: URL?
55 @Environment(\.dismiss) private var dismiss
56
57 var body: some View {
58 VStack(alignment: .leading, spacing: 10) {
59 Text("Sync Conflict Copies").font(.headline)
60 if copies.isEmpty {
61 Text("No conflict copies of this file.").foregroundStyle(.secondary)
62 } else {
63 Picker("Copy", selection: $selected) {
64 ForEach(copies, id: \.self) { Text($0.lastPathComponent).tag(Optional($0)) }
65 }
66 Text("Lines marked - are in the file, + in the copy. The copy goes to the recovery folder when it's resolved.")
67 .foregroundStyle(.secondary)
68 if let selected, let copy = session.recoveryText(selected), let mine = session.document?.text {
69 ThemedDiff(old: mine, new: copy)
70 }
71 }
72 HStack {
73 if copies.isEmpty {
74 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
75 } else {
76 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
77 }
78 Spacer()
79 if let selected {
80 Button("Keep File") { resolve(selected, .keepFile) }
81 Button("Merge with Markers") { resolve(selected, .merge) }
82 Button("Use Copy") { resolve(selected, .useCopy) }
83 }
84 }
85 }
86 .padding()
87 .frame(minWidth: 640, minHeight: 440)
88 .onAppear(perform: reload)
89 }
90
91 private func reload() {
92 copies = session.syncConflictCopies()
93 if selected.map({ !copies.contains($0) }) ?? true { selected = copies.first }
94 }
95
96 private func resolve(_ copy: URL, _ resolution: DocumentSession.CopyResolution) {
97 if let message = session.resolve(copy: copy, resolution) { report(message) }
98 reload()
99 if copies.isEmpty { dismiss() }
100 }
101}
102
103/// Versions of the open file kept in the recovery folder, newest first, against the buffer.
104struct RecoverySheet: View {
105 let session: DocumentSession
106 let report: (String) -> Void
107 @State private var versions: [(url: URL, date: Date, label: String)] = []
108 @State private var selected: URL?
109 @Environment(\.dismiss) private var dismiss
110
111 var body: some View {
112 Group {
113 if versions.isEmpty {
114 VStack {
115 ContentUnavailableView(
116 "No Recovery Versions", systemImage: "clock.arrow.circlepath",
117 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.")
118 )
119 HStack {
120 Spacer()
121 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
122 }
123 }
124 .padding()
125 } else {
126 versionList
127 }
128 }
129 .frame(minWidth: 760, minHeight: 460)
130 .onAppear {
131 versions = session.recoveryVersions()
132 selected = versions.first?.url
133 }
134 }
135
136 private var versionList: some View {
137 HSplitView {
138 List(versions, id: \.url, selection: $selected) { version in
139 VStack(alignment: .leading) {
140 Text(version.date.formatted(date: .abbreviated, time: .standard))
141 Text(Self.describe(version.label)).font(.caption).foregroundStyle(.secondary)
142 }
143 .tag(version.url)
144 }
145 .frame(minWidth: 200, idealWidth: 220)
146 VStack(alignment: .leading, spacing: 10) {
147 Text("Recovery Versions").font(.headline)
148 Text("Lines marked - are in the buffer, + in the kept version.").foregroundStyle(.secondary)
149 if let selected, let kept = session.recoveryText(selected), let mine = session.document?.text {
150 ThemedDiff(old: mine, new: kept)
151 } else {
152 Spacer()
153 }
154 HStack {
155 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
156 if let selected {
157 Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([selected]) }
158 }
159 Spacer()
160 if let selected {
161 Button("Restore") {
162 if let message = session.restore(selected) { report(message) } else { report("Restored; undo brings back what was there.") }
163 dismiss()
164 }
165 .keyboardShortcut(.defaultAction)
166 }
167 }
168 }
169 .padding()
170 }
171 }
172
173 static func describe(_ label: String) -> String {
174 switch label {
175 case "local": "Your version, replaced"
176 case "external": "Disk version, replaced"
177 case "sync-conflict": "Sync conflict copy"
178 case "snapshot": "Snapshot, before a save"
179 default: label
180 }
181 }
182}