Sources/Orgstar/WelcomeView.swift

6fa43a3d81440242a647c235bf3a7f9fbc11426c
orgstar/Sources/Orgstar/WelcomeView.swift

116 lines · 5689 bytes

  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            Text("⇧⌘P finds any command. The Org menu lists them with their keys.").font(.callout).foregroundStyle(.secondary)
 34            #if APPSTORE
 35            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….")
 36                .font(.callout).foregroundStyle(.secondary)
 37            ForEach(workspace.unresolvedRoots, id: \.path) { root in
 38                HStack {
 39                    Label(OrgPaths.abbreviatingWithTilde(root.path), systemImage: "exclamationmark.triangle")
 40                        .help("orgstar can't open this folder")
 41                    Spacer()
 42                    Button("Grant Access…") { GrantAccess.regrant(root, workspace: workspace) }
 43                }
 44            }
 45            #endif
 46            if let error { Text(error).foregroundStyle(.red) }
 47            HStack {
 48                Button("Not Now") { dismiss() }
 49                Spacer()
 50                #if APPSTORE
 51                // The sandbox reaches iCloud Drive only through a folder chosen there.
 52                Button("New Folder in iCloud Drive…") { chooseInICloudDrive() }
 53                    .help("Makes notes.org in a folder you choose or make in iCloud Drive and adds the folder")
 54                #else
 55                if let drive = Self.iCloudDrive {
 56                    Button("New Folder in iCloud Drive") { start(in: drive.appendingPathComponent("org")) }
 57                        .help("Makes org/notes.org in iCloud Drive and adds the folder")
 58                }
 59                #endif
 60                Button("Choose Folder…") {
 61                    dismiss()
 62                    chooseFolder()
 63                }
 64                .keyboardShortcut(.defaultAction)
 65            }
 66        }
 67        .padding(20)
 68        .frame(width: 480)
 69        .onAppear {
 70            if UserDefaults.standard.object(forKey: "keymap") == nil { preset = Self.suggestedPreset }
 71        }
 72    }
 73
 74    /// Doom keys when a Doom configuration is found, Emacs keys for another Emacs
 75    /// configuration, and Mac keys without one.
 76    static var suggestedPreset: KeymapPreset {
 77        guard let found = EmacsImport.defaultLocations().first else { return .mac }
 78        let doom = [ProcessInfo.processInfo.environment["DOOMDIR"], OrgPaths.home + "/.config/doom", OrgPaths.home + "/.doom.d"]
 79        return doom.contains(found.path) ? .doom : .emacs
 80    }
 81
 82    private var presetNote: LocalizedStringKey {
 83        switch preset {
 84        case .emacs: "Org's own bindings: C-c C-t cycles TODO, C-c C-s schedules, M-RET makes a heading."
 85        case .mac: "Standard Mac editing keys, with Org commands on ⌃⌘: ⌃⌘T cycles TODO, ⌃⌘S schedules."
 86        case .doom: "Modal editing as in Doom Emacs, with SPC as the leader: SPC m t cycles TODO."
 87        }
 88    }
 89
 90    #if APPSTORE
 91    /// An open panel in iCloud Drive; the folder chosen or made there becomes the notes folder.
 92    private func chooseInICloudDrive() {
 93        let message = String(localized: "Choose or make the folder for your notes in iCloud Drive.", comment: "Open panel on the welcome sheet")
 94        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 }
 95        start(in: folder)
 96    }
 97    #endif
 98
 99    /// Makes `folder` with a notes file in it, adds it, and opens the file.
100    private func start(in folder: URL) {
101        Task {
102            do {
103                let file = folder.appendingPathComponent("notes.org")
104                try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
105                if !FileManager.default.fileExists(atPath: file.path) {
106                    try "* Inbox\n".write(to: file, atomically: true, encoding: .utf8)
107                }
108                try await workspace.addRoot(folder)
109                open(file.standardizedFileURL.path)
110                dismiss()
111            } catch {
112                self.error = error.localizedDescription
113            }
114        }
115    }
116}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form