Skip to content

Replace a document at once, with a crossfade - #41

Merged
servercimen merged 10 commits into
feature/right-to-leftfrom
feature/replace-transition
Nov 24, 2026
Merged

servercimen merged 10 commits into
feature/right-to-leftfrom
feature/replace-transition

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Part 11 of a stack of 13 pull requests. The base is #40 (feature/right-to-left). #31 (part 1) tells how to merge the stack.

On this stacked branch, swift test passes (379 tests), and the iOS build and build-for-testing pass.

Summary

An app that swaps one complete text for another, such as a summary for its details, needs the new height in the same SwiftUI update and a smooth change of text. Before this change, a new document always went to the layout queue. Until that layout was installed, sizeThatFits(_:) gave the old height, and the new text then appeared with no transition. The new option is off by default.

  • TranscriptTextView.replacementCrossfadeDuration (nil by default) and TranscriptText.crossfadesReplacedText(_:duration:) (0.25 s by default). With a duration, the document setter lays out a replaced document on the main thread at once. sizeThatFits(_:) and intrinsicContentSize then give the new height in the same update. Zero shows the new text at once.
  • A fade CATransition on the canvas layer crossfades from the old drawing to the new drawing. During the fade, the drawing is clipped to the view's bounds plus the space that tag shapes can draw in, so the old text does not show below a view that got shorter.
  • A new replacement, a zero duration, a removal from the window and a background install that is not an append stop a running crossfade. The view removes it by kCATransition, the key that Core Animation uses.
  • A document that only adds to the one before (TranscriptDocument.extends(_:)) keeps the background path, as stream updates do. The README tells apps to parse a summary and its details with different id prefixes.
  • A "Replace document" signpost interval marks each replacement on the main thread.
  • The demo app gets a check screen (-replacementCheck). The README has a new section, "Replacing the text".

Tests

  • swift test on macOS: 188 tests pass. The new ReplacementTests cover the choice of path, the new height, and the queue that must not lay out a replacement.
  • iOS build: xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS' (Xcode 27.2) succeeds with no warnings.

Checked on an iPhone 12 mini

A check screen in a local test app swapped a short text and a long text. The height, SwiftUI and picture checks ran on main first and showed the problem.

  • Height: main gives the new height right after the setter in 0 of 20 swaps. The branch does in 20 of 20 swaps, in 7 runs.
  • SwiftUI: on main, the height callback comes in a later run-loop turn in 12 of 12 swaps. On the branch, one callback comes in the same turn in 12 of 12 swaps, in 5 runs. With the option off, the branch behaves as main.
  • Picture: the old text crossfades to the new text. Inside withAnimation, the height moves smoothly, with no stretched glyphs. Tag shapes outside the view fade with the text and are not cut.
  • Main-thread cost: the setter takes 1.1 to 3.0 ms, and 2.9 to 7.7 ms with the tile drawing. On main, it takes 0.05 ms, and the layout comes later.

Round 1 found two problems on the branch. A zero-duration swap, a background install and a removal from the window did not stop a running crossfade, and the old text showed below a view that got shorter. Round 2, with the kCATransition key and the clip, showed no fade after these cases, and the clip only during a fade. The main run of round 2 stopped when the phone disconnected, so the main results come from round 1.

Not checked

  • The crossfade at the default 0.25 s, by eye. The checks used fades of 0.6 s, 4 s and 6 s, with screenshots at set times.
  • The crossfade with Reduce Motion on.

Known limits

  • The crossfade has no Reduce Motion gate. It also plays when Reduce Motion is on, because a crossfade has no motion. The text fade-in of this package is off with Reduce Motion. An app can set the duration to zero when UIAccessibility.isReduceMotionEnabled is true. The view then still lays out the new text at once, with no fade. A nil duration sends the text back to the background path.
  • The layout of a replaced document runs on the main thread, so its cost grows with the length of the text. The phone check measured only two short texts.

🤖 Generated with Claude Code

servercimen and others added 9 commits November 23, 2026 17:15
DocumentUpdate decides how a view takes a document that the app sets.
A document that does not add to the one before goes to the main thread
when the app asks for it. An append, and the first text after an empty
document, stay on the background path.

LayoutEngine.submit takes schedulesLayout. When it is false, the queue
does not lay out the request, because the main thread calls relayout at
once. This prevents a second layout of the same document.

Tests cover the choice, the height of the new layout, and an append
after a replacement. An opt-in test times the main-thread call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptTextView gets replacementCrossfadeDuration, and TranscriptText
gets crossfadesReplacedText(_:duration:). With a duration, the view lays
out a replaced document on the main thread in the document setter. Then
sizeThatFits gives the new height in the same SwiftUI update. A fade
CATransition on the canvas crossfades from the old drawing to the new
one. Appends and streams keep the background path. The default is nil,
so the behavior does not change for current apps.

A signpost in Points of Interest marks each main-thread replacement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Launch the demo with -replacementCheck to swap a short text and a long
text in a card every two seconds. Add -crossfade to turn on the new
option. The console shows each swap and each height change of the card,
with the time from the swap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The view adds the fade transition to its layer itself, so it does not
depend on UIView.areAnimationsEnabled. The crossfade then also plays in
a SwiftUI update that turns off UIKit animations, if one does. An app
that wants no crossfade sets a duration of zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add -animatedSwap to -replacementCheck to swap the text inside
withAnimation. The card's height then animates, and a screenshot during
the animation shows whether the red bar under the card moves with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A drain that was already scheduled took a request that the main thread
lays out. The main thread then waited for that layout and laid out the
same document again. The engine now marks such a request, and a drain
leaves it for relayout.

A replacement with no crossfade, and a background install that is not
an append, now remove a crossfade that still runs.

The README and the doc comments now say that a text that starts with
the old one, with the same block ids, and the first text after an empty
document only add text. They do not crossfade.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Core Animation keeps every CATransition under the key kCATransition,
whatever key the add call gets. The view added the fade under its own
key, so the remove calls did not find it. A zero-duration replacement,
a background install that is not an append, and a replacement out of
the window then left the old fade on the canvas. The text from before
the first replacement showed over the new text until that fade ended.

The view now adds and removes the fade with kCATransition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The crossfade shows the old drawing until it ends. When the new text is
shorter, the view gets its new height at once, but the old text showed
below the view, over the content that follows it. When the view grows
in an animation, the new text showed below the view too.

The canvas is now in a container view. While a crossfade plays, the
container clips to the bounds, widened by the space that tags' shapes
may draw in. The clip ends with the crossfade, or when the view stops
the crossfade or leaves the window. Outside a crossfade, nothing is
clipped, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptTextView: the file keeps both the side scroll logger and the replacement signposter.
TranscriptTextView.didMoveToWindow: the view does the side scroll and tag steps, and then stops the crossfade.
TranscriptTextView: the file keeps both helper classes, SideScrollFrameTarget and CrossfadeEnd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit d4289d2 into main Nov 24, 2026
0 of 3 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