diff --git a/Sources/TranscriptKit/Layout/LayoutEngine.swift b/Sources/TranscriptKit/Layout/LayoutEngine.swift index 937c13e..9071a53 100644 --- a/Sources/TranscriptKit/Layout/LayoutEngine.swift +++ b/Sources/TranscriptKit/Layout/LayoutEngine.swift @@ -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 { diff --git a/Sources/TranscriptKit/iOS/TranscriptText.swift b/Sources/TranscriptKit/iOS/TranscriptText.swift index 6157488..83ccba0 100644 --- a/Sources/TranscriptKit/iOS/TranscriptText.swift +++ b/Sources/TranscriptKit/iOS/TranscriptText.swift @@ -27,6 +27,10 @@ public struct TranscriptText: UIViewRepresentable { private var playTrigger: AnyHashable? private var selectionChange: ((Range?) -> 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) @@ -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? { diff --git a/Sources/TranscriptKit/iOS/TranscriptTextView.swift b/Sources/TranscriptKit/iOS/TranscriptTextView.swift index f66eca3..45c857d 100644 --- a/Sources/TranscriptKit/iOS/TranscriptTextView.swift +++ b/Sources/TranscriptKit/iOS/TranscriptTextView.swift @@ -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 @@ -935,6 +942,7 @@ 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() @@ -942,13 +950,29 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD 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 {