Sources/Orgstar/VersionViews.swift
cmc Board: keyword colours as in the agenda; theme colours in the diff and board
7e86cdaf6e 11 hours ago157 lines · 6451 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 HSplitView {
109 List(versions, id: \.url, selection: $selected) { version in
110 VStack(alignment: .leading) {
111 Text(version.date.formatted(date: .abbreviated, time: .standard))
112 Text(Self.describe(version.label)).font(.caption).foregroundStyle(.secondary)
113 }
114 .tag(version.url)
115 }
116 .frame(minWidth: 200, idealWidth: 220)
117 VStack(alignment: .leading, spacing: 10) {
118 Text("Lines marked - are in the buffer, + in the kept version.").foregroundStyle(.secondary)
119 if let selected, let kept = session.recoveryText(selected), let mine = session.document?.text {
120 ThemedDiff(old: mine, new: kept)
121 } else {
122 Spacer()
123 }
124 HStack {
125 Button("Close") { dismiss() }.keyboardShortcut(.cancelAction)
126 if let selected {
127 Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([selected]) }
128 }
129 Spacer()
130 if let selected {
131 Button("Restore") {
132 if let message = session.restore(selected) { report(message) } else { report("Restored; undo brings back what was there.") }
133 dismiss()
134 }
135 .keyboardShortcut(.defaultAction)
136 }
137 }
138 }
139 .padding()
140 }
141 .frame(minWidth: 760, minHeight: 460)
142 .onAppear {
143 versions = session.recoveryVersions()
144 selected = versions.first?.url
145 }
146 }
147
148 static func describe(_ label: String) -> String {
149 switch label {
150 case "local": "Your version, replaced"
151 case "external": "Disk version, replaced"
152 case "sync-conflict": "Sync conflict copy"
153 case "snapshot": "Snapshot, before a save"
154 default: label
155 }
156 }
157}