Docs native S1b: no whole-rest measuring once the text view leaves its window
Fable on #173, finding 1. An idle chunk already scheduled when the text view left its window ran with canDefer() false, and so measured the whole rest of the document in one main-thread turn (1.7 s at 400 pages). The chunk now returns unless the view is in a window, and the next layout in a window schedules the measuring again. Test: 400 pages, the view taken out of its window mid-measure, nothing measured for 300 ms; back in a window, measuring completes. It failed before (the frontier jumped to the end).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+28
−1
native/apple/Sources/DocsKit/DocsPagination.swift+7 −1native/apple/Tests/DocsKitTests/PaginationLargeDocumentTests.swift+21 −0
diff --git a/native/apple/Sources/DocsKit/DocsPagination.swift b/native/apple/Sources/DocsKit/DocsPagination.swift| index 4935eef..9e85689 100644 |
| --- a/native/apple/Sources/DocsKit/DocsPagination.swift |
| +++ b/native/apple/Sources/DocsKit/DocsPagination.swift |
| @@ -305,6 +305,9 @@ final class DocsMeasureTextContainer: NSTextContainer { |
| let deferring = canDefer() |
| let wantsMore = frontier < units.count && (!deferring || chunk > 0 || frontierTop() <= visibleBottom |
| || units[frontier].location <= (character ?? -1)) |
| + // In a window with more to measure and no chunk waiting (it was |
| + // dropped while the view was out of its window): carry on. |
| + if deferring, frontier < units.count, !wantsMore { scheduleChunk() } |
| guard breaksFrom != nil || stale > 0 || wantsMore else { return } |
| #if DOCS_PROBES |
| let probeStart = DocsProbes.now() |
| @@ -467,7 +470,10 @@ final class DocsMeasureTextContainer: NSTextContainer { |
| MainActor.assumeIsolated { |
| guard let self else { return } |
| self.chunkScheduled = false |
| - guard self.frontier < self.units.count else { return } |
| + // Out of a window (the view was removed with measuring still |
| + // to do): never the whole rest in this one turn. The next |
| + // layout in a window picks it up. |
| + guard self.frontier < self.units.count, self.canDefer() else { return } |
| // One chunk past the frontier, then the next turn. |
| self.update(visibleBottom: -.greatestFiniteMagnitude, chunk: Self.chunk) |
| } |
diff --git a/native/apple/Tests/DocsKitTests/PaginationLargeDocumentTests.swift b/native/apple/Tests/DocsKitTests/PaginationLargeDocumentTests.swift| index 3aaadad..f8bbf1b 100644 |
| --- a/native/apple/Tests/DocsKitTests/PaginationLargeDocumentTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/PaginationLargeDocumentTests.swift |
| @@ -125,6 +125,27 @@ import XCTest |
| assertEndChecks(document, "jumped before the idle chunks") |
| } |
| + /// The text view leaving its window while the rest is still being |
| + /// measured (Fable on #173, finding 1): the pending idle chunk must not |
| + /// then measure the whole rest in one turn (1.7 s at 400 pages). It |
| + /// waits, and measuring resumes once the view is in a window again. |
| + func testLeavingTheWindowMidMeasureMeasuresNothingMore() async throws { |
| + DocsFonts.register() |
| + let document = Window(Self.pages(400)) |
| + defer { document.close() } |
| + let view = document.view |
| + let frontier = view.pagination.frontier |
| + XCTAssertLessThan(frontier, view.pagination.units.count, "measuring is still going on") |
| + document.window.contentView = nil |
| + XCTAssertNil(view.window) |
| + try await Task.sleep(for: .milliseconds(300)) |
| + XCTAssertEqual(view.pagination.frontier, frontier, "nothing measured out of a window") |
| + document.window.contentView = document.scroll |
| + view.needsLayout = true |
| + document.window.displayIfNeeded() |
| + try await until("measuring resumes and completes") { view.pagination.isComplete } |
| + } |
| + |
| #if DOCS_PROBES |
| /// §7's line-fragment gate, per key at p20 and p200: TextKit's requests |
| /// to the live container (counted inside `lineFragmentRect`, so its own |