Sources/Orgstar/WelcomeView.swift

104 lines · 4969 bytes

3 symbols in this file
  1import OrgApp
  2import OrgCore
  3import OrgWorkspace
  4import SwiftUI
  5
  6/// The first launch, with no folder added: add a folder or start one, and pick the keys.
  7struct WelcomeView: View {
  8    let workspace: WorkspaceModel
  9    /// Opens the folder picker once the sheet has closed.
 10    let chooseFolder: () -> Void
 11    /// Shows the file the sheet made.
 12    let open: (String) -> Void
 13    @AppStorage("keymap") private var preset = KeymapPreset.emacs
 14    @State private var error: String?
 15    @Environment(\.dismiss) private var dismiss
 16
 17    /// iCloud Drive's folder in the real home folder.
 18    static let iCloudDriveFolder = URL(fileURLWithPath: OrgPaths.home).appendingPathComponent("Library/Mobile Documents/com~apple~CloudDocs")
 19
 20    /// iCloud Drive's folder, when iCloud Drive is on for this user.
 21    static var iCloudDrive: URL? {
 22        FileManager.default.fileExists(atPath: iCloudDriveFolder.path) ? iCloudDriveFolder : nil
 23    }
 24
 25    var body: some View {
 26        VStack(alignment: .leading, spacing: 14) {
 27            Text("Welcome to orgstar").font(.title2.bold())
 28            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….")
 29            Picker("Keys", selection: $preset) {
 30                ForEach(KeymapPreset.allCases, id: \.self) { Text($0.title).tag($0) }
 31            }
 32            Text(presetNote).font(.callout).foregroundStyle(.secondary)
 33            #if APPSTORE
 34            Text("orgstar keeps its settings in a folder of its own. To use the ones in ~/.config/orgstar, choose that folder in Settings ▸ General ▸ Choose Configuration Folder….")
 35                .font(.callout).foregroundStyle(.secondary)
 36            ForEach(workspace.unresolvedRoots, id: \.path) { root in
 37                HStack {
 38                    Label(OrgPaths.abbreviatingWithTilde(root.path), systemImage: "exclamationmark.triangle")
 39                        .help("orgstar can't open this folder")
 40                    Spacer()
 41                    Button("Grant Access…") { GrantAccess.regrant(root, workspace: workspace) }
 42                }
 43            }
 44            #endif
 45            if let error { Text(error).foregroundStyle(.red) }
 46            HStack {
 47                Button("Not Now") { dismiss() }
 48                Spacer()
 49                #if APPSTORE
 50                // The sandbox reaches iCloud Drive only through a folder chosen there.
 51                Button("New Folder in iCloud Drive…") { chooseInICloudDrive() }
 52                    .help("Makes notes.org in a folder you choose or make in iCloud Drive and adds the folder")
 53                #else
 54                if let drive = Self.iCloudDrive {
 55                    Button("New Folder in iCloud Drive") { start(in: drive.appendingPathComponent("org")) }
 56                        .help("Makes org/notes.org in iCloud Drive and adds the folder")
 57                }
 58                #endif
 59                Button("Choose Folder…") {
 60                    dismiss()
 61                    chooseFolder()
 62                }
 63                .keyboardShortcut(.defaultAction)
 64            }
 65        }
 66        .padding(20)
 67        .frame(width: 480)
 68    }
 69
 70    private var presetNote: LocalizedStringKey {
 71        switch preset {
 72        case .emacs: "Org's own bindings: C-c C-t cycles TODO, C-c C-s schedules, M-RET makes a heading."
 73        case .mac: "Standard Mac editing keys, with Org commands on ⌃⌘: ⌃⌘T cycles TODO, ⌃⌘S schedules."
 74        case .doom: "Modal editing as in Doom Emacs, with SPC as the leader: SPC m t cycles TODO."
 75        }
 76    }
 77
 78    #if APPSTORE
 79    /// An open panel in iCloud Drive; the folder chosen or made there becomes the notes folder.
 80    private func chooseInICloudDrive() {
 81        let message = String(localized: "Choose or make the folder for your notes in iCloud Drive.", comment: "Open panel on the welcome sheet")
 82        guard let folder = GrantAccess.choose(at: Self.iCloudDriveFolder, message: message, prompt: String(localized: "Use Folder", comment: "Open panel button on the welcome sheet")) else { return }
 83        start(in: folder)
 84    }
 85    #endif
 86
 87    /// Makes `folder` with a notes file in it, adds it, and opens the file.
 88    private func start(in folder: URL) {
 89        Task {
 90            do {
 91                let file = folder.appendingPathComponent("notes.org")
 92                try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
 93                if !FileManager.default.fileExists(atPath: file.path) {
 94                    try "* Inbox\n".write(to: file, atomically: true, encoding: .utf8)
 95                }
 96                try await workspace.addRoot(folder)
 97                open(file.standardizedFileURL.path)
 98                dismiss()
 99            } catch {
100                self.error = error.localizedDescription
101            }
102        }
103    }
104}