Sync build 64 manual reader and qualification tests

This commit is contained in:
Brian Holman committed 2026-11-14 08:35:30 -07:00
1 parent 8236ca17c6
commit 4abc194001
5 files changed
+272 -76

No files matched your search

+114 -56
View File
@@ -101,19 +101,26 @@ final class SoftwareLibraryManualModel: ObservableObject {
@Published var page = 1
@Published var searchText = ""
@Published var searchMessage = ""
let pdfView = PDFView()
let pdfView = SoftwareManualPDFView()
private var task: Task<Void, Never>?
private var directory: URL?
private var companion: (any CompanionLibraryAccess)?
#if !VCC_COMPANION_CLIENT
var continuity: LibraryContinuityStore?
private var savedPosition: LibraryReadingPosition?
#endif
private var positionTracking: [NSObjectProtocol] = []
private var lastPositionSave = Date.distantPast
private var restoringPosition = false
@Published private(set) var readingReady = false
func open(_ asset: SoftwareLibraryAsset, directory: URL, companion: (any CompanionLibraryAccess)? = nil) {
if self.asset?.id == asset.id && (document != nil || text != nil) { return }
let retained = self.asset?.id == asset.id && (document != nil || text != nil)
if retained && (readingReady || pdfView.whenLaidOut != nil) { return }
saveReadingPosition(force: true)
stopPositionTracking()
#if !VCC_COMPANION_CLIENT
savedPosition = companion == nil ? continuity?.reading(for: "software-manual:" + asset.id) : nil
#endif
if retained { return }
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 {
@@ -147,7 +154,7 @@ final class SoftwareLibraryManualModel: ObservableObject {
text = decoded; textView.string = decoded
} else {
guard let loaded = PDFDocument(url: local), !loaded.isLocked, loaded.pageCount > 0 else { throw SoftwareLibraryManualCache.Failure.response }
document = loaded; pdfView.document = loaded; pdfView.autoScales = true
document = loaded; pdfView.document = loaded
}
loading = false
} catch is CancellationError { }
@@ -156,9 +163,9 @@ final class SoftwareLibraryManualModel: ObservableObject {
}
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() {
func goToPage(_ number: Int? = nil) {
guard let document else { return }
page = min(max(page, 1), document.pageCount)
page = min(max(number ?? page, 1), document.pageCount)
if let target = document.page(at: page - 1) { pdfView.go(to: target) }
}
func findNext() {
@@ -168,67 +175,106 @@ 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() {
private func stopPositionTracking() {
if readingReady { readingReady = false }
pdfView.whenLaidOut = nil
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)))
guard !readingReady, let asset else { return }
if let document {
pdfView.whenLaidOut = { [weak self] in
guard let self, self.document === document, self.pdfView.document === document else { return }
self.pdfView.autoScales = true
var number = 1
#if !VCC_COMPANION_CLIENT
if let saved = self.savedPosition {
number = min(max(1, saved.page), document.pageCount)
if saved.autoScales == false, let zoom = saved.zoom {
self.pdfView.autoScales = false
self.pdfView.scaleFactor = min(10, max(0.1, zoom))
}
}
} 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
self.pdfView.layoutDocumentView()
self.goToPage(number)
#if !VCC_COMPANION_CLIENT
if let saved = self.savedPosition, let target = document.page(at: number - 1) {
let bounds = target.bounds(for: self.pdfView.displayBox)
let point = CGPoint(x: bounds.minX + bounds.width * saved.horizontalOffset,
y: bounds.minY + bounds.height * saved.offset)
self.pdfView.go(to: PDFDestination(page: target, at: point))
}
#endif
self.trackReadingPosition()
}
pdfView.needsLayout = true
} else if text != nil {
DispatchQueue.main.async { [weak self] in
guard let self, self.asset?.id == asset.id, self.text != nil,
let clip = self.textView.enclosingScrollView?.contentView else { return }
#if !VCC_COMPANION_CLIENT
if let saved = self.savedPosition {
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
self.trackReadingPosition()
}
}
#endif
}
private func trackReadingPosition() {
stopPositionTracking()
guard !readingReady else { return }
readingReady = true
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) }
Task { @MainActor [weak self] in self?.updateReadingPosition(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() }
Task { @MainActor [weak self] in self?.updateReadingPosition() }
})
}
}
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)))
private func updateReadingPosition(force: Bool = false) {
guard readingReady else { return }
if let document, let current = pdfView.currentDestination?.page, current.document === document {
let index = document.index(for: current)
if (0..<document.pageCount).contains(index), page != index + 1 { page = index + 1 }
}
saveReadingPosition(force: force)
}
private func saveReadingPosition(force: Bool = false) {
#if !VCC_COMPANION_CLIENT
guard readingReady, companion == nil, let asset, let continuity,
force || Date().timeIntervalSince(lastPositionSave) >= 1 else { return }
var position = LibraryReadingPosition(documentID: asset.id, page: page)
if let document {
guard let destination = pdfView.currentDestination, let current = destination.page,
current.document === document else { return }
let index = document.index(for: current)
let bounds = current.bounds(for: pdfView.displayBox)
let point = destination.point
let scale = pdfView.scaleFactor
guard bounds.minX.isFinite, bounds.minY.isFinite, bounds.width.isFinite, bounds.height.isFinite,
bounds.width > 0, bounds.height > 0, point.x.isFinite, point.y.isFinite,
scale.isFinite, scale > 0, (0..<document.pageCount).contains(index) else { return }
position.page = index + 1
position.offset = min(1, max(0, (point.y - bounds.minY) / bounds.height))
position.horizontalOffset = min(1, max(0, (point.x - bounds.minX) / bounds.width))
position.zoom = scale; position.autoScales = pdfView.autoScales
} else if text != nil, let clip = textView.enclosingScrollView?.contentView {
position.offset = min(1,max(0,clip.bounds.minY / max(1,textView.bounds.height - clip.bounds.height)))
} else { return }
lastPositionSave = Date()
continuity.setReading(position, for: "software-manual:" + asset.id)
#endif
}
@@ -252,26 +298,26 @@ struct SoftwareLibraryManualView: View {
Button { model.pdfView.zoomIn(nil) } label: { Image(systemName: "plus.magnifyingglass") }.help("Zoom in").accessibilityLabel("Zoom in")
Button("Fit Page") { model.pdfView.autoScales = true }.accessibilityIdentifier("manual-fit")
}
}.padding(12)
}.disabled(model.document != nil && !model.readingReady).padding(12)
if let document = model.document {
HStack {
Button { model.pdfView.goToPreviousPage(nil) } label: { Image(systemName: "chevron.left") }.help("Previous page").accessibilityLabel("Previous page").disabled(model.page <= 1)
Button { model.goToPage(model.page - 1) } label: { Image(systemName: "chevron.left") }.help("Previous page").accessibilityLabel("Previous page").disabled(model.page <= 1)
TextField("Page", value: $model.page, format: .number).frame(width: 45).onSubmit { model.goToPage() }.accessibilityLabel("Page number")
Text("of \(document.pageCount)").foregroundStyle(.secondary)
Button { model.pdfView.goToNextPage(nil) } label: { Image(systemName: "chevron.right") }.help("Next page").accessibilityLabel("Next page").disabled(model.page >= document.pageCount)
Button { model.goToPage(model.page + 1) } label: { Image(systemName: "chevron.right") }.help("Next page").accessibilityLabel("Next page").disabled(model.page >= document.pageCount)
Spacer()
TextField("Find in manual", text: $model.searchText).frame(maxWidth: 230).onSubmit { model.findNext() }.accessibilityIdentifier("manual-search")
Button("Find Next") { model.findNext() }.disabled(model.searchText.isEmpty)
}.padding(.horizontal, 12).padding(.bottom, 10)
}.disabled(!model.readingReady).padding(.horizontal, 12).padding(.bottom, 10)
}
Divider()
if model.document != nil {
HStack(spacing: 0) {
if showPages { ManualPages(view: model.pdfView).frame(width: 115) }
ManualPDF(view: model.pdfView).accessibilityIdentifier("manual-pdf").onAppear { model.restoreReadingPosition() }
ManualPDF(view: model.pdfView).id(model.asset?.id).accessibilityIdentifier("manual-pdf").onAppear { model.restoreReadingPosition() }
}
} else if model.text != nil {
ManualText(view: model.textView).accessibilityIdentifier("manual-text").onAppear { model.restoreReadingPosition() }
ManualText(view: model.textView).id(model.asset?.id).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 {
@@ -292,13 +338,25 @@ struct SoftwareLibraryManualView: View {
}
}.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() }
}
}
// PDFKit chooses its fit scale from the attached view's viewport. Its document and
// scale notifications before that layout do not represent a reading position.
final class SoftwareManualPDFView: PDFView {
var whenLaidOut: (() -> Void)?
override func layout() {
super.layout()
guard whenLaidOut != nil, window != nil, !bounds.isEmpty else { return }
DispatchQueue.main.async { [weak self] in
guard let self, self.window != nil, !self.bounds.isEmpty else { return }
let restore = self.whenLaidOut
self.whenLaidOut = nil
restore?()
}
}
}
private struct ManualPDF: NSViewRepresentable {
let view: PDFView
func makeNSView(context: Context) -> PDFView {