native: idle starts for the breaks a key leaves below the screen
Fable's delta review on #180, F2. A key rebuilds only the breaks through the screen's end and leaves the rest for idle, but idle started only for paragraphs to measure or place: the breaks below stayed unbuilt (text over the page gaps) until a scroll reached them, and a jump applied the whole cascade at once. `scheduleIdle` now also starts when breaks are waiting and nothing is being composed. The benchmarks README said they waited for idle; it now says what happens. Test: PhonePaginationTests.testIdleBuildsTheBreaksAKeyLeftBelowTheScreen (failed before): with idle settled, a peer's lines above the screen leave breaks waiting below it; idle builds them with nothing on screen moving, and a jump to the end lands where a whole layout puts it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+48
−2
native/apple/Sources/DocsKit/DocsPagedTextView.swift+2 −1native/apple/Tests/DocsKitTests/PhonePaginationTests.swift+43 −0native/benchmarks/README.md+3 −1
diff --git a/native/apple/Sources/DocsKit/DocsPagedTextView.swift b/native/apple/Sources/DocsKit/DocsPagedTextView.swift| index d940666..e15be2d 100644 |
| --- a/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| +++ b/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| @@ -503,7 +503,8 @@ import UIKit |
| /// The rest of the document, a chunk per turn, while nothing else runs. |
| private func scheduleIdle() { |
| - guard idle == nil, pages.firstUnmeasured != nil || pages.settleFrom != nil else { return } |
| + // Also for breaks a key left below the screen (Fable's delta review, F2). |
| + guard idle == nil, pages.firstUnmeasured != nil || pages.settleFrom != nil || (pages.hasChanges && markedTextRange == nil) else { return } |
| idle = Task { @MainActor [weak self] in |
| while !Task.isCancelled { |
| try? await Task.sleep(for: .milliseconds(2)) |
diff --git a/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift b/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift| index 9c06de3..59cdaa4 100644 |
| --- a/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift |
| @@ -165,6 +165,49 @@ import XCTest |
| try checkScreen(text, "and down again") |
| } |
| + /// Breaks a key moves below the screen wait for idle (Fable's delta |
| + /// review, F2): idle rebuilds them, with nothing on screen moving, and a |
| + /// jump to the end afterwards lands where a whole layout puts it. |
| + func testIdleBuildsTheBreaksAKeyLeftBelowTheScreen() async throws { |
| + DocsFonts.register() |
| + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) |
| + window.makeKeyAndVisible() |
| + defer { window.isHidden = true } |
| + let state = SurfaceState(width: 402) |
| + let text = state.text |
| + text.frame = window.bounds |
| + text.paged = true |
| + window.addSubview(text) |
| + defer { text.removeFromSuperview() } |
| + state.markPendingOnEdit = false |
| + var nodes = SurfaceFixture.pages(20) |
| + state.publish(SurfaceFixture.projection(nodes), generation: 1, dirty: nil) |
| + text.layoutIfNeeded() |
| + text.paginateEverything() |
| + text.setContentOffset(CGPoint(x: 0, y: 1200), animated: false) |
| + text.layoutIfNeeded() |
| + // Settled: idle has nothing left from the open. |
| + let settled = Date().addingTimeInterval(3) |
| + while text.isMeasuringWhileIdle && Date() < settled { try await Task.sleep(for: .milliseconds(20)) } |
| + XCTAssertFalse(text.isMeasuringWhileIdle) |
| + // A peer's lines above the screen, enough to move every break after. |
| + let peer = try XCTUnwrap(nodes.firstIndex { $0.id == "p0-1" }) |
| + nodes[peer].children?[0].runs?[0].insert += String(repeating: " A peer's words, enough of them to wrap onto a new line.", count: 5) |
| + state.publish(SurfaceFixture.projection(nodes), generation: 1, dirty: ["p0-1"]) |
| + text.layoutIfNeeded() |
| + XCTAssertTrue(text.pages.hasChanges, "breaks below the screen wait") |
| + let offset = text.contentOffset.y |
| + let deadline = Date().addingTimeInterval(3) |
| + while text.pages.hasChanges && Date() < deadline { try await Task.sleep(for: .milliseconds(20)) } |
| + text.layoutIfNeeded() |
| + XCTAssertFalse(text.pages.hasChanges, "idle built them") |
| + XCTAssertEqual(text.contentOffset.y, offset, accuracy: 0.5, "with nothing on screen moving") |
| + try checkScreen(text, "after idle") |
| + text.setContentOffset(CGPoint(x: 0, y: text.contentSize.height - text.bounds.height), animated: false) |
| + text.layoutIfNeeded() |
| + try checkScreen(text, "a jump to the end") |
| + } |
| + |
| /// Marked text (Fable on #180, required 2): composing in the paragraph |
| /// that carries a page break's space, growing it by a line, moves the |
| /// break, but its fragment is not built again (an attribute edit of the |
diff --git a/native/benchmarks/README.md b/native/benchmarks/README.md| index 9cb0278..6c66ae6 100644 |
| --- a/native/benchmarks/README.md |
| +++ b/native/benchmarks/README.md |
| @@ -276,7 +276,9 @@ The screen's text did not move on any key. Local typing, the open and the |
| jump are unchanged: open 160–165 ms (12 of 1,260 paragraphs), per key |
| median 6.5–6.9 ms and max 9.5–10.5 ms, the jump 478–482 ms with 0 breaks |
| misplaced and 0 of 41 lines away from a whole layout. On a key only the |
| -breaks through the screen's end are rebuilt; those below wait for idle. |
| +breaks through the screen's end are rebuilt; those below are rebuilt |
| +while idle, which starts for them (until Fable's delta review it did not, |
| +and they waited for a scroll to reach them). |
| A moved break still rebuilds its paragraph's fragment (TextKit 2 stacks a |
| fragment's new height only after laying it out again), so a cascade costs |
| about 0.4 ms a break. |