Skip to content

Measure streamed text in views that SwiftUI has not placed - #15

Merged
servercimen merged 1 commit into
mainfrom
fix/stream-in-lazy-stack
Nov 20, 2026
Merged

servercimen merged 1 commit into
mainfrom
fix/stream-in-lazy-stack

Conversation

@servercimen

@servercimen servercimen commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

A lazy stack measures a TranscriptText(stream:) row of zero height but does not place it. The view then has no width, so the engine does not lay out the text that arrives, and invalidateIntrinsicContentSize() reaches no one. The row stays empty.

With this change, while the view has no width of its own:

  • sizeThatFits keeps the proposed width and asks the engine to lay out new text at that width (LayoutEngine.layOutUntilPlaced).
  • When such a layout changes the height, the view calls onHeightChangeWhileUnsized.
  • TranscriptText then changes a @State value that its update reads, so SwiftUI measures the view again.

When the view gets a width, the normal path takes over. Views that already have a width, and TranscriptTextViews used directly from UIKit, do not use this path.

Refs #12.

Results

The repros from #12 on an iPhone 17 Pro, iOS 27.0.1:

Case Before After
Repro 1: an empty row in a LazyVStack, text a second later never shown shown in 3 of 3 runs
Repro 2: an answer row after 50 gray rows of 2,432 pt never shown shown in 3 of 3 runs
Repro 2 with 50 TranscriptText rows from the rich corpus never shown shown in 2 of 2 runs
Repro 2 with 50 gray rows of 1,400 pt never shown not shown in 3 of 3 runs

What this does not fix

In the last case, the lazy stack estimates the new row at about the average row height and keeps the scroll offset at that estimated bottom. The real row is above the visible area, and SwiftUI does not update it: updateUIView and sizeThatFits are not called again, so the state change does not reach the view.

A workaround in the app is to put the streaming row after the LazyVStack, not inside it. That works with and without this change. #12 stays open for this case.

Tests

🤖 Generated with Claude Code

A lazy stack measures a TranscriptText(stream:) row of zero height but
does not place it. The view then has no width, so the engine does not lay
out the text that arrives, and SwiftUI never measures the view again.

While the view has no width of its own, the engine now lays out new text
at the width of the last sizeThatFits. When that changes the height, the
view tells TranscriptText, which changes a state value that its update
reads, so SwiftUI measures the view again. Once the view has a width, the
normal path takes over.

Refs #12.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen force-pushed the fix/stream-in-lazy-stack branch from e8fa64f to 593c9d1 Compare November 20, 2026 18:50
@servercimen
servercimen merged commit 5f5178c into main Nov 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant