oficina / docs Public

native: page breaks wait for a composition to end

Fable on #180 (required 2). Building a moved break's fragment again is an
attribute edit of the paragraph before it (`storage.edited`), and that
paragraph can be the one an input method is composing in (ja, zh): a
line added by marked text moves the break below it. The text view now
holds break rebuilds while it has marked text (`applyBreaks`, every
caller: the restack before layout, paginate, a jump, idle, a whole
layout), and lays out again when the composition ends, which builds them.

Test: PhonePaginationTests.testABreakWaitsForACompositionToEnd composes
Japanese marked text (setMarkedText, as an IME does) at the end of the
paragraph carrying the first break's space, adding a line: no break is
built again while composing (it was one before), the marked text
survives, and after unmarkText the break is built and the screen matches
a whole layout. Not confirmed with a live ja keyboard on the simulator.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev a6fb32836ee3fe5292b426f0fa78bae8a44d29df parent 589c1af
3 files changed, +59 −6
  • native/apple/Sources/DocsKit/DocsPagedTextView.swift +16 −6
  • native/apple/Sources/DocsKit/DocumentComposition.swift +2 −0
  • native/apple/Tests/DocsKitTests/PhonePaginationTests.swift +41 −0
diff --git a/native/apple/Sources/DocsKit/DocsPagedTextView.swift b/native/apple/Sources/DocsKit/DocsPagedTextView.swift
index ce6b2b0..cbab451 100644
--- a/native/apple/Sources/DocsKit/DocsPagedTextView.swift
+++ b/native/apple/Sources/DocsKit/DocsPagedTextView.swift
@@ -378,7 +378,7 @@ import UIKit
let last = (pages.paragraph(at: end) ?? 0) + 1
pages.measureHoles(through: last, in: layout)
pages.settle()
- pages.applyChanges(through: last)
+ applyBreaks(through: last)
pages.ensureLayout(through: end, in: layout)
return anchor
}
@@ -419,8 +419,8 @@ import UIKit
pages.measureHoles(through: last, in: layout)
pages.settle()
// Breaks below the screen wait for idle.
- let changed = pages.hasChanges(through: last)
- pages.applyChanges(through: last)
+ let changed = pages.hasChanges(through: last) && markedTextRange == nil
+ applyBreaks(through: last)
let walked = pages.ensureLayout(through: end, in: layout)
guard changed || walked else { break }
layout.textViewportLayoutController.layoutViewport()
@@ -440,11 +440,21 @@ import UIKit
DocsProbes.relaidParagraphs += (pages.paragraph(at: end) ?? 0) - (pages.paragraph(at: span.lowerBound) ?? 0) + 1
#endif
layout.invalidateLayout(for: range)
- pages.applyChanges()
+ applyBreaks()
pages.ensureLayout(through: end, in: layout)
layout.textViewportLayoutController.layoutViewport()
}
+ /// The breaks that moved built again (through paragraph `limit`), unless
+ /// text is being composed: building one again is an attribute edit of
+ /// its paragraph, which may be the one an input method is composing in
+ /// (Fable on #180). They wait for the composition to end, which lays
+ /// out again (`DocsTextView`).
+ private func applyBreaks(through limit: Int = .max) {
+ guard markedTextRange == nil else { return }
+ pages.applyChanges(through: limit)
+ }
+
/// 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 }
@@ -456,7 +466,7 @@ import UIKit
self.pages.measure(hole..<min(self.pages.paragraphs.count, hole + DocsPhonePages.chunk), in: layout)
}
self.pages.settle()
- if self.pages.hasChanges { self.pages.applyChanges(); self.setNeedsLayout() }
+ 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 }
}
}
@@ -470,7 +480,7 @@ import UIKit
if pages.firstUnmeasured == hole { break }
}
pages.settle()
- pages.applyChanges()
+ applyBreaks()
pages.ensureLayout(through: textStorage.length, in: layout)
layout.textViewportLayoutController.layoutViewport()
setNeedsLayout(); layoutIfNeeded()
diff --git a/native/apple/Sources/DocsKit/DocumentComposition.swift b/native/apple/Sources/DocsKit/DocumentComposition.swift
index 0b44394..43856ef 100644
--- a/native/apple/Sources/DocsKit/DocumentComposition.swift
+++ b/native/apple/Sources/DocsKit/DocumentComposition.swift
@@ -160,6 +160,8 @@ final class DocsTextView: DocsPagedTextView {
if composing && markedTextRange == nil { finishComposition() }
}
private func finishComposition() {
+ // Page breaks held while composing are built now.
+ setNeedsLayout()
guard let original else { return }
if compositionCommitted?(original, text) == false { attributedText = original }
self.original = nil; compositionChanged?(false)
diff --git a/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift b/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift
index f2acab6..53e5d25 100644
--- a/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift
+++ b/native/apple/Tests/DocsKitTests/PhonePaginationTests.swift
@@ -93,6 +93,47 @@ import XCTest
}
}
+ /// 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
+ /// paragraph being composed) until the composition ends; then it is,
+ /// and the break is where it was placed.
+ func testABreakWaitsForACompositionToEnd() 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
+ state.publish(SurfaceFixture.projection(SurfaceFixture.pages(6)), generation: 1, dirty: nil)
+ text.layoutIfNeeded()
+ text.paginateEverything()
+ // The first break, and the paragraph before it, which carries its space.
+ let broken = try XCTUnwrap(text.pages.paragraphs.indices.first { $0 > 0 && text.pages.paragraphs[$0].extra > 0 })
+ let carrier = text.pages.range(of: broken - 1)
+ text.setContentOffset(CGPoint(x: 0, y: max(0, (text.pages.paragraphs[broken].bottom ?? 0) - 400)), animated: false)
+ text.layoutIfNeeded()
+ XCTAssertTrue(text.becomeFirstResponder())
+ text.selectedRange = NSRange(location: carrier.location + carrier.length - 1, length: 0)
+ let changes = DocsProbes.pageBreakChanges
+ // Japanese input, as an IME marks it: enough to add a line.
+ let marked = String(repeating: "にほんごのにゅうりょく", count: 4)
+ text.setMarkedText(marked, selectedRange: NSRange(location: marked.utf16.count, length: 0))
+ text.layoutIfNeeded()
+ XCTAssertNotNil(text.markedTextRange, "still composing")
+ XCTAssertEqual(DocsProbes.pageBreakChanges, changes, "no break built again while composing")
+ text.unmarkText()
+ text.layoutIfNeeded()
+ XCTAssertNil(text.markedTextRange)
+ XCTAssertGreaterThan(DocsProbes.pageBreakChanges, changes, "the break is built again once the composition ends")
+ try checkScreen(text, "after the composition")
+ }
+
/// 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 }