Jotley / app / macOS / Views / NotesListView+Layout.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | extension NotesListView { |
| 4 | var navigationView: some View { |
| 5 | NavigationSplitView(columnVisibility: $sidebarVisibility) { |
| 6 | mainContent |
| 7 | .frame(minWidth: 280) |
| 8 | .toolbar(removing: .sidebarToggle) |
| 9 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 10 | .navigationSplitViewColumnWidth(min: 280, ideal: 320, max: 400) |
| 11 | .overlay(alignment: .trailing) { |
| 12 | Rectangle() |
| 13 | .fill(Color.themeTextSecondary.opacity(0.3)) |
| 14 | .frame(width: 1) |
| 15 | .ignoresSafeArea() |
| 16 | .allowsHitTesting(false) |
| 17 | .accessibilityHidden(true) |
| 18 | } |
| 19 | } detail: { |
| 20 | detailContent |
| 21 | .toolbar(removing: .sidebarToggle) |
| 22 | .overlay(alignment: .bottom) { |
| 23 | // A selected note owns the shared idle/live footer. Keep |
| 24 | // recording controls available if filters hide every note. |
| 25 | if viewModel.recorder.isRecording && !notes.contains(where: { viewModel.selectedNoteIDs.contains($0.objectID) }) { |
| 26 | MacCompactTranscriptionBar(viewModel: viewModel) |
| 27 | .modifier(MacNoteFooterPlacement()) |
| 28 | } |
| 29 | } |
| 30 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 31 | .frame(maxWidth: .infinity) |
| 32 | } |
| 33 | .navigationSplitViewStyle(.automatic) |
| 34 | .toolbar { |
| 35 | // The window toolbar stays visible during setup for the traffic lights, so drop the toggle there. |
| 36 | if !onboardingActive { |
| 37 | ToolbarItem(placement: .navigation) { |
| 38 | let title = sidebarVisibility == .detailOnly ? "Show Sidebar" : "Hide Sidebar" |
| 39 | Button { |
| 40 | sidebarVisibility = sidebarVisibility == .detailOnly ? .all : .detailOnly |
| 41 | } label: { |
| 42 | Image(systemName: "sidebar.left").font(.system(size: 15, weight: .medium)) |
| 43 | } |
| 44 | .buttonStyle(JotleyGlassButtonStyle(circular: true, diameter: 30, floatsOverContent: true)) |
| 45 | .padding(4) |
| 46 | .accessibilityLabel(title) |
| 47 | .accessibilityIdentifier("notesSidebarToggle") |
| 48 | .help(title) |
| 49 | } |
| 50 | .sharedBackgroundVisibility(.hidden) |
| 51 | } |
| 52 | } |
| 53 | .background(Color.themeContentBackground) |
| 54 | } |
| 55 | |
| 56 | @ViewBuilder |
| 57 | private var mainContent: some View { |
| 58 | ZStack(alignment: .topLeading) { |
| 59 | VStack(spacing: 0) { |
| 60 | // Search field at the top of the sidebar |
| 61 | HStack(spacing: 8) { |
| 62 | ThemedTextField( |
| 63 | text: $searchText, |
| 64 | placeholder: String(localized: "Search notes"), |
| 65 | font: NSFont.systemFont(ofSize: 14), |
| 66 | textColor: NSColor(Color.themeTextPrimary), |
| 67 | minimumHeight: MacNoteHeaderLayout.controlHeight, |
| 68 | allowsEditing: !MacScreenshotFixture.isActive |
| 69 | ) |
| 70 | .frame(height: MacNoteHeaderLayout.controlHeight) |
| 71 | .accessibilityIdentifier("notesListSearchField") |
| 72 | .padding(.horizontal, 8) |
| 73 | .padding(.leading, 24) |
| 74 | .padding(.trailing, 24) |
| 75 | .background(Color.themeAccent.opacity(0.5)) |
| 76 | .cornerRadius(8) |
| 77 | .foregroundColor(Color.themeTextPrimary) |
| 78 | .overlay(alignment: .leading) { |
| 79 | Image(systemName: "magnifyingglass") |
| 80 | .font(.system(size: 16)) |
| 81 | .foregroundColor(Color.themeTextSecondary) |
| 82 | .padding(.leading, 10) |
| 83 | } |
| 84 | .overlay(alignment: .trailing) { |
| 85 | if !searchText.isEmpty { |
| 86 | Button(action: { searchText = "" }) { |
| 87 | Image(systemName: "xmark.circle.fill") |
| 88 | .font(.system(size: 11)) |
| 89 | .foregroundColor(Color.themeTextSecondary) |
| 90 | } |
| 91 | .buttonStyle(.plain) |
| 92 | .padding(.trailing, 6) |
| 93 | .accessibilityIdentifier("clearSearchButton") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Filter & sort button and anchored dropdown |
| 98 | Button(action: { |
| 99 | showingTagFilter.toggle() |
| 100 | }) { |
| 101 | Image(systemName: filterState.hasTagFilter || filterState.isActive ? "line.3.horizontal.decrease.circle.fill" : "line.3.horizontal.decrease.circle") |
| 102 | .font(.system(size: 18)) |
| 103 | } |
| 104 | .buttonStyle(JotleyGlassButtonStyle(circular: true, floatsOverContent: true)) |
| 105 | .accessibilityElement(children: .ignore) |
| 106 | .accessibilityLabel("Filter & sort") |
| 107 | .accessibilityAddTraits(.isButton) |
| 108 | .help("Filter & sort") |
| 109 | .accessibilityIdentifier("notesListTagFilterButton") |
| 110 | } |
| 111 | .padding(.leading, 16) |
| 112 | // Match the native sidebar toggle's trailing inset. |
| 113 | .padding(.trailing, 8) |
| 114 | .padding(.top, MacNoteHeaderLayout.topInset) |
| 115 | .padding(.bottom, 12) |
| 116 | |
| 117 | folderSidebarSection() |
| 118 | .padding(.horizontal, 16) |
| 119 | .padding(.bottom, 10) |
| 120 | |
| 121 | if let recordingNote = activeRecordingNote { |
| 122 | activeRecordingBanner(for: recordingNote) |
| 123 | .padding(.horizontal, 16) |
| 124 | .padding(.bottom, 10) |
| 125 | } |
| 126 | |
| 127 | switch currentEmptyState { |
| 128 | case .noSearchResults: |
| 129 | ContentUnavailableView { |
| 130 | Label { |
| 131 | Text("No Results for \"\(searchText)\"") |
| 132 | .foregroundColor(Color.themeTextPrimary) |
| 133 | } icon: { |
| 134 | Image(systemName: "magnifyingglass") |
| 135 | .foregroundColor(Color.themePrimaryAccent) |
| 136 | } |
| 137 | } description: { |
| 138 | Text("Try searching with different keywords") |
| 139 | .foregroundColor(Color.themeTextSecondary) |
| 140 | } |
| 141 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 142 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 143 | .accessibilityIdentifier("notesListNoSearchResults") |
| 144 | case .noNotesYet: |
| 145 | ContentUnavailableView { |
| 146 | Label { |
| 147 | Text("No notes yet") |
| 148 | .foregroundColor(Color.themeTextPrimary) |
| 149 | } icon: { |
| 150 | Image(systemName: "note.text") |
| 151 | .foregroundColor(Color.themePrimaryAccent) |
| 152 | } |
| 153 | } description: { |
| 154 | Text("New transcripts and summaries will appear here.") |
| 155 | .foregroundColor(Color.themeTextSecondary) |
| 156 | } |
| 157 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 158 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 159 | .accessibilityIdentifier("notesListNoNotesYet") |
| 160 | case .noNotesInFolder: |
| 161 | VStack(spacing: 10) { |
| 162 | Image(systemName: "folder") |
| 163 | .font(.system(size: 22)) |
| 164 | .foregroundColor(Color.themePrimaryAccent) |
| 165 | Text("No notes in this folder") |
| 166 | .font(.system(size: 13, weight: .semibold)) |
| 167 | .foregroundColor(Color.themeTextPrimary) |
| 168 | Text("Create a note here, or pick another folder from the sidebar.") |
| 169 | .font(.system(size: 12)) |
| 170 | .foregroundColor(Color.themeTextSecondary) |
| 171 | .multilineTextAlignment(.center) |
| 172 | } |
| 173 | .padding(.horizontal, 24) |
| 174 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 175 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 176 | .accessibilityIdentifier("notesListNoNotesInFolder") |
| 177 | case .filtersHidingNotes: |
| 178 | VStack(spacing: 10) { |
| 179 | Image(systemName: "line.3.horizontal.decrease.circle") |
| 180 | .font(.system(size: 22)) |
| 181 | .foregroundColor(Color.themePrimaryAccent) |
| 182 | Text("Filters are hiding notes") |
| 183 | .font(.system(size: 13, weight: .semibold)) |
| 184 | .foregroundColor(Color.themeTextPrimary) |
| 185 | Text("Your current filters match no notes in this view.") |
| 186 | .font(.system(size: 12)) |
| 187 | .foregroundColor(Color.themeTextSecondary) |
| 188 | .multilineTextAlignment(.center) |
| 189 | Button { |
| 190 | filterState.clearAll() |
| 191 | } label: { |
| 192 | Text("Clear Filters").font(.system(size: 12, weight: .semibold)) |
| 193 | } |
| 194 | .buttonStyle(JotleyGlassButtonStyle()) |
| 195 | .accessibilityIdentifier("notesListClearFiltersButton") |
| 196 | .padding(.top, 4) |
| 197 | } |
| 198 | .padding(.horizontal, 24) |
| 199 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 200 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 201 | .accessibilityIdentifier("notesListFiltersHidingNotes") |
| 202 | case .none: |
| 203 | noteList() |
| 204 | } |
| 205 | if currentEmptyState != .none { |
| 206 | NotesTrashLink(showingTrash: $showingTrash) |
| 207 | .padding(.horizontal, 16) |
| 208 | .padding(.bottom, 72) |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | .overlay(alignment: .bottom) { |
| 215 | HStack(alignment: .center) { |
| 216 | Button { showingSettingsSheet = true } label: { |
| 217 | Image(systemName: "gearshape") |
| 218 | .font(.system(size: 18, weight: .medium)) |
| 219 | .frame(width: 36, height: 36) |
| 220 | } |
| 221 | .buttonStyle(JotleyGlassButtonStyle(circular: true, floatsOverContent: true)) |
| 222 | .help("Settings") |
| 223 | .accessibilityLabel("Settings") |
| 224 | .accessibilityIdentifier("notesListSettingsButton") |
| 225 | Spacer() |
| 226 | Button { Task { await viewModel.startNewNote() } } label: { |
| 227 | Image(systemName: "plus") |
| 228 | .font(.system(size: 18, weight: .semibold)) |
| 229 | } |
| 230 | .buttonStyle(JotleyGlassButtonStyle(circular: true, floatsOverContent: true)) |
| 231 | .disabled(!canCreateNewNote) |
| 232 | .help("New Transcription") |
| 233 | .accessibilityLabel("New Transcription") |
| 234 | .accessibilityIdentifier("notesListNewNoteButton") |
| 235 | } |
| 236 | .padding([.horizontal, .bottom], MacNoteFooterPlacement.edgeInset) |
| 237 | } |
| 238 | .overlay { |
| 239 | ZStack(alignment: .top) { |
| 240 | if showingTagFilter { |
| 241 | Color.black.opacity(0.25) |
| 242 | .onTapGesture { |
| 243 | showingTagFilter = false |
| 244 | } |
| 245 | .transition(.opacity) |
| 246 | |
| 247 | FilterAndSortPopoverView( |
| 248 | state: filterState, |
| 249 | allTags: getAllUniqueTags(), |
| 250 | tagCounts: getTagCountsForVisibleResults(), |
| 251 | onDone: { showingTagFilter = false } |
| 252 | ) |
| 253 | .shadow(color: Color.black.opacity(0.25), radius: 16, x: 0, y: 8) |
| 254 | .padding(.top, MacNoteHeaderLayout.menuTopInset) |
| 255 | .padding(.horizontal, 12) |
| 256 | .transition(.opacity) |
| 257 | } |
| 258 | } |
| 259 | .animation(.easeInOut(duration: 0.18), value: showingTagFilter) |
| 260 | .allowsHitTesting(showingTagFilter) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | @ViewBuilder |
| 265 | private func noteList() -> some View { |
| 266 | // Snapshot the (expensive, recomputed) grouped list ONCE per body pass, |
| 267 | // and pre-compute cumulative row offsets per group. Without these locals |
| 268 | // the inner ForEach was rebuilding the Dictionary grouping and running |
| 269 | // an O(n) prefix/map/reduce per row, making list rendering O(n²) and |
| 270 | // tripping App Hanging alerts on lists with hundreds of notes. |
| 271 | let groups = groupedNotes |
| 272 | let enumeratedGroups = Array(groups.enumerated()) |
| 273 | let groupRowOffsets: [Int] = { |
| 274 | var offsets: [Int] = [] |
| 275 | offsets.reserveCapacity(groups.count) |
| 276 | var running = 0 |
| 277 | for g in groups { |
| 278 | offsets.append(running) |
| 279 | running += g.1.count |
| 280 | } |
| 281 | return offsets |
| 282 | }() |
| 283 | |
| 284 | ScrollView { |
| 285 | LazyVStack(alignment: .leading, spacing: 4, pinnedViews: [.sectionHeaders]) { |
| 286 | ForEach(enumeratedGroups, id: \.element.0) { group in |
| 287 | let groupIndex = group.offset |
| 288 | let date = group.element.0 |
| 289 | let notesInGroup = group.element.1 |
| 290 | let enumeratedNotes = Array(notesInGroup.enumerated()) |
| 291 | |
| 292 | Section { |
| 293 | ForEach(enumeratedNotes, id: \.element.objectID) { noteItem in |
| 294 | let noteIndex = noteItem.offset |
| 295 | let currentNote = noteItem.element |
| 296 | |
| 297 | // Break into simpler expressions to fix compiler type-checking |
| 298 | let isRowSelected = viewModel.selectedNoteIDs.contains(currentNote.objectID) |
| 299 | let globalRowIndex = groupRowOffsets[groupIndex] + noteIndex |
| 300 | let isRecordingNote = viewModel.activeRecordingNote?.objectID == currentNote.objectID |
| 301 | |
| 302 | NoteRowView( |
| 303 | note: currentNote, |
| 304 | isSelected: isRowSelected, |
| 305 | toggleSelection: { note, isCommandPressed in |
| 306 | viewModel.toggleSelection(for: note, isCommandKeyPressed: isCommandPressed) |
| 307 | }, |
| 308 | prepareDelete: viewModel.performContextualDelete, |
| 309 | rowIndex: globalRowIndex, |
| 310 | isRecordingNote: isRecordingNote |
| 311 | ) |
| 312 | // Add swipe to delete on Mac |
| 313 | .swipeActions(edge: .trailing) { |
| 314 | Button(role: .destructive) { |
| 315 | Task { |
| 316 | await viewModel.deleteNotes(inGroup: notesInGroup, atOffsets: IndexSet([noteIndex])) |
| 317 | } |
| 318 | } |
| 319 | label: { |
| 320 | Label("Move to Trash", systemImage: "trash") |
| 321 | } |
| 322 | .tint(.themePrimaryAccent) |
| 323 | } |
| 324 | } |
| 325 | } header: { |
| 326 | if let headerText = formatDateSectionHeader(for: date) { |
| 327 | Text(headerText) |
| 328 | .font(.headline) |
| 329 | .fontWeight(.bold) |
| 330 | .padding(.vertical, 8) |
| 331 | .padding(.horizontal, 16) |
| 332 | .frame(maxWidth: .infinity, alignment: .leading) |
| 333 | .background(Color.themeContentBackground) |
| 334 | .foregroundColor(.secondary) |
| 335 | } else { |
| 336 | EmptyView() |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | NotesTrashLink(showingTrash: $showingTrash) |
| 342 | .padding(.horizontal, 16) |
| 343 | .padding(.top, 8) |
| 344 | |
| 345 | // Keep the Trash row clear of the floating controls. |
| 346 | Color.themeContentBackground |
| 347 | .frame(maxWidth: .infinity, minHeight: 80) |
| 348 | } |
| 349 | } |
| 350 | .frame(maxWidth: .infinity) |
| 351 | .background(Color.themeContentBackground) |
| 352 | } |
| 353 | |
| 354 | private func formatDateSectionHeader(for date: Date) -> String? { |
| 355 | // Sentinel used by non-date sort modes to collapse everything into a single unlabeled group. |
| 356 | if date == .distantFuture { return nil } |
| 357 | let calendar = Calendar.current |
| 358 | if calendar.isDateInToday(date) { |
| 359 | return String(localized: "Today") |
| 360 | } else if calendar.isDateInYesterday(date) { |
| 361 | return String(localized: "Yesterday") |
| 362 | } else { |
| 363 | let dateFormatter = DateFormatter() |
| 364 | dateFormatter.dateFormat = "E, MMM d" // "Tue, Jan 23" |
| 365 | return dateFormatter.string(from: date) |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // MARK: - Folder Sidebar Section |
| 370 | |
| 371 | @ViewBuilder |
| 372 | func folderSidebarSection() -> some View { |
| 373 | FolderSidebarView( |
| 374 | filterState: filterState, |
| 375 | isExpanded: $foldersExpanded, |
| 376 | showingFolderManagement: $showingFolderManagement |
| 377 | ) |
| 378 | .sheet(isPresented: $showingFolderManagement) { |
| 379 | FolderManagementSheet() |
| 380 | .environment(\.managedObjectContext, viewContext) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | @ViewBuilder |
| 385 | private func activeRecordingBanner(for note: Note) -> some View { |
| 386 | let isSelected = viewModel.selectedNoteIDs.contains(note.objectID) |
| 387 | |
| 388 | Button { |
| 389 | viewModel.jumpToActiveRecordingNote() |
| 390 | } label: { |
| 391 | HStack(spacing: 10) { |
| 392 | ActiveRecordingIcon() |
| 393 | |
| 394 | VStack(alignment: .leading, spacing: 2) { |
| 395 | Text("Transcription in progress") |
| 396 | .font(.system(size: 12, weight: .semibold)) |
| 397 | .foregroundColor(Color.themeTextPrimary) |
| 398 | Text(isSelected ? note.titleOrUntitled : "Click to jump back") |
| 399 | .font(.system(size: 11)) |
| 400 | .foregroundColor(Color.themeTextSecondary) |
| 401 | .lineLimit(1) |
| 402 | } |
| 403 | |
| 404 | Spacer(minLength: 0) |
| 405 | |
| 406 | if !isSelected { |
| 407 | Image(systemName: "arrow.turn.up.left") |
| 408 | .font(.system(size: 11, weight: .semibold)) |
| 409 | .foregroundColor(Color.themePrimaryAccent) |
| 410 | } |
| 411 | } |
| 412 | .padding(.horizontal, 12) |
| 413 | .padding(.vertical, 9) |
| 414 | .background(.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 10)) |
| 415 | .overlay { |
| 416 | RoundedRectangle(cornerRadius: 10) |
| 417 | .strokeBorder(.primary.opacity(0.16)) |
| 418 | .allowsHitTesting(false) |
| 419 | } |
| 420 | } |
| 421 | .buttonStyle(.plain) |
| 422 | .help(isSelected ? "Transcription is active on this note" : "Jump back to active transcription") |
| 423 | .accessibilityIdentifier("activeRecordingJumpBanner") |
| 424 | } |
| 425 | |
| 426 | private struct ActiveRecordingIcon: View { |
| 427 | @ObservedObject private var themeManager = ThemeManager.shared |
| 428 | @State private var isPulsing = false |
| 429 | @State private var themeUpdateId = UUID() |
| 430 | |
| 431 | var body: some View { |
| 432 | ZStack { |
| 433 | Circle() |
| 434 | .fill(Color.primary.opacity(0.12)) |
| 435 | .frame(width: 24, height: 24) |
| 436 | .overlay( |
| 437 | Circle() |
| 438 | .stroke(Color.white.opacity(0.2), lineWidth: 1) |
| 439 | ) |
| 440 | |
| 441 | Circle() |
| 442 | .stroke(Color.white.opacity(0.9), lineWidth: 1.6) |
| 443 | .frame(width: 24, height: 24) |
| 444 | .scaleEffect(isPulsing ? 1.7 : 1.0) |
| 445 | .opacity(isPulsing ? 0.0 : 0.95) |
| 446 | |
| 447 | Circle() |
| 448 | .stroke(Color.themeTextSecondary.opacity(0.75), lineWidth: 2) |
| 449 | .frame(width: 24, height: 24) |
| 450 | .scaleEffect(isPulsing ? 1.95 : 1.05) |
| 451 | .opacity(isPulsing ? 0.0 : 0.55) |
| 452 | |
| 453 | Image(systemName: "mic.fill") |
| 454 | .symbolRenderingMode(.monochrome) |
| 455 | .font(.system(size: 11, weight: .bold)) |
| 456 | .foregroundStyle(Color.themePrimaryAccent) |
| 457 | .accessibilityIdentifier("activeRecordingBannerMicrophone") |
| 458 | } |
| 459 | .id(themeUpdateId) |
| 460 | .modifier(JotleyControlShadow()) |
| 461 | .onAppear { |
| 462 | isPulsing = false |
| 463 | withAnimation(.easeOut(duration: 1.1).repeatForever(autoreverses: false)) { |
| 464 | isPulsing = true |
| 465 | } |
| 466 | } |
| 467 | .onChange(of: themeManager.currentThemeColor) { _, _ in |
| 468 | // Recreate the animated icon so the pulsing layers immediately adopt the new accent. |
| 469 | themeUpdateId = UUID() |
| 470 | } |
| 471 | .onDisappear { |
| 472 | isPulsing = false |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | @ViewBuilder |
| 478 | private var detailContent: some View { |
| 479 | if let selectedID = viewModel.selectedNoteIDs.first, |
| 480 | let selectedNote = notes.first(where: { $0.objectID == selectedID }) { |
| 481 | NoteDetailPlaceholderView(note: selectedNote, selectedTab: $viewModel.selectedDetailTab, viewModel: viewModel) |
| 482 | .id(selectedNote.objectID) |
| 483 | .onAppear { |
| 484 | } |
| 485 | .onDisappear { |
| 486 | } |
| 487 | .frame(minWidth: 340) |
| 488 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 489 | .accessibilityIdentifier("noteDetailContainer") |
| 490 | } else { |
| 491 | // Show welcome view for both empty and no selection states |
| 492 | WelcomeView( |
| 493 | hasNotes: !notes.isEmpty, |
| 494 | canCreateNewNote: canCreateNewNote, |
| 495 | viewModel: viewModel |
| 496 | ) |
| 497 | .onAppear { |
| 498 | } |
| 499 | .frame(minWidth: 340) |
| 500 | .background(Color.themeContentBackground.ignoresSafeArea()) |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // MARK: - Helper Functions |
| 505 | } |