import AppKit import SwiftUI /// SwiftUI reports its natural height even after asynchronous content changes. /// The transcript keeps its row identity and shifts everything below it. @MainActor final class ResizingHostingView: NSHostingView> { init(content: Content, width: CGFloat, onHeightChange: @escaping (NSView, CGFloat) -> Void) { super.init(rootView: MeasuredContent(content: content, width: width, changed: { _ in })) rootView = MeasuredContent(content: content, width: width) { [weak self] height in Task { @MainActor [weak self] in await Task.yield() guard let self else { return } onHeightChange(self, ceil(height)) } } frame.size = NSSize(width: width, height: ceil(fittingSize.height)) } required init(rootView: MeasuredContent) { super.init(rootView: rootView) } required init?(coder: NSCoder) { fatalError() } } struct MeasuredContent: View { let content: Content let width: CGFloat let changed: (CGFloat) -> Void var body: some View { content.frame(width: width, alignment: .leading) .fixedSize(horizontal: false, vertical: true) .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { changed($0) } } }