Sources/Orgstar/WelcomeView.swift

75 lines · 3261 bytes

 1import OrgApp
 2import OrgCore
 3import SwiftUI
 4
 5/// The first launch, with no folder added: add a folder or start one, and pick the keys.
 6struct WelcomeView: View {
 7    let workspace: WorkspaceModel
 8    /// Opens the folder picker once the sheet has closed.
 9    let chooseFolder: () -> Void
10    /// Shows the file the sheet made.
11    let open: (String) -> Void
12    @AppStorage("keymap") private var preset = KeymapPreset.emacs
13    @State private var error: String?
14    @Environment(\.dismiss) private var dismiss
15
16    /// iCloud Drive's folder, when iCloud Drive is on for this user.
17    static var iCloudDrive: URL? {
18        let url = FileManager.default.homeDirectoryForCurrentUser
19            .appendingPathComponent("Library/Mobile Documents/com~apple~CloudDocs")
20        return FileManager.default.fileExists(atPath: url.path) ? url : nil
21    }
22
23    var body: some View {
24        VStack(alignment: .leading, spacing: 14) {
25            Text("Welcome to orgstar").font(.title2.bold())
26            Text("orgstar edits a folder of plain-text org files in place. Add the folder your notes are in, or start a new one. Nothing is copied or moved, and you can add more folders later with File ▸ Add Folder….")
27            Picker("Keys", selection: $preset) {
28                ForEach(KeymapPreset.allCases, id: \.self) { Text($0.title).tag($0) }
29            }
30            Text(presetNote).font(.callout).foregroundStyle(.secondary)
31            if let error { Text(error).foregroundStyle(.red) }
32            HStack {
33                Button("Not Now") { dismiss() }
34                Spacer()
35                if let drive = Self.iCloudDrive {
36                    Button("New Folder in iCloud Drive") { start(in: drive.appendingPathComponent("org")) }
37                        .help("Makes org/notes.org in iCloud Drive and adds the folder")
38                }
39                Button("Choose Folder…") {
40                    dismiss()
41                    chooseFolder()
42                }
43                .keyboardShortcut(.defaultAction)
44            }
45        }
46        .padding(20)
47        .frame(width: 480)
48    }
49
50    private var presetNote: LocalizedStringKey {
51        switch preset {
52        case .emacs: "Org's own bindings: C-c C-t cycles TODO, C-c C-s schedules, M-RET makes a heading."
53        case .mac: "Standard Mac editing keys, with Org commands on ⌃⌘: ⌃⌘T cycles TODO, ⌃⌘S schedules."
54        case .doom: "Modal editing as in Doom Emacs, with SPC as the leader: SPC m t cycles TODO."
55        }
56    }
57
58    /// Makes `folder` with a notes file in it, adds it, and opens the file.
59    private func start(in folder: URL) {
60        Task {
61            do {
62                let file = folder.appendingPathComponent("notes.org")
63                try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
64                if !FileManager.default.fileExists(atPath: file.path) {
65                    try "* Inbox\n".write(to: file, atomically: true, encoding: .utf8)
66                }
67                try await workspace.addRoot(folder)
68                open(file.standardizedFileURL.path)
69                dismiss()
70            } catch {
71                self.error = error.localizedDescription
72            }
73        }
74    }
75}