Skip to content

Add an option to fade in text as it streams in - #27

Merged
servercimen merged 1 commit into
mainfrom
feature/fade-in
Nov 20, 2026
Merged

servercimen merged 1 commit into
mainfrom
feature/fade-in

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Summary

Text that streams in appears at once. Some apps fade it in. If a view does that by changing the text's color on every frame, the main thread lays out and redraws the paragraph on every frame.

This PR adds an optional fade-in. Core Animation plays it, and the main thread does no work for each frame of a fade:

  • TranscriptTextView.textFadeInDuration and TranscriptText.fadesInText(_:duration:) turn it on. It is off by default. It is also off with Reduce Motion and with a line limit.
  • Each update's new text is one fade, so the fade follows the text as it arrives. The tail and the tiles leave out the glyphs from a frontier on: the start of the line that the oldest fade in progress is on. So a line is drawn again once, when all of its text is done, not once for each fade. Fades done before then stay at full opacity.
  • A tail that moves down keeps the fading text in it while it can hold the text. So the tail draws that text again off the main thread, not the tiles.
  • On the layout queue, the engine draws one piece for each fade on each line, with only that fade's glyphs. A piece is kept while its glyphs stay where they are and keep their style. It is drawn again when they move (a word wraps, a centered line moves) or change (code colors arrive).
  • The view shows the pieces in layers with an opacity animation. It gives a layer a new image only when the piece is new, so an update hands Core Animation about one small image.
  • When a fade ends, the next update draws its rows again in the tail or the tiles, and its layers go in the same transaction. When updates stop, the engine ends the fades and draws their rows into the tail, as an update that adds no text.
  • A relayout after the image store changes (a picture, a formula, code colors) keeps the fades if their text did not move.
  • Glyphs and their underline and strikethrough fade. Backgrounds, list markers, images and formulas show at once.
  • A tag that starts its animation ends the fades, and its update does not fade, so its letters are not drawn twice.

Strikethrough and underline now cover only the glyphs that are drawn, and their ends sit on whole pixels. So a line drawn in two parts covers the same pixels as one.

Tests

  • Nine new unit tests. The main one streams Markdown through the layout engine with a test clock, at three chunk sizes and with centered text. At every third update, it checks that the drawing with the fading text left out, plus all the pieces at full opacity, gives the same pixels as a drawing in one go. It checks this for the document from the fading text down, and for the tail surface. The sample has a word that wraps, links, strikethrough, inline code, a list, a code block and a table with a cell that wraps.
  • Other tests check that the frontier moves a line at a time and its rows are drawn again, that the engine draws the faded text into the tail when updates stop, that unchanged pieces keep their images, that a piece whose style changes is drawn again, that nothing fades without a duration, that a tag that starts its animation ends the fades, that a relayout ends them, and that a relayout for the image store keeps fades that did not move. The main test also checks that the fading text is in the tail whenever the tail can hold it.
  • swift test: 171 tests pass. The package builds for iOS.
  • On an iPhone 12 mini, a local demo app streams an answer with the fade on: the newest words are lighter, and the finished text is at full opacity with no glyph drawn twice. Code that streams in with a highlighter fades too and ends in its colors.

Cost on an iPhone 12 mini

A local benchmark app streams an answer into a TranscriptText(stream:) row of a LazyVStack, with the fade off and on. The table gives the median of three runs.

Scenario Main-thread CPU, off → on Process CPU, off → on Dropped frames, off / on Memory peak, off → on
6,000 characters at 300 a second, after 50 messages 1.96 → 2.91 s 4.76 → 7.19 s 0 / 0 48 → 55 MB
The same, with a 0.6 s pause every 1.5 s 2.17 → 3.04 s 5.22 → 7.44 s 0 / 0 48 → 55 MB
20,000 characters at 2,000 a second 2.50 → 2.76 s 4.42 → 4.91 s 0 / 0 63 → 103 MB

Over a 20 s answer, the fade adds about 1 s of main-thread work and about 1.5 s of work on other threads. No run dropped a frame, and the worst frame stayed at 16.8 ms. At 2,000 characters a second, more text fades at once, so more pieces are in memory.

🤖 Generated with Claude Code

Text that streams in appeared at once. Some apps fade it in, but doing
that by changing the text's color on every frame costs the main thread
a layout and a redraw of the paragraph for each frame.

`TranscriptTextView.textFadeInDuration` and
`TranscriptText.fadesInText(_:duration:)` turn on a fade. It is off by
default, and off with Reduce Motion and with a line limit.

- Each update's new text is one fade. The tail and the tiles leave out
  the glyphs from a frontier on: the start of the line that the oldest
  fade in progress is on. So a line is drawn again once, when all of its
  text is done, not once for each fade. Fades done before then stay at
  full opacity.
- A tail that moves down keeps the fading text in it while it can hold
  the text, so the tail draws that text again off the main thread, not
  the tiles.
- On the layout queue, the engine draws one piece for each fade on each
  line, with only that fade's glyphs. A piece is kept while its glyphs
  stay where they are and keep their style, and drawn again when they
  move (a word wraps, a centered line moves) or change (code colors
  arrive).
- The view shows the pieces in layers with an opacity animation, which
  Core Animation plays. It gives a layer a new image only when the piece
  is new, so an update copies about one small image.
- When a fade ends, the next update draws its rows again in the tail or
  the tiles, and its layers go in the same transaction. When updates
  stop, the engine ends the fades and draws their rows into the tail, as
  an update that adds no text.
- A relayout after the image store changes (a picture, a formula, code
  colors) keeps the fades if their text did not move.
- A tag that starts its animation ends the fades, and its update does
  not fade, so its letters are not drawn twice.

Strikethrough and underline now cover only the glyphs that are drawn,
and their ends sit on whole pixels, so that a line drawn in two parts
covers the same pixels as one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen merged commit 1afd010 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