Sources/Orgstar/Sidebar.swift
168 lines · 7370 bytes
1import OrgApp
2import OrgCore
3import OrgWorkspace
4import SwiftUI
5import UniformTypeIdentifiers
6
7struct Sidebar: View {
8 let workspace: WorkspaceModel
9 let session: DocumentSession
10 @Binding var selection: String?
11 var addFolder: () -> Void = {}
12 /// Folders shown collapsed, by path, one per line.
13 @AppStorage("collapsedFolders") private var collapsedText = ""
14 @State private var removing: String?
15 /// A file operation waiting for a name or a confirmation.
16 @Environment(\.orgTheme) private var theme
17 @Environment(\.colorScheme) private var colorScheme
18 @State var pending: FileOperation?
19 @State var name = ""
20 @State var failure: String?
21
22 private var collapsed: Set<String> { Set(collapsedText.split(separator: "\n").map(String.init)) }
23
24 private func expanded(_ root: String) -> Binding<Bool> {
25 Binding {
26 !collapsed.contains(root)
27 } set: { isExpanded in
28 var set = collapsed
29 if isExpanded { set.remove(root) } else { set.insert(root) }
30 collapsedText = set.sorted().joined(separator: "\n")
31 }
32 }
33
34 var body: some View {
35 List(selection: $selection) {
36 ForEach(workspace.rootPaths, id: \.self) { root in
37 Section(isExpanded: expanded(root)) {
38 OutlineGroup(workspace.tree(forRoot: root).children ?? [], children: \.children) { node in
39 Label(node.name, systemImage: icon(node))
40 .foregroundStyle(node.file?.kind == .conflict ? .orange : node.isDownloading ? .secondary : theme.color("sidebar-foreground") ?? .primary)
41 .help(node.isDownloading ? "Downloading from iCloud" : "")
42 .tag(node.file?.path)
43 .contextMenu { fileMenu(node) }
44 }
45 } header: {
46 Text((root as NSString).lastPathComponent)
47 .foregroundStyle(theme.color("sidebar-header") ?? .secondary)
48 .help(root)
49 .contextMenu { folderMenu(root) }
50 }
51 }
52 #if APPSTORE
53 if !workspace.unresolvedRoots.isEmpty {
54 Section("Can't Open") {
55 ForEach(workspace.unresolvedRoots, id: \.path) { root in
56 HStack {
57 Label((root.path as NSString).lastPathComponent, systemImage: "exclamationmark.triangle")
58 .foregroundStyle(.secondary)
59 Spacer()
60 Button("Grant Access…") { GrantAccess.regrant(root, workspace: workspace) }
61 .buttonStyle(.borderless)
62 .controlSize(.small)
63 }
64 .help(root.path)
65 .contextMenu {
66 Button("Grant Access…") { GrantAccess.regrant(root, workspace: workspace) }
67 Button("Remove from Sidebar…") { removing = root.path }
68 }
69 }
70 }
71 }
72 #endif
73 }
74 .listStyle(.sidebar)
75 .themedListBackground(theme.color("sidebar-background", in: colorScheme))
76 .safeAreaInset(edge: .bottom, spacing: 0) {
77 HStack(spacing: 4) {
78 Button("Add Folder…", systemImage: "plus", action: addFolder)
79 .help("Add a folder (⇧⌘O)")
80 Menu {
81 ForEach(workspace.rootPaths, id: \.self) { root in
82 Button((root as NSString).lastPathComponent) { removing = root }
83 }
84 } label: {
85 Label("Remove Folder", systemImage: "minus")
86 }
87 .menuIndicator(.hidden)
88 .fixedSize()
89 .disabled(workspace.rootPaths.isEmpty)
90 .help("Remove a folder from the sidebar")
91 Spacer()
92 }
93 .labelStyle(.iconOnly)
94 .buttonStyle(.borderless)
95 .menuStyle(.borderlessButton)
96 .padding(.horizontal, 10)
97 .padding(.vertical, 6)
98 }
99 .confirmationDialog(
100 "Remove “\(removing.map { ($0 as NSString).lastPathComponent } ?? "")” from the sidebar?",
101 isPresented: Binding { removing != nil } set: { if !$0 { removing = nil } }
102 ) {
103 Button("Remove") {
104 if let root = removing { try? workspace.removeRoot(path: root) }
105 removing = nil
106 }
107 } message: {
108 Text("The folder and its files stay on disk.")
109 }
110 .fileOperations(self)
111 }
112
113 @ViewBuilder
114 private func folderMenu(_ root: String) -> some View {
115 Button("New File…") { ask(.create(folder: root)) }
116 Divider()
117 Button(collapsed.contains(root) ? "Expand" : "Collapse") { expanded(root).wrappedValue.toggle() }
118 Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: root)]) }
119 Divider()
120 Button("Remove from Sidebar…") { removing = root }
121 }
122
123 private func icon(_ node: FileTreeNode) -> String {
124 if node.isDownloading { return "icloud.and.arrow.down" }
125 guard let file = node.file else { return "folder" }
126 switch file.kind {
127 case .org: return "doc.text"
128 case .archive: return "archivebox"
129 case .conflict: return "exclamationmark.triangle"
130 case .other:
131 guard let type = UTType(filenameExtension: (file.path as NSString).pathExtension) else { return "doc" }
132 if type.conforms(to: .image) { return "photo" }
133 if type.conforms(to: .pdf) { return "doc.richtext" }
134 if type.conforms(to: .audiovisualContent) { return "play.rectangle" }
135 if type.conforms(to: .sourceCode) || type.conforms(to: .script) { return "chevron.left.forwardslash.chevron.right" }
136 if type.conforms(to: .text) { return "doc.plaintext" }
137 return "doc"
138 }
139 }
140}
141
142struct OutlineColumn: View {
143 let session: DocumentSession
144 @Environment(\.orgTheme) private var theme
145 @Environment(\.colorScheme) private var colorScheme
146
147 var body: some View {
148 List(Array(session.outline.enumerated()), id: \.element.start) { _, entry in
149 Button {
150 session.reveal(entry.start)
151 } label: {
152 Text(entry.title.isEmpty ? "(untitled)" : entry.title)
153 .foregroundStyle(theme.color("sidebar-foreground") ?? .primary)
154 .lineLimit(1)
155 .padding(.leading, CGFloat(entry.level - 1) * 12)
156 }
157 .buttonStyle(.plain)
158 }
159 .themedListBackground(theme.color("sidebar-background", in: colorScheme))
160 .overlay {
161 if session.path != nil, !session.isOrg {
162 ContentUnavailableView("No outline", systemImage: "doc.plaintext", description: Text("Only org files have an outline."))
163 } else if session.path != nil, session.outline.isEmpty {
164 ContentUnavailableView("No headings", systemImage: "list.bullet.indent")
165 }
166 }
167 }
168}