Branchwell

Jotley / app / macOS / Views / FolderSidebarView.swift

1import SwiftUI
2import CoreData
3
4/// Collapsible folder list that lives at the top of the notes sidebar.
5/// Selecting a folder scopes the notes list via `FilterAndSortState`.
6struct FolderSidebarView: View {
7 @Environment(\.colorScheme) private var colorScheme
8 @ObservedObject private var theme = ThemeManager.shared
9 @ObservedObject var filterState: FilterAndSortState
10 @Binding var isExpanded: Bool
11 @Binding var showingFolderManagement: Bool
12 private let rowHeight: CGFloat = 28
13 private let rowSpacing: CGFloat = 4
14 private var folderTextColor: Color {
15 colorScheme == .dark ? .white : .black
16 }
17 private var folderIconColor: Color { theme.currentThemeColor.color(in: colorScheme) }
18
19 @FetchRequest(fetchRequest: {
20 // Resolve the entity in the environment's context, including isolated stores.
21 let request = NSFetchRequest<Folder>(entityName: "Folder")
22 request.sortDescriptors = [
23 NSSortDescriptor(keyPath: \Folder.sortOrder, ascending: true),
24 NSSortDescriptor(keyPath: \Folder.name, ascending: true)
25 ]
26 return request
27 }(), animation: .default)
28 private var folders: FetchedResults<Folder>
29
30 var body: some View {
31 VStack(alignment: .leading, spacing: 6) {
32 header
33
34 if isExpanded {
35 VStack(alignment: .leading, spacing: rowSpacing) {
36 row(
37 title: String(localized: "All Notes"),
38 systemImage: "tray.full",
39 isSelected: filterState.selectedFolder == .all,
40 action: { filterState.selectedFolder = .all }
41 )
42 row(
43 title: String(localized: "Unfiled"),
44 systemImage: "tray",
45 isSelected: filterState.selectedFolder == .unfiled,
46 action: { filterState.selectedFolder = .unfiled }
47 )
48
49 if !folders.isEmpty {
50 Divider().padding(.vertical, 2)
51 ScrollView(.vertical, showsIndicators: true) {
52 VStack(alignment: .leading, spacing: rowSpacing) {
53 ForEach(folders) { folder in
54 folderRow(folder)
55 }
56 }
57 }
58 .frame(maxHeight: foldersMaxHeight)
59 }
60
61 Button {
62 showingFolderManagement = true
63 } label: {
64 HStack(spacing: 6) {
65 Image(systemName: "folder.badge.plus")
66 .font(.system(size: 11))
67 .foregroundStyle(folderIconColor)
68 Text("Manage Folders")
69 .font(.system(size: 12))
70 Spacer()
71 }
72 .foregroundColor(Color.themeTextSecondary)
73 .padding(.horizontal, 8)
74 .frame(minHeight: rowHeight)
75 }
76 .buttonStyle(.plain)
77 .help("Create, rename, or delete folders")
78 .accessibilityIdentifier("notesListManageFoldersButton")
79 }
80 .padding(.leading, 4)
81 }
82 }
83 }
84
85 private var foldersMaxHeight: CGFloat {
86 let visibleRows = min(folders.count, 6)
87 return CGFloat(visibleRows) * rowHeight + CGFloat(max(0, visibleRows - 1)) * rowSpacing
88 }
89
90 // MARK: - Rows
91
92 private var header: some View {
93 Button {
94 withAnimation(.easeInOut(duration: 0.15)) {
95 isExpanded.toggle()
96 }
97 } label: {
98 HStack(spacing: 8) {
99 Image(systemName: isExpanded ? "chevron.down" : "chevron.right")
100 .font(.system(size: 9, weight: .semibold))
101 .foregroundColor(Color.themeTextSecondary)
102 Image(systemName: isExpanded ? "folder.fill" : "folder")
103 .font(.system(size: 13, weight: .medium))
104 .foregroundStyle(folderIconColor)
105 .accessibilityHidden(true)
106 Text("Folders")
107 .font(.system(size: 11, weight: .bold))
108 .foregroundStyle(folderTextColor)
109 .textCase(nil)
110 Spacer()
111 }
112 .padding(.vertical, 6)
113 .contentShape(Rectangle())
114 }
115 .buttonStyle(.plain)
116 }
117
118 private func row(title: String, systemImage: String, isSelected: Bool, action: @escaping () -> Void) -> some View {
119 Button(action: action) {
120 HStack(spacing: 8) {
121 Image(systemName: systemImage)
122 .font(.system(size: 11))
123 .foregroundColor(isSelected ? Color.themePrimaryAccent : Color.themeTextSecondary)
124 .frame(width: 14)
125 Text(title)
126 .font(.system(size: 12, weight: isSelected ? .semibold : .regular))
127 .foregroundColor(isSelected ? Color.themeTextPrimary : Color.themeTextPrimary.opacity(0.85))
128 Spacer()
129 }
130 .padding(.horizontal, 8)
131 .frame(minHeight: rowHeight)
132 .background(
133 RoundedRectangle(cornerRadius: 5)
134 .fill(isSelected ? Color.themeSelectionBackground : Color.clear)
135 )
136 .contentShape(Rectangle())
137 }
138 .buttonStyle(.plain)
139 }
140
141 private func folderRow(_ folder: Folder) -> some View {
142 let isSelected: Bool = {
143 if case .folder(let id) = filterState.selectedFolder {
144 return id == folder.id
145 }
146 return false
147 }()
148
149 return Button {
150 if let id = folder.id {
151 filterState.selectedFolder = .folder(id)
152 }
153 } label: {
154 HStack(spacing: 8) {
155 Image(systemName: isSelected ? "folder.fill" : "folder")
156 .font(.system(size: 11))
157 .foregroundStyle(folderIconColor)
158 .frame(width: 14)
159 Text(folder.displayName)
160 .font(.system(size: 12, weight: isSelected ? .semibold : .regular))
161 .foregroundStyle(folderTextColor)
162 .lineLimit(1)
163 Spacer()
164 Text("\(folder.noteCount)")
165 .font(.system(size: 10, weight: .semibold, design: .rounded))
166 .monospacedDigit()
167 .foregroundColor(Color.themeTextSecondary)
168 }
169 .padding(.horizontal, 8)
170 .frame(minHeight: rowHeight)
171 .background(
172 RoundedRectangle(cornerRadius: 5)
173 .fill(isSelected ? Color.themeSelectionBackground : Color.clear)
174 )
175 .contentShape(Rectangle())
176 }
177 .buttonStyle(.plain)
178 .accessibilityIdentifier("folderSidebarRow-\(folder.displayName)")
179 }
180}