native: idle measuring stops when a pass gets nowhere
Fable on #180 (5). The idle loop ran until every paragraph was measured and placed; a paragraph that cannot be measured (an empty document's) kept it waking every 2 ms for good, and so would breaks held for a composition. A pass that changes nothing now ends it; layout starts it again when there is something new, as does a composition ending. Test: PhonePaginationTests.testIdleMeasuringStopsWhenItGetsNowhere (an empty paged document: failed before, the loop still running).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+27
−0
native/apple/Sources/DocsKit/DocsPagedTextView.swift+11 −0native/apple/Tests/DocsKitTests/PhonePaginationTests.swift+16 −0
diff --git a/native/apple/Sources/DocsKit/DocsPagedTextView.swift b/native/apple/Sources/DocsKit/DocsPagedTextView.swift| index cbab451..93f0d61 100644 |
| --- a/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| +++ b/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| @@ -455,6 +455,9 @@ import UIKit |
| pages.applyChanges(through: limit) |
| } |
| + /// Whether the idle measuring is running (tests). |
| + var isMeasuringWhileIdle: Bool { idle != nil } |
| + |
| /// 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 } |
| @@ -462,10 +465,18 @@ import UIKit |
| while !Task.isCancelled { |
| try? await Task.sleep(for: .milliseconds(2)) |
| guard let self, let layout = self.textLayoutManager else { return } |
| + let before = (self.pages.firstUnmeasured, self.pages.settleFrom) |
| if let hole = self.pages.firstUnmeasured { |
| self.pages.measure(hole..<min(self.pages.paragraphs.count, hole + DocsPhonePages.chunk), in: layout) |
| } |
| self.pages.settle() |
| + // A pass that got nowhere (a paragraph that cannot be |
| + // measured: an empty document; breaks held for a |
| + // composition) ends it; layout starts it again when there is |
| + // something new, and so does a composition ending. |
| + if (self.pages.firstUnmeasured, self.pages.settleFrom) == before, !self.pages.hasChanges || self.markedTextRange != nil { |
| + self.idle = nil; return |
| + } |
| if self.pages.hasChanges && self.markedTextRange == nil { self.applyBreaks(); self.setNeedsLayout() } |
| if self.pages.firstUnmeasured == nil && self.pages.settleFrom == nil { self.idle = nil; self.setNeedsLayout(); return } |
| } |
diff --git a/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift b/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift| index 53e5d25..07156a4 100644 |
| --- a/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift |
| @@ -134,6 +134,22 @@ import XCTest |
| try checkScreen(text, "after the composition") |
| } |
| + /// The idle measuring stops when a pass gets nowhere (Fable on #180): |
| + /// an empty document has a paragraph that cannot be measured, and the |
| + /// loop must not wake every 2 ms for ever. |
| + func testIdleMeasuringStopsWhenItGetsNowhere() async throws { |
| + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) |
| + window.makeKeyAndVisible() |
| + defer { window.isHidden = true } |
| + let text = DocsTextView(frame: window.bounds) |
| + text.paged = true |
| + window.addSubview(text) |
| + defer { text.removeFromSuperview() } |
| + text.layoutIfNeeded() |
| + try await Task.sleep(for: .milliseconds(100)) |
| + XCTAssertFalse(text.isMeasuringWhileIdle, "an empty document leaves nothing running") |
| + } |
| + |
| /// The screen's lines, by where they are drawn. |
| private static func screen(_ text: DocsTextView) -> [(range: NSRange, rect: CGRect)] { |
| PhoneLargeDocumentTests.lines(in: text, near: text.contentOffset.y, height: text.bounds.height).filter { $0.range.length > 0 } |