Sync source edition with VCC Plus 3.7.0 build 63

This commit is contained in:
Brian Holman committed 2026-11-14 01:43:46 -07:00
1 parent c652a5276d
commit b2ecec4d87
86 files changed
+10519 -472

No files matched your search

@@ -105,8 +105,15 @@ final class SoftwareLibraryManualModel: ObservableObject {
private var task: Task<Void, Never>?
private var directory: URL?
private var companion: (any CompanionLibraryAccess)?
#if !VCC_COMPANION_CLIENT
var continuity: LibraryContinuityStore?
#endif
private var positionTracking: [NSObjectProtocol] = []
private var lastPositionSave = Date.distantPast
private var restoringPosition = false
func open(_ asset: SoftwareLibraryAsset, directory: URL, companion: (any CompanionLibraryAccess)? = nil) {
if self.asset?.id == asset.id && (document != nil || text != nil) { return }
saveReadingPosition(force: true)
task?.cancel(); self.asset = asset; self.directory = directory; self.companion = companion
document = nil; pdfView.document = nil; text = nil; textView.string = ""; page = 1; searchText = ""; searchMessage = ""; error = nil; loading = true
task = Task {
@@ -124,11 +131,16 @@ final class SoftwareLibraryManualModel: ObservableObject {
let size = values.fileSize, size > 0, size <= 64 * 1024 * 1024 else { throw SoftwareLibraryManualCache.Failure.response }
}.value
local = shared
} else if asset.matchStatus == "personal", let url = asset.manualURL, url.isFileURL {
local = try await Task.detached(priority: .userInitiated) {
try SoftwareLibraryLocalAsset.validate(asset, url: url, maximumBytes: url.pathExtension.lowercased() == "txt" ? 1024 * 1024 : 512 * 1024 * 1024)
return url
}.value
} else {
local = try await SoftwareLibraryManualCache.shared.localCopy(for: asset, directory: directory)
}
try Task.checkCancellation()
if local.pathExtension == "txt" {
if local.pathExtension.lowercased() == "txt" {
let bytes = try await Task.detached(priority: .userInitiated) { try Data(contentsOf: local) }.value
try Task.checkCancellation()
let decoded = try Self.decodeText(bytes)
@@ -142,7 +154,7 @@ final class SoftwareLibraryManualModel: ObservableObject {
catch { self.error = error.localizedDescription; loading = false }
}
}
func cancel() { task?.cancel(); loading = false }
func cancel() { saveReadingPosition(force: true); task?.cancel(); loading = false; stopPositionTracking() }
func retry() { if let asset, let directory { open(asset, directory: directory, companion: companion) } }
func goToPage() {
guard let document else { return }
@@ -156,6 +168,70 @@ final class SoftwareLibraryManualModel: ObservableObject {
if let selection { pdfView.setCurrentSelection(selection, animate: false); pdfView.go(to: selection); searchMessage = "" }
else { searchMessage = "No matching text. Scanned pages may not contain searchable text." }
}
func stopPositionTracking() {
for token in positionTracking { NotificationCenter.default.removeObserver(token) }
positionTracking = []
}
func restoreReadingPosition() {
#if !VCC_COMPANION_CLIENT
guard companion == nil, let asset, let continuity else { return }
let identity = asset.id
let saved = continuity.reading(for: "software-manual:" + identity)
restoringPosition = true
DispatchQueue.main.async { [weak self] in
guard let self, self.asset?.id == identity else { return }
defer { self.restoringPosition = false; self.trackReadingPosition() }
if let saved, let document = self.document {
let index = min(max(0,saved.page-1),document.pageCount-1)
self.page = index+1
if let page = document.page(at: index) {
self.pdfView.autoScales = saved.autoScales ?? true
if saved.autoScales == false, let zoom = saved.zoom { self.pdfView.scaleFactor = min(10,max(0.1,zoom)) }
let bounds = page.bounds(for: self.pdfView.displayBox)
self.pdfView.go(to: PDFDestination(page: page, at: CGPoint(x: bounds.minX + bounds.width * saved.horizontalOffset,
y: bounds.minY + bounds.height * saved.offset)))
}
} else if let saved, let clip = self.textView.enclosingScrollView?.contentView {
let height = max(0,self.textView.bounds.height - clip.bounds.height)
clip.scroll(to: NSPoint(x: 0,y: height * saved.offset))
self.textView.enclosingScrollView?.reflectScrolledClipView(clip)
}
}
#endif
}
private func trackReadingPosition() {
stopPositionTracking()
for name in [Notification.Name.PDFViewPageChanged, .PDFViewScaleChanged] {
positionTracking.append(NotificationCenter.default.addObserver(forName: name, object: pdfView, queue: .main) { [weak self] _ in
Task { @MainActor [weak self] in self?.saveReadingPosition(force: true) }
})
}
let clip = document == nil ? textView.enclosingScrollView?.contentView : pdfView.documentView?.enclosingScrollView?.contentView
if let clip {
clip.postsBoundsChangedNotifications = true
positionTracking.append(NotificationCenter.default.addObserver(forName: NSView.boundsDidChangeNotification, object: clip, queue: .main) { [weak self] _ in
Task { @MainActor [weak self] in self?.saveReadingPosition() }
})
}
}
func saveReadingPosition(force: Bool = false) {
#if !VCC_COMPANION_CLIENT
guard !restoringPosition, companion == nil, let asset, let continuity, document != nil || text != nil,
force || Date().timeIntervalSince(lastPositionSave) >= 1 else { return }
lastPositionSave = Date()
var position = LibraryReadingPosition(documentID: asset.id,page: page)
if let document, let destination = pdfView.currentDestination, let current = destination.page {
position.page = document.index(for: current)+1
let bounds = current.bounds(for: pdfView.displayBox)
position.offset = bounds.height > 0 ? min(1,max(0,(destination.point.y-bounds.minY)/bounds.height)) : 0
position.horizontalOffset = bounds.width > 0 ? min(1,max(0,(destination.point.x-bounds.minX)/bounds.width)) : 0
position.zoom = pdfView.scaleFactor; position.autoScales = pdfView.autoScales
} else if let clip = textView.enclosingScrollView?.contentView {
position.offset = min(1,max(0,clip.bounds.minY / max(1,textView.bounds.height - clip.bounds.height)))
}
continuity.setReading(position, for: "software-manual:" + asset.id)
#endif
}
}
struct SoftwareLibraryManualView: View {
@@ -192,10 +268,10 @@ struct SoftwareLibraryManualView: View {
if model.document != nil {
HStack(spacing: 0) {
if showPages { ManualPages(view: model.pdfView).frame(width: 115) }
ManualPDF(view: model.pdfView).accessibilityIdentifier("manual-pdf")
ManualPDF(view: model.pdfView).accessibilityIdentifier("manual-pdf").onAppear { model.restoreReadingPosition() }
}
} else if model.text != nil {
ManualText(view: model.textView).accessibilityIdentifier("manual-text")
ManualText(view: model.textView).accessibilityIdentifier("manual-text").onAppear { model.restoreReadingPosition() }
} else if model.loading {
VStack(spacing: 12) { ProgressView("Downloading manual…"); Button("Cancel") { model.cancel() } }.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
@@ -211,12 +287,14 @@ struct SoftwareLibraryManualView: View {
HStack {
Text(model.asset?.credit ?? "Manual preserved by Color Computer Archive").foregroundStyle(.secondary)
Spacer()
if let asset = model.asset { Link(asset.matchStatus == "project-owned" ? "Project source" : "Archive source", destination: asset.sourceURL) }
if let asset = model.asset, asset.matchStatus != "personal" { Link(asset.sourceURL.host?.lowercased() == "colorcomputerarchive.com" ? "Archive source" : "Project source", destination: asset.sourceURL) }
else if model.asset?.matchStatus == "personal" { Text("Private — On This Mac").foregroundStyle(.secondary) }
}
}.font(.caption).padding(10)
}.frame(minWidth: 620, minHeight: 480)
.onReceive(NotificationCenter.default.publisher(for: .PDFViewPageChanged, object: model.pdfView)) { _ in
if let page = model.pdfView.currentPage, let document = model.document { model.page = document.index(for: page) + 1 }
model.saveReadingPosition(force: true)
}
.onDisappear { model.cancel() }
}