Publish VCC Plus OSS source distribution for 3.6.2 build 60
This commit is contained in:
commit
c652a5276d
1192 files changed
+272368
No files matched your search
@@ -0,0 +1,294 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import SwiftUI
|
||||
|
||||
private struct ArchiveCollection: Identifiable {
|
||||
let id: String
|
||||
let name: String
|
||||
let children: [Self]?
|
||||
|
||||
static func tree(_ paths: [String]) -> [Self] {
|
||||
var descendants: [String: Set<String>] = [:]
|
||||
for path in paths {
|
||||
var parent = ""
|
||||
for component in path.split(separator: "/") {
|
||||
let child = parent.isEmpty ? String(component) : parent + "/" + component
|
||||
descendants[parent, default: []].insert(child); parent = child
|
||||
}
|
||||
}
|
||||
func items(_ parent: String) -> [Self] {
|
||||
(descendants[parent] ?? []).sorted { $0.localizedStandardCompare($1) == .orderedAscending }.map { path in
|
||||
Self(id: path, name: String(path.split(separator: "/").last ?? ""), children: descendants[path] == nil ? nil : items(path))
|
||||
}
|
||||
}
|
||||
return items("")
|
||||
}
|
||||
}
|
||||
|
||||
private struct ArchiveBrowseRequest: Equatable { let collection: String; let offset: Int; let retry: Int }
|
||||
private struct ArchiveSearchRequest: Equatable { let query: String; let collection: String?; let offset: Int; let retry: Int }
|
||||
private struct ArchiveResultGroup: Identifiable {
|
||||
let document: ArchiveDocument
|
||||
var hits: [ArchiveHit]
|
||||
var id: String { document.id }
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class ArchiveLibraryQuery: ObservableObject {
|
||||
@Published var text: String
|
||||
init(_ text: String = "") { self.text = text }
|
||||
}
|
||||
|
||||
struct ArchiveLibraryView: View {
|
||||
@AppStorage("archive-library.collection") private var collection = ""
|
||||
@AppStorage("archive-library.browse-selection") private var browseID: String?
|
||||
@AppStorage("archive-library.browse-offset") private var browseOffset = 0
|
||||
@AppStorage("archive-library.scope") private var localScope = false
|
||||
@State private var collections: [ArchiveCollection] = []
|
||||
@State private var documents: [ArchiveDocument] = []
|
||||
@State private var hits: [ArchiveHit] = []
|
||||
@State private var searchID: String?
|
||||
@State private var searchOffset = 0
|
||||
@StateObject private var archiveQuery: ArchiveLibraryQuery
|
||||
@State private var browseMore = false
|
||||
@State private var searchMore = false
|
||||
@State private var browseBusy = true
|
||||
@State private var searchBusy = false
|
||||
@State private var browseError: String?
|
||||
@State private var searchError: String?
|
||||
@State private var retry = 0
|
||||
@State private var compactReader = false
|
||||
@State private var showCollections = false
|
||||
@State private var searchBrowseCollection: String?
|
||||
@State private var searchFolder: String?
|
||||
@FocusState private var queryFocused: Bool
|
||||
@StateObject private var browseReader: ArchiveReaderModel
|
||||
@StateObject private var searchReader: ArchiveReaderModel
|
||||
private let batch = 35
|
||||
private let catalog: ArchiveLibraryCatalog
|
||||
|
||||
init(catalog: ArchiveLibraryCatalog = .shared, query: ArchiveLibraryQuery? = nil, searchReader: ArchiveReaderModel? = nil) {
|
||||
self.catalog = catalog
|
||||
_browseReader = StateObject(wrappedValue: ArchiveReaderModel(remembersPage: true, catalog: catalog))
|
||||
_archiveQuery = StateObject(wrappedValue: query ?? ArchiveLibraryQuery())
|
||||
_searchReader = StateObject(wrappedValue: searchReader ?? ArchiveReaderModel(remembersPage: false, catalog: catalog))
|
||||
}
|
||||
|
||||
private var query: String { archiveQuery.text }
|
||||
private var searching: Bool { !query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
|
||||
private var browseDocument: ArchiveDocument? { documents.first { $0.id == browseID } }
|
||||
private var hit: ArchiveHit? { hits.first { $0.id == searchID } }
|
||||
private var selected: ArchiveDocument? { searching ? hit?.document : browseDocument }
|
||||
private var reader: ArchiveReaderModel { searching ? searchReader : browseReader }
|
||||
private var requestedPage: Int { searching ? (hit?.locationNumber ?? 1) : browseID.map(ArchiveReaderModel.savedPage) ?? 1 }
|
||||
private var sidebarPath: String { searching ? (searchFolder ?? collection) : collection }
|
||||
private var scopePath: String? {
|
||||
if searching, let searchFolder, !searchFolder.isEmpty { return searchFolder }
|
||||
return !collection.isEmpty ? collection : (searchBrowseCollection ?? browseDocument?.collection)
|
||||
}
|
||||
private var key: ArchiveSearchRequest { .init(query: query, collection: localScope ? scopePath : nil, offset: searchOffset, retry: retry) }
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
let narrow = geometry.size.width < 790
|
||||
let side = geometry.size.width >= 990
|
||||
VStack(spacing: 0) {
|
||||
header(narrow: narrow, sidebar: side)
|
||||
Divider()
|
||||
HSplitView {
|
||||
if side { sidebar.frame(minWidth: 150, idealWidth: 180, maxWidth: 220) }
|
||||
if !narrow || !compactReader { listing.frame(minWidth: narrow ? 180 : 220, idealWidth: 290, maxWidth: narrow ? .infinity : 365) }
|
||||
if !narrow || compactReader {
|
||||
// An empty reader must accept the full pane height, just like the PDF reader.
|
||||
detail.frame(minWidth: narrow ? 230 : 330, maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
}.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
.frame(minWidth: 430, minHeight: 320)
|
||||
.task(id: retry) {
|
||||
do { let paths = try await catalog.collections(); try Task.checkCancellation(); collections = ArchiveCollection.tree(paths) }
|
||||
catch { if !Task.isCancelled { browseError = error.localizedDescription } }
|
||||
}
|
||||
.task(id: ArchiveBrowseRequest(collection: collection, offset: browseOffset, retry: retry)) { await loadBrowse() }
|
||||
.task(id: key) { await loadSearch(key) }
|
||||
.onChange(of: query) { old, new in
|
||||
if old.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && !new.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
searchBrowseCollection = browseDocument?.collection
|
||||
if browseReader.progress != nil { browseReader.stop() }
|
||||
}
|
||||
searchID = nil; searchOffset = 0; compactReader = false; searchReader.reset()
|
||||
if !searching { searchBrowseCollection = nil; searchFolder = nil }
|
||||
}
|
||||
.onChange(of: localScope) { _, _ in searchOffset = 0; searchID = nil; compactReader = false; searchReader.reset() }
|
||||
.onChange(of: browseID) { _, id in
|
||||
if id == nil { browseReader.reset() }
|
||||
else if !searching { compactReader = true }
|
||||
}
|
||||
.onChange(of: searchID) { _, id in
|
||||
if id == nil { searchReader.reset() }
|
||||
else if searching { compactReader = true }
|
||||
}
|
||||
.onDisappear { browseReader.stop(); searchReader.stop() }
|
||||
}
|
||||
|
||||
private func header(narrow: Bool, sidebar side: Bool) -> some View {
|
||||
HStack {
|
||||
if narrow, compactReader {
|
||||
Button { compactReader = false } label: { Label(searching ? "Results" : "Browse", systemImage: "chevron.left") }
|
||||
} else if !side {
|
||||
Button { showCollections.toggle() } label: { Label("Collections", systemImage: "sidebar.left") }
|
||||
.popover(isPresented: $showCollections) { sidebar.frame(width: 255, height: 360) }
|
||||
}
|
||||
HStack(spacing: 5) {
|
||||
Button { queryFocused = true } label: { Image(systemName: "magnifyingglass").foregroundStyle(.secondary) }
|
||||
.buttonStyle(.plain).keyboardShortcut("f", modifiers: [.command, .shift]).accessibilityLabel("Focus Archive search")
|
||||
TextField("Search Archive", text: $archiveQuery.text).textFieldStyle(.plain).focused($queryFocused)
|
||||
.accessibilityLabel("Search Archive").accessibilityIdentifier("archive-search")
|
||||
Button { archiveQuery.text = ""; queryFocused = true } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(.secondary) }
|
||||
.buttonStyle(.plain).accessibilityLabel("Clear Archive search")
|
||||
.opacity(query.isEmpty ? 0 : 1).disabled(query.isEmpty).accessibilityHidden(query.isEmpty)
|
||||
}.padding(6).background(.quaternary, in: RoundedRectangle(cornerRadius: 6))
|
||||
Picker("Search scope", selection: $localScope) {
|
||||
Text("Entire Archive").tag(false)
|
||||
Text(scopePath?.split(separator: "/").last.map(String.init) ?? "Current collection").tag(true).disabled(scopePath == nil)
|
||||
}.labelsHidden().frame(width: narrow ? 115 : 160).accessibilityLabel("Archive search scope")
|
||||
Button { MCPArchiveAccess.shared.show() } label: { Image(systemName: "link") }
|
||||
.help("Archive Search connection for MCP").accessibilityLabel("Archive Search connection for MCP")
|
||||
}.padding(8)
|
||||
}
|
||||
|
||||
private var sidebar: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Button { chooseCollection("") } label: {
|
||||
Label("All Documents", systemImage: "books.vertical")
|
||||
.fontWeight(sidebarPath.isEmpty ? .semibold : .regular)
|
||||
.frame(maxWidth: .infinity, alignment: .leading).padding(6)
|
||||
.background(sidebarPath.isEmpty ? Color.accentColor.opacity(0.13) : .clear, in: RoundedRectangle(cornerRadius: 5))
|
||||
}.buttonStyle(.plain).padding(7).accessibilityAddTraits(sidebarPath.isEmpty ? .isSelected : [])
|
||||
List(selection: Binding<String?>(get: { sidebarPath.isEmpty ? nil : sidebarPath }, set: { if let value = $0 { chooseCollection(value) } })) {
|
||||
Section("Collections") {
|
||||
OutlineGroup(collections, children: \.children) { folder in
|
||||
Label(folder.name, systemImage: folder.children == nil ? "folder" : "folder.fill").tag(folder.id)
|
||||
}
|
||||
}
|
||||
}.listStyle(.sidebar).accessibilityLabel("Archive collections")
|
||||
}
|
||||
}
|
||||
|
||||
private func chooseCollection(_ path: String) {
|
||||
showCollections = false
|
||||
if searching {
|
||||
if path != sidebarPath { searchID = nil; searchReader.reset() }
|
||||
searchFolder = path; searchOffset = 0; localScope = !path.isEmpty; compactReader = false
|
||||
return
|
||||
}
|
||||
guard path != collection else { return }
|
||||
collection = path; browseID = nil; browseOffset = 0; compactReader = false; browseReader.reset()
|
||||
}
|
||||
|
||||
private var listing: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
HStack {
|
||||
Text(searching ? "Matching pages" : (collection.isEmpty ? "All Documents" : String(collection.split(separator: "/").last ?? ""))).font(.headline)
|
||||
Spacer()
|
||||
if searching ? searchBusy : browseBusy { ProgressView().controlSize(.small) }
|
||||
}.padding(10)
|
||||
Divider()
|
||||
if let error = searching ? searchError : browseError {
|
||||
VStack(spacing: 8) { Text(error).multilineTextAlignment(.center); Button("Try Again") { retry += 1 } }.padding().frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else {
|
||||
// Keeping the browse List mounted preserves its scroll position and selection through a search.
|
||||
ZStack {
|
||||
browseList.opacity(searching ? 0 : 1).allowsHitTesting(!searching).accessibilityHidden(searching).disabled(searching)
|
||||
if searching { resultsList }
|
||||
}
|
||||
Divider()
|
||||
HStack {
|
||||
Button { moveBatch(-1) } label: { Image(systemName: "chevron.left") }.accessibilityLabel("Previous results").disabled((searching ? searchOffset : browseOffset) == 0)
|
||||
let count = searching ? hits.count : documents.count
|
||||
if count > 0 { let offset = searching ? searchOffset : browseOffset; Text("\(offset + 1)–\(offset + count)").font(.caption).monospacedDigit() }
|
||||
Button { moveBatch(1) } label: { Image(systemName: "chevron.right") }.accessibilityLabel("Next results").disabled(!(searching ? searchMore : browseMore))
|
||||
Spacer()
|
||||
}.padding(8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var browseList: some View {
|
||||
List(selection: $browseID) {
|
||||
ForEach(documents) { doc in
|
||||
VStack(alignment: .leading, spacing: 3) {
|
||||
Text(doc.title).lineLimit(2)
|
||||
Text(doc.collection.replacingOccurrences(of: "/", with: " › ")).font(.caption).foregroundStyle(.secondary).lineLimit(1)
|
||||
}.padding(.vertical, 2).frame(maxWidth: .infinity, alignment: .leading).contentShape(Rectangle()).tag(doc.id)
|
||||
.simultaneousGesture(TapGesture().onEnded { browseID = doc.id; compactReader = true })
|
||||
}
|
||||
}.accessibilityLabel("Browse Archive documents").accessibilityIdentifier("archive-browse-list")
|
||||
.onKeyPress(.return, action: readSelectedWithKeyboard)
|
||||
.overlay { if documents.isEmpty, !browseBusy { ContentUnavailableView("No documents in this collection", systemImage: "books.vertical") } }
|
||||
}
|
||||
|
||||
private var groups: [ArchiveResultGroup] {
|
||||
var ordered: [ArchiveResultGroup] = []; var indices: [String: Int] = [:]
|
||||
for item in hits {
|
||||
if let index = indices[item.document.id] { ordered[index].hits.append(item) }
|
||||
else { indices[item.document.id] = ordered.count; ordered.append(.init(document: item.document, hits: [item])) }
|
||||
}
|
||||
return ordered
|
||||
}
|
||||
private var resultsList: some View {
|
||||
List(selection: $searchID) {
|
||||
ForEach(groups) { group in
|
||||
Section {
|
||||
ForEach(group.hits) { result in
|
||||
HStack { Text(result.locator); if result.ocr { Text("OCR").font(.caption).foregroundStyle(.secondary) } }
|
||||
.frame(maxWidth: .infinity, alignment: .leading).contentShape(Rectangle()).tag(result.id)
|
||||
.simultaneousGesture(TapGesture().onEnded { searchID = result.id; compactReader = true })
|
||||
}
|
||||
} header: { Text(group.document.title).lineLimit(3).textCase(nil) }
|
||||
}
|
||||
}.accessibilityLabel("Matching Archive pages").accessibilityIdentifier("archive-results-list")
|
||||
.onKeyPress(.return, action: readSelectedWithKeyboard)
|
||||
.overlay { if hits.isEmpty, !searchBusy { ContentUnavailableView("No matching pages", systemImage: "magnifyingglass") } }
|
||||
}
|
||||
|
||||
@ViewBuilder private var detail: some View {
|
||||
if let selected {
|
||||
ArchiveReaderPane(record: selected, hit: searching ? hit : nil, reader: reader, requestedPage: requestedPage, catalog: catalog)
|
||||
} else { ContentUnavailableView("Choose a document", systemImage: "book.pages", description: Text("Browse a collection or search the Archive.")) }
|
||||
}
|
||||
private func moveBatch(_ direction: Int) {
|
||||
if searching { searchOffset = max(0, searchOffset + direction * batch); searchID = nil }
|
||||
else { browseOffset = max(0, browseOffset + direction * batch); browseID = nil }
|
||||
}
|
||||
private func readSelectedWithKeyboard() -> KeyPress.Result {
|
||||
guard selected != nil else { return .ignored }
|
||||
// On compact layouts Return can reopen the current row after Back.
|
||||
Task { @MainActor in compactReader = true }
|
||||
return .handled
|
||||
}
|
||||
private func loadBrowse() async {
|
||||
let request = ArchiveBrowseRequest(collection: collection, offset: browseOffset, retry: retry)
|
||||
browseBusy = true; browseError = nil
|
||||
do {
|
||||
let rows = try await catalog.browse(collection: collection.isEmpty ? nil : collection, limit: batch + 1, offset: browseOffset)
|
||||
try Task.checkCancellation()
|
||||
guard request == ArchiveBrowseRequest(collection: collection, offset: browseOffset, retry: retry) else { return }
|
||||
documents = Array(rows.prefix(batch)); browseMore = rows.count > batch
|
||||
if !documents.contains(where: { $0.id == browseID }) { browseID = nil }
|
||||
browseBusy = false
|
||||
} catch { if !Task.isCancelled { browseError = error.localizedDescription; browseBusy = false } }
|
||||
}
|
||||
private func loadSearch(_ request: ArchiveSearchRequest) async {
|
||||
searchError = nil; hits = []; searchMore = false
|
||||
guard searching else { searchBusy = false; return }
|
||||
searchBusy = true
|
||||
do {
|
||||
try await Task.sleep(for: .milliseconds(180))
|
||||
let rows = try await catalog.search(query: request.query, collection: request.collection, limit: batch + 1, offset: request.offset)
|
||||
try Task.checkCancellation(); guard key == request else { return }
|
||||
hits = Array(rows.prefix(batch)); searchMore = rows.count > batch
|
||||
if !hits.contains(where: { $0.id == searchID }) { searchID = nil }; searchBusy = false
|
||||
} catch { if !Task.isCancelled { searchError = error.localizedDescription; searchBusy = false } }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user