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
16 changes: 16 additions & 0 deletions Sources/TranscriptKit/Layout/LayoutEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ final class LayoutEngine: @unchecked Sendable {
}
}

/// Lays out the documents that arrive from now on at `width`, while the
/// view has no width of its own: SwiftUI measured it but has not placed
/// it, as a lazy stack does with a row of zero height. The width the view
/// gets when it is placed replaces this one.
func layOutUntilPlaced(width: CGFloat, style: TranscriptStyle) {
guard width > 0 else { return }
queue.async {
guard self.width == 0 else { return }
self.width = width
self.style = style
if let measured = self.measured, measured.width == width, measured.style == style {
self.base = measured
}
}
}

/// The height of the newest document at `width`, for sizing. The layout
/// is kept, so laying out at that width afterwards costs nothing.
func measure(width: CGFloat, style: TranscriptStyle) -> CGFloat {
Expand Down
9 changes: 9 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public struct TranscriptText: UIViewRepresentable {
private var playTrigger: AnyHashable?
private var selectionChange: ((Range<Int>?) -> Void)?
private var selectsAll = false
/// Changed when the view's height changes before SwiftUI has placed it,
/// so that SwiftUI measures the view again (see
/// `TranscriptTextView.onHeightChangeWhileUnsized`).
@State private var unsizedRevision = 0

public init(_ document: TranscriptDocument, style: TranscriptStyle = .init()) {
self.source = .document(document)
Expand Down Expand Up @@ -169,6 +173,11 @@ public struct TranscriptText: UIViewRepresentable {
view.openLink = { openURL($0) }
view.onSelectionChange = selectionChange
view.selectsAllWhenShown = selectsAll
// SwiftUI updates and measures the view again for a state change
// only if the update reads that state.
_ = unsizedRevision
let revision = $unsizedRevision
view.onHeightChangeWhileUnsized = { revision.wrappedValue &+= 1 }
}

public func sizeThatFits(_ proposal: ProposedViewSize, uiView: TranscriptTextView, context: Context) -> CGSize? {
Expand Down
26 changes: 25 additions & 1 deletion Sources/TranscriptKit/iOS/TranscriptTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,13 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD
engine.settleAnimations(skipped.started + skipped.waiting)
}
}
if bounds.width == 0, unsizedWidth > 0, output.source == source, output.layout.width == unsizedWidth,
output.layout.style == style {
// Not placed yet: nothing to show, but SwiftUI must hear that the height changed.
engine.settleAnimations(output.started + output.waiting)
if ceil(output.layout.height) != unsizedHeight { onHeightChangeWhileUnsized?() }
return
}
guard output.source == source, output.layout.width == bounds.width, output.layout.style == style else {
if output.source < source, output.layout.width == bounds.width, output.layout.style == style {
// From the source just replaced: kept in case the next
Expand Down Expand Up @@ -935,20 +942,37 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD
super.layoutSubviews()
canvas.frame = bounds
animationLayer.frame = canvas.bounds
if bounds.width > 0 { unsizedWidth = 0 }
if bounds.width > 0, installed?.width != bounds.width {
relayoutNow()
refreshSelectionDisplay()
}
updateCanvas()
}

/// Called when the text's height changes while the view has no width of
/// its own, measured at the width of the last `sizeThatFits`. A lazy
/// stack measures a row of zero height but does not place it, so the
/// view never lays out and `invalidateIntrinsicContentSize` reaches nobody;
/// `TranscriptText` asks SwiftUI to measure again instead.
var onHeightChangeWhileUnsized: (() -> Void)?
/// The width and height of the last `sizeThatFits` while the view had no width.
private var unsizedWidth: CGFloat = 0
private var unsizedHeight: CGFloat = -1

public override func sizeThatFits(_ size: CGSize) -> CGSize {
let width = size.width.isFinite && size.width > 0 ? size.width : max(bounds.width, 1)
if let installed, installed.width == width {
return CGSize(width: width, height: ceil(installed.height))
}
// Kept by the engine, so the relayout when the view takes this width is free.
return CGSize(width: width, height: ceil(engine.measure(width: width, style: style)))
let height = ceil(engine.measure(width: width, style: style))
if bounds.width == 0, onHeightChangeWhileUnsized != nil, size.width.isFinite, size.width > 0 {
unsizedWidth = width
unsizedHeight = height
engine.layOutUntilPlaced(width: width, style: style)
}
return CGSize(width: width, height: height)
}

public override var intrinsicContentSize: CGSize {
Expand Down
Loading