Sources/Orgstar/VersionViews.swift

design-themes-widgets
orgstar/Sources/Orgstar/VersionViews.swift history · blame · raw

142 lines · 5952 bytes

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