Files
Kymo/kymo/UI/Components/NativeContentHost.swift
T

33 lines
1.2 KiB
Swift

import AppKit
import SwiftUI
/// Keeps local animation rendering inside a fixed-size child host.
struct NativeContentHost<Content: View>: NSViewRepresentable {
let content: Content
func makeNSView(context: Context) -> NSHostingView<AnyView> {
let host = NSHostingView(rootView: root(context))
// The parent owns the card frame, including its responsive column width.
host.sizingOptions = []
return host
}
func updateNSView(_ host: NSHostingView<AnyView>, context: Context) {
host.rootView = root(context)
}
func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSHostingView<AnyView>, context: Context) -> CGSize? {
CGSize(width: proposal.width.flatMap { $0.isFinite ? max(0, $0) : nil } ?? 10,
height: proposal.height.flatMap { $0.isFinite ? max(0, $0) : nil } ?? 10)
}
static func dismantleNSView(_ host: NSHostingView<AnyView>, coordinator: ()) {
// AppKit may cache a detached view. Release its mounted content immediately.
host.rootView = AnyView(EmptyView())
}
private func root(_ context: Context) -> AnyView {
AnyView(content.environment(\.self, context.environment))
}
}