Sources/Orgstar/WelcomeView.swift
102 lines · 4721 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 #if APPSTORE
34 ForEach(workspace.unresolvedRoots, id: \.path) { root in
35 HStack {
36 Label(OrgPaths.abbreviatingWithTilde(root.path), systemImage: "exclamationmark.triangle")
37 .help("orgstar can't open this folder")
38 Spacer()
39 Button("Grant Access…") { GrantAccess.regrant(root, workspace: workspace) }
40 }
41 }
42 #endif
43 if let error { Text(error).foregroundStyle(.red) }
44 HStack {
45 Button("Not Now") { dismiss() }
46 Spacer()
47 #if APPSTORE
48 // The sandbox reaches iCloud Drive only through a folder chosen there.
49 Button("New Folder in iCloud Drive…") { chooseInICloudDrive() }
50 .help("Makes notes.org in a folder you choose or make in iCloud Drive and adds the folder")
51 #else
52 if let drive = Self.iCloudDrive {
53 Button("New Folder in iCloud Drive") { start(in: drive.appendingPathComponent("org")) }
54 .help("Makes org/notes.org in iCloud Drive and adds the folder")
55 }
56 #endif
57 Button("Choose Folder…") {
58 dismiss()
59 chooseFolder()
60 }
61 .keyboardShortcut(.defaultAction)
62 }
63 }
64 .padding(20)
65 .frame(width: 480)
66 }
67
68 private var presetNote: LocalizedStringKey {
69 switch preset {
70 case .emacs: "Org's own bindings: C-c C-t cycles TODO, C-c C-s schedules, M-RET makes a heading."
71 case .mac: "Standard Mac editing keys, with Org commands on ⌃⌘: ⌃⌘T cycles TODO, ⌃⌘S schedules."
72 case .doom: "Modal editing as in Doom Emacs, with SPC as the leader: SPC m t cycles TODO."
73 }
74 }
75
76 #if APPSTORE
77 /// An open panel in iCloud Drive; the folder chosen or made there becomes the notes folder.
78 private func chooseInICloudDrive() {
79 let message = String(localized: "Choose or make the folder for your notes in iCloud Drive.", comment: "Open panel on the welcome sheet")
80 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 }
81 start(in: folder)
82 }
83 #endif
84
85 /// Makes `folder` with a notes file in it, adds it, and opens the file.
86 private func start(in folder: URL) {
87 Task {
88 do {
89 let file = folder.appendingPathComponent("notes.org")
90 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
91 if !FileManager.default.fileExists(atPath: file.path) {
92 try "* Inbox\n".write(to: file, atomically: true, encoding: .utf8)
93 }
94 try await workspace.addRoot(folder)
95 open(file.standardizedFileURL.path)
96 dismiss()
97 } catch {
98 self.error = error.localizedDescription
99 }
100 }
101 }
102}