Repository navigation
Add contentSettled() to wait for a view's final content - #31
Merged
Merged
Conversation
The layout engine and the image store now count their work in a PendingWork counter. A drain counts from when it is scheduled until it ends. A result counts until the view takes it from the mailbox. A picture load, a formula render and a code highlight count until they end, also when they return nothing. The relayout on main after a picture, a formula or code colors arrive counts too. The count reaches zero only when no work is left. A later commit uses it to tell when the view shows its final content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptStream now has an internal isIdle and whenIdle. The stream is idle when no appended text waits for a frame and no parse runs. By then the attached views have every version. Text that the parser holds back makes no new version, so the views get no update for it. whenIdle lets a view know when such a parse ends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A snapshot or a test needs to know when the view has nothing pending. Layouts of later updates, images, formulas and code colors come back on later turns of the main run loop, so a harness that does not wait for them misses them. TranscriptTextView.contentSettled() returns when no layout, stream parse, image load, formula or code colors are pending, the last layout is on screen and its tiles are drawn. The tiles take back the tail first, so that they hold all of the text. While a stream that has not finished goes on, it returns at the first moment when nothing is pending. A cancelled task returns at once. The bookkeeping is in ContentSettledWaiters, which has no UIKit code, so the macOS tests drive it with a layout engine and a stream. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptStream.flush() runs its idle actions inline. The waiters therefore laid out and drew the view inside the caller's flush() call, which can be in the middle of a SwiftUI update or a layout pass. The stream's idle callback now hops to main before it checks again, as the work counter's callback does. PendingWork.end() without a begin() now stops only debug builds. A release build keeps the count at zero, so a counting mistake does not crash an app that never waits. New tests cover a flush while a task waits, work that the layout on main starts, the fade settling on the layout queue, and a stream that changes while a task waits. Two tests no longer sleep before they check a state that only the test can change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A load, a formula or a highlight that never ends holds back contentSettled() until the task is cancelled. A view with no width lays out nothing, so it settles at once with no content. The doc comment and the README now say both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test aFlushDoesNotSettleInsideTheCall failed in some full test runs. It started a second task for the wait and yielded until the wait registered. The wait registered at once. On a busy machine, the test task did not run again on main before the frame, 1/60 s after the append. The frame parsed the text that the parser holds back, so the wait settled before the test called flush(). The test task now calls wait() itself. The test puts a block in the main queue before the append. The block records the waiter count, calls flush(), and records the draw count. The append starts the frame timer after the block is in the queue, so the block always runs first. The test checks the same three facts: one wait is registered before the flush, the flush does not draw, and the wait resumes after the flush. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A callback of the layout, such as onSelectionChange, can append to the stream. An append starts no work, so the check after the layout saw no work and let the waiting tasks go before the text was parsed. Now the check looks at the stream before and after the layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A style or a renderer that the app sets before the document, on a view that has a width, makes the view lay out the empty text first. The document then shows on a later turn of the main run loop. The README said that plain text is laid out at once, which is not true in that case, and the example of the section is that case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 24, 2026
Xcode 26.6 rejects the weak capture of self in the cancel handler of contentSettled(): it says that sending self risks data races. The waiters are main actor isolated and so Sendable, and the handler runs one time, so a strong capture is safe and builds with each Xcode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part 1 of a stack of 13 pull requests. The base is
main.Each part holds only its own change. The merge commits in a part join the branch below it, with the conflicts between the changes resolved. To merge the stack, start at the top: merge part 13 into its base, then part 12, and go down to this part, which you merge into
mainlast. Each merge then has no conflicts.On this branch,
swift testpasses (195 tests), and the iOS build passes.Summary
A snapshot test or an app must know when a
TranscriptTextViewhas nothing pending. Layouts of later updates, images, formulas and code colors come back on later turns of the main run loop. Plain text can also show on a later turn. This change addscontentSettled(), which returns when the view shows its final content.TranscriptTextView.contentSettled()is the new public API. Before it returns, the view lays out if it must and draws the tiles that wait for the next commit. While a stream goes on, it returns at the first moment when nothing is pending. It does not wait for fades and tag animations.PendingWorkcounts the layout drains, the fade settling, the results that the view has not taken, and the picture, formula and highlight work.TranscriptStreamgetsisIdleandwhenIdle.ContentSettledWaiterslets the tasks go when the count is zero, the stream is idle and the view has drawn.onSelectionChangecan append to the stream. A cancelled task returns at once. Aflush()call does not lay out or draw inside the call.Tests
swift teston macOS: 195 tests pass. The new tests are inContentSettledTests.swiftandStreamTests.swift.aFlushDoesNotSettleInsideTheCallno longer depends on the frame timer.xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS' CODE_SIGNING_ALLOWED=NO: passes.Checked on an iPhone 12 mini
A check screen in a local test app ran on main first (5 runs) and showed the problem. Then it ran on this branch (6 Release runs and 1 Debug run). Stand-ins: the check calls the API from code. Its renderers have fixed delays: 200 ms for the formula, 100 ms for the code colors and 300 ms for the picture. The snapshots come from
layer.render(in:).Task.yield, the snapshot shows the TeX source, the code in the text color and the alt text. It has 0 red code pixels and 0 picture pixels, and the formula area is 0% dark.contentSettled()returns 319 to 341 ms after the set, 6 to 10 ms after the picture. The snapshot has 504 red code pixels and 7200 picture pixels, and the formula area is 92% dark. 0 pixels change 1 s later.PendingWorkassertion.Not checked
🤖 Generated with Claude Code