Docs native S1a: a new text view is drawn whole when the old one is gone (#170, final 3)
Fable's final check, finding 3. attach() drew a different text view whole only when the old one was still referenced; the surface holds it weakly, so a rebuilt host whose old view was already released left the new one empty. It now compares the view itself. SurfaceLifecycleTests.testANewTextViewIsDrawnWholeWhenTheOldOneIsGone lets the first view go before attaching the second; it failed before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+26
−5
native/apple/Sources/DocsKit/DocumentSurface.swift+6 −5native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift+20 −0
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift| index 9d26481..967362b 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -137,12 +137,13 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| // MARK: - The text view |
| func attach(_ text: DocsTextView) { |
| - if let old = textView, old !== text { |
| + if textView !== text { |
| // Another text view, without the model opening again (the host |
| - // rebuilt: an iPad split collapsing, a window restructured). |
| - // Let the old one go, and draw the new one whole: what the |
| - // surface remembers is the old storage's, not this one's. |
| - Self.release(old) |
| + // rebuilt: an iPad split collapsing, a window restructured), or |
| + // the first. Let the old one go if it is still there (its host |
| + // may have released it already), and draw the new one whole: |
| + // what the surface remembers is the old storage's, not this one's. |
| + if let old = textView { Self.release(old) } |
| entries = [:]; order = []; sourceCounts = [:] |
| pendingDirty = []; fullDiff = newest != nil |
| } |
diff --git a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift| index ebf6f6c..c192a1a 100644 |
| --- a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| @@ -75,4 +75,24 @@ import XCTest |
| XCTAssertEqual(rendered, [], "the old document's generation is not reported after the reset") |
| XCTAssertEqual(sources.last ?? [], [], "the last image sources the model hears are the reset's, none") |
| } |
| + |
| + /// The old text view is already gone (its host released it) when the |
| + /// new one attaches: the new one is still drawn whole (Fable on #170, |
| + /// final check, finding 3). |
| + func testANewTextViewIsDrawnWholeWhenTheOldOneIsGone() async throws { |
| + let surface = DocumentSurface() |
| + var hooks = DocumentSurface.Hooks(); hooks.canRender = { _ in true } |
| + surface.hooks = hooks |
| + autoreleasepool { |
| + let first = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| + surface.attach(first) |
| + surface.apply(DocumentSurface.Publication(generation: 1, projection: projection(), revision: 1, dirty: nil)) |
| + XCTAssertFalse(Keyboard.storage(of: first).string.isEmpty) |
| + } |
| + for _ in 0..<50 where surface.textView != nil { try await Task.sleep(for: .milliseconds(10)) } |
| + XCTAssertNil(surface.textView, "the first view is gone") |
| + let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| + surface.attach(second) |
| + SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), projection(), "a new view after the old one is gone") |
| + } |
| } |