Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Demo/TranscriptDemo/DemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ enum Sample {

/// Launch with `-replacementCheck` to replace the text of a card every two
/// seconds, with a short text and a long one in turn. Add `-crossfade` to lay
/// out each new text at once and crossfade to it. Add `-animatedSwap` to
/// swap inside `withAnimation`, so that the card's height animates. Each swap
/// out each new text at once. Add `-animatedSwap` to swap inside
/// `withAnimation`, so that the card's height animates and, with
/// `-crossfade`, the text crossfades. Each swap
/// logs when it happened, and each change of the card's height logs when
/// SwiftUI saw it.
struct ReplacementCheckView: View {
Expand All @@ -233,7 +234,8 @@ struct ReplacementCheckView: View {
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Text(crossfades ? "Crossfade, laid out at once" : "Laid out in the background")
Text(crossfades ? (animatesSwap ? "Crossfade, laid out at once" : "Laid out at once")
: "Laid out in the background")
.font(.footnote.monospaced())
.padding(.bottom, 8)
TranscriptText(MarkdownStreamParser.document(from: showsLong ? Self.long : Self.short))
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,19 @@ another, for example a summary for its details, can lay out the new text at once
```swift
TranscriptText(showsSummary ? summary : details)
.crossfadesReplacedText(duration: 0.25)

withAnimation { showsSummary.toggle() }
```

The view then lays out the new document on the main thread in the same SwiftUI update, so SwiftUI gets the new
height at once. The old drawing crossfades to the new one, as a `UITextView` does in a `.transitionCrossDissolve`
transition. While the crossfade plays, the drawing is clipped to the view's bounds and the space that tags' shapes
may draw in, so the old text does not show below a view that got shorter. A duration of zero shows the new text at
once. A document that only adds text to the one before (the same blocks with more text, or more blocks) still goes
to the background, and so do the updates of a stream. The crossfade has no motion, so it also plays with Reduce
Motion. In UIKit, set `replacementCrossfadeDuration` on `TranscriptTextView`.
height at once. In an update with an animation, the old drawing crossfades to the new one, as a `UITextView` does in
a `.transitionCrossDissolve` transition. In an update without an animation, the new text shows at once, as a `Text`
does, so a text that a binding replaces while the person types does not fade. While the crossfade plays, the drawing
is clipped to the view's bounds and the space that tags' shapes may draw in, so the old text does not show below a
view that got shorter. A duration of zero shows the new text at once. A document that only adds text to the one
before (the same blocks with more text, or more blocks) still goes to the background, and so do the updates of a
stream. The crossfade has no motion, so it also plays with Reduce Motion. In UIKit, set
`replacementCrossfadeDuration` on `TranscriptTextView`.

Block ids decide what adds to the text. A summary that is the start of its details, parsed with the same id prefix,
gives the details the ids of the summary's blocks, so the switch to the details only adds text: it goes to the
Expand Down
20 changes: 13 additions & 7 deletions Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,21 @@ public struct TranscriptText: UIViewRepresentable {
}

/// Lays out a document that replaces the one before at once, so that
/// SwiftUI gets its new height in the same update, and crossfades from
/// SwiftUI gets its new height in the same update. In an update with an
/// animation, for example in `withAnimation`, the view crossfades from
/// the old text to the new one; see
/// `TranscriptTextView.replacementCrossfadeDuration`. A document that
/// only adds text to the one before is laid out in the background, as
/// before. That includes a text that starts with the old one, parsed
/// with the same `idPrefix`, and the first text after an empty
/// document. Off by default.
/// `TranscriptTextView.replacementCrossfadeDuration`. In an update
/// without an animation, the new text shows at once, as a `Text` does.
/// A document that only adds text to the one before is laid out in the
/// background, as before. That includes a text that starts with the old
/// one, parsed with the same `idPrefix`, and the first text after an
/// empty document. Off by default.
///
/// ```swift
/// TranscriptText(showsSummary ? summary : details)
/// .crossfadesReplacedText(duration: 0.25)
///
/// withAnimation { showsSummary.toggle() }
/// ```
public func crossfadesReplacedText(_ crossfades: Bool = true, duration: TimeInterval = 0.25) -> TranscriptText {
var copy = self
Expand Down Expand Up @@ -234,7 +238,9 @@ public struct TranscriptText: UIViewRepresentable {
view.animatesTags = animatesTags
view.animatesTagsInFirstText = animatesFirstText
view.textFadeInDuration = textFadeInDuration
view.replacementCrossfadeDuration = replacementCrossfadeDuration
// Zero still lays out a replaced document at once, so SwiftUI gets the new height in this update.
let animates = context.transaction.animation != nil && !context.transaction.disablesAnimations
view.replacementCrossfadeDuration = replacementCrossfadeDuration.map { animates ? $0 : 0 }
view.tagAnimationTiming = animationTiming
view.isSelectable = isSelectable
view.showsWritingTools = showsWritingTools
Expand Down
76 changes: 76 additions & 0 deletions Tests/TranscriptKitViewTests/TranscriptTextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ private func isAsTallAsItsText(_ view: TranscriptTextView) -> Bool {
view.bounds.height == fittingHeight(of: view)
}

/// Whether a crossfade plays in `view` or in a view inside it.
@MainActor
private func isCrossfading(_ view: UIView) -> Bool {
view.layer.animation(forKey: kCATransition) != nil || view.subviews.contains { isCrossfading($0) }
}

private let shortText = MarkdownStreamParser.document(from: "A short text.", idPrefix: "short")
private let longText = MarkdownStreamParser.document(from: longParagraph, idPrefix: "long")

@MainActor @Observable
private final class Swap {
var showsLong = false
var showsWritingTools = true
}

/// A text that crossfades when `swap` replaces it.
private struct SwapView: View {
let swap: Swap

var body: some View {
VStack {
TranscriptText(swap.showsLong ? longText : shortText)
.crossfadesReplacedText(duration: 0.25)
.showsWritingTools(swap.showsWritingTools)
Spacer()
}
}
}

@Suite(.serialized, .timeLimit(.minutes(1))) @MainActor
struct TranscriptTextTests {
/// A window of a fixed size, so that no test depends on the screen. Each
Expand Down Expand Up @@ -112,5 +141,52 @@ struct TranscriptTextTests {
#expect(view.bounds.height == ceil(cut.height))
#expect(cut.height < DocumentLayout(document: document, width: view.bounds.width).height)
}

/// Shows the short text of `swap`, and returns the root and the text view once the text is on screen.
private func showSwap(_ swap: Swap) async throws -> (UIView, TranscriptTextView) {
let root = show(SwapView(swap: swap))
await waitUntil(root) { placedTextViews(in: root).count == 1 }
let view = try #require(placedTextViews(in: root).first)
await view.contentSettled()
return (root, view)
}

@Test func aReplacementWithoutAnimationShowsAtOnce() async throws {
let swap = Swap()
let (root, view) = try await showSwap(swap)
let before = fittingHeight(of: view)

swap.showsLong = true
root.layoutIfNeeded()
try #require(view.document == longText)
#expect(fittingHeight(of: view) > before)
#expect(!isCrossfading(view))
}

@Test func aReplacementInAnAnimationCrossfades() async throws {
let swap = Swap()
let (root, view) = try await showSwap(swap)
let before = fittingHeight(of: view)

withAnimation(.easeInOut(duration: 0.25)) { swap.showsLong = true }
root.layoutIfNeeded()
try #require(view.document == longText)
#expect(fittingHeight(of: view) > before)
#expect(isCrossfading(view))
}

@Test func anUpdateWithoutAnimationLetsACrossfadeEnd() async throws {
let swap = Swap()
let (root, view) = try await showSwap(swap)

withAnimation(.easeInOut(duration: 0.25)) { swap.showsLong = true }
root.layoutIfNeeded()
try #require(isCrossfading(view))

swap.showsWritingTools = false
root.layoutIfNeeded()
try #require(!view.showsWritingTools)
#expect(isCrossfading(view))
}
}
#endif
Loading