Sources/Orgstar/VersionViews.swift
cmc One capitalisation rule: title case for titles and buttons, sentence case for settings
7c1313ad9f 10 hours ago177 lines · 7178 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("Decide Later") { 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 Button("Close") { dismiss() }.keyboardShortcut(.cancelAction)
74 Spacer()
75 if let selected {
76 Button("Keep File") { resolve(selected, .keepFile) }
77 Button("Merge with Markers") { resolve(selected, .merge) }
78 Button("Use Copy") { resolve(selected, .useCopy) }
79 }
80 }
81 }
82 .padding()
83 .frame(minWidth: 640, minHeight: 440)
84 .onAppear(perform: reload)
85 }
86
87 private func reload() {
88 copies = session.syncConflictCopies()
89 if selected.map({ !copies.contains($0) }) ?? true { selected = copies.first }
90 }
91
92 private func resolve(_ copy: URL, _ resolution: DocumentSession.CopyResolution) {
93 if let message = session.resolve(copy: copy, resolution) { report(message) }
94 reload()
95 if copies.isEmpty { dismiss() }
96 }
97}
98
99/// Versions of the open file kept in the recovery folder, newest first, against the buffer.
100struct RecoverySheet: View {
101 let session: DocumentSession
102 let report: (String) -> Void
103 @State private var versions: [(url: URL, date: Date, label: String)] = []
104 @State private var selected: URL?
105 @Environment(\.dismiss) private var dismiss
106
107 var body: some View {
108 Group {
109 if versions.isEmpty {
110 VStack {
111 ContentUnavailableView(
112 "No Recovery Versions", systemImage: "clock.arrow.circlepath",
113 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.")
114 )
115 HStack {
116 Spacer()
117 Button("Close") { dismiss() }.keyboardShortcut(.cancelAction)
118 }
119 }
120 .padding()
121 } else {
122 versionList
123 }
124 }
125 .frame(minWidth: 760, minHeight: 460)
126 .onAppear {
127 versions = session.recoveryVersions()
128 selected = versions.first?.url
129 }
130 }
131
132 private var versionList: some View {
133 HSplitView {
134 List(versions, id: \.url, selection: $selected) { version in
135 VStack(alignment: .leading) {
136 Text(version.date.formatted(date: .abbreviated, time: .standard))
137 Text(Self.describe(version.label)).font(.caption).foregroundStyle(.secondary)
138 }
139 .tag(version.url)
140 }
141 .frame(minWidth: 200, idealWidth: 220)
142 VStack(alignment: .leading, spacing: 10) {
143 Text("Lines marked - are in the buffer, + in the kept version.").foregroundStyle(.secondary)
144 if let selected, let kept = session.recoveryText(selected), let mine = session.document?.text {
145 ThemedDiff(old: mine, new: kept)
146 } else {
147 Spacer()
148 }
149 HStack {
150 Button("Close") { dismiss() }.keyboardShortcut(.cancelAction)
151 if let selected {
152 Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([selected]) }
153 }
154 Spacer()
155 if let selected {
156 Button("Restore") {
157 if let message = session.restore(selected) { report(message) } else { report("Restored; undo brings back what was there.") }
158 dismiss()
159 }
160 .keyboardShortcut(.defaultAction)
161 }
162 }
163 }
164 .padding()
165 }
166 }
167
168 static func describe(_ label: String) -> String {
169 switch label {
170 case "local": "Your version, replaced"
171 case "external": "Disk version, replaced"
172 case "sync-conflict": "Sync conflict copy"
173 case "snapshot": "Snapshot, before a save"
174 default: label
175 }
176 }
177}