Repository navigation
Add an option to fade in text as it streams in - #27
Merged
Merged
Conversation
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>
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.
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.textFadeInDurationandTranscriptText.fadesInText(_:duration:)turn it on. It is off by default. It is also off with Reduce Motion and with a line limit.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
swift test: 171 tests pass. The package builds for iOS.Cost on an iPhone 12 mini
A local benchmark app streams an answer into a
TranscriptText(stream:)row of aLazyVStack, with the fade off and on. The table gives the median of three runs.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