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