Sources/Orgstar/SearchViews.swift

eee69add637e6a92d9a22e27f1bfc83d40ef6baf
orgstar/Sources/Orgstar/SearchViews.swift

101 lines · 4145 bytes

  1import AppKit
  2import OrgApp
  3import OrgIndex
  4import OrgWorkspace
  5import SwiftUI
  6
  7struct SearchResults: View {
  8    let results: Result<[HeadingLocation], any Error>
  9    let open: (HeadingLocation) -> Void
 10
 11    var body: some View {
 12        let found = (try? results.get()) ?? []
 13        List(found, id: \.self) { location in
 14            Button {
 15                open(location)
 16            } label: {
 17                VStack(alignment: .leading, spacing: 2) {
 18                    Text(location.title.isEmpty ? String(localized: "(untitled)") : location.title).lineLimit(1)
 19                    if let snippet = location.snippet {
 20                        Text(snippet.attributed).font(.callout).foregroundStyle(.secondary).lineLimit(3)
 21                    }
 22                    Text((location.path as NSString).lastPathComponent).font(.caption).foregroundStyle(.tertiary)
 23                }
 24            }
 25            .buttonStyle(.plain)
 26        }
 27        .overlay {
 28            if case .failure(let error) = results {
 29                ContentUnavailableView(error.localizedDescription, systemImage: "exclamationmark.triangle")
 30            } else if found.isEmpty {
 31                ContentUnavailableView.search
 32            }
 33        }
 34    }
 35}
 36
 37struct QuickOpenView: View {
 38    let workspace: WorkspaceModel
 39    let open: (FileEntry) -> Void
 40    @State private var query = ""
 41    /// The highlighted match, moved by ↑/↓ or C-p/C-n and opened by Return.
 42    @State private var selected = 0
 43    @Environment(\.dismiss) private var dismiss
 44
 45    var body: some View {
 46        let matches = workspace.quickOpen(query)
 47        let current = min(selected, matches.count - 1)
 48        VStack(spacing: 0) {
 49            TextField("Open file", text: $query)
 50                .textFieldStyle(.plain)
 51                .font(.title3)
 52                .padding(12)
 53                .onSubmit { if current >= 0 { open(matches[current]) } }
 54                .movesSelection($selected, current: current, count: matches.count)
 55                .onChange(of: query) { selected = 0 }
 56            Divider()
 57            ScrollViewReader { proxy in
 58                List(matches.indices, id: \.self) { index in
 59                    let entry = matches[index]
 60                    Button {
 61                        open(entry)
 62                    } label: {
 63                        VStack(alignment: .leading, spacing: 2) {
 64                            Text(entry.name)
 65                            Text(entry.relativePath).font(.caption).foregroundStyle(.secondary)
 66                        }
 67                        .frame(maxWidth: .infinity, alignment: .leading)
 68                        .contentShape(Rectangle())
 69                    }
 70                    .buttonStyle(.plain)
 71                    .selectedRow(index == current)
 72                }
 73                .onChange(of: current) { if current >= 0 { proxy.scrollTo(current) } }
 74            }
 75        }
 76        .frame(width: 520, height: 360)
 77        .onExitCommand { dismiss() }
 78    }
 79}
 80
 81extension View {
 82    /// ↑/↓ and C-p/C-n move `selected` through `count` rows; `current` is the row shown as selected.
 83    func movesSelection(_ selected: Binding<Int>, current: Int, count: Int) -> some View {
 84        onKeyPress(phases: .down) { press in
 85            let control = press.modifiers == .control
 86            // Shift with an arrow is the date prompt's.
 87            let plain = press.modifiers.intersection([.shift, .control, .option, .command]).isEmpty
 88            let step = if press.key == .downArrow && plain || control && press.characters == "n" { 1 }
 89                else if press.key == .upArrow && plain || control && press.characters == "p" { -1 } else { 0 }
 90            guard step != 0, count > 0 else { return .ignored }
 91            selected.wrappedValue = max(0, min(current + step, count - 1))
 92            return .handled
 93        }
 94    }
 95
 96    /// The highlight of the row arrow keys select in a list.
 97    func selectedRow(_ selected: Bool) -> some View {
 98        listRowBackground(selected ? Color.accentColor.opacity(0.25) : Color.clear)
 99            .accessibilityAddTraits(selected ? .isSelected : [])
100    }
101}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form