// // WebView.swift // Zyquo Atlas // // Author: Simon-Pierre Boucher // Mail: contact@spboucher.ai // // SwiftUI host for a Tab's WKWebView. Kept intentionally thin: navigation // state, delegates, and KVO live on the Tab; this only mounts the existing // web view into the view tree (WebKit's own out-of-process rendering keeps the // page off the main thread). Swapping the active tab swaps which web view is // hosted, so background tabs keep their live web views. // import SwiftUI import WebKit struct WebView: NSViewRepresentable { let tab: Tab func makeNSView(context: Context) -> ContainerView { let container = ContainerView() container.mount(tab.webView) return container } func updateNSView(_ container: ContainerView, context: Context) { // Re-mount only when the hosted web view actually changed (tab switch). if container.hostedWebView !== tab.webView { container.mount(tab.webView) } } /// A plain container that pins a single WKWebView to its bounds. final class ContainerView: NSView { private(set) weak var hostedWebView: WKWebView? func mount(_ webView: WKWebView) { guard hostedWebView !== webView else { return } hostedWebView?.removeFromSuperview() webView.translatesAutoresizingMaskIntoConstraints = false addSubview(webView) NSLayoutConstraint.activate([ webView.topAnchor.constraint(equalTo: topAnchor), webView.bottomAnchor.constraint(equalTo: bottomAnchor), webView.leadingAnchor.constraint(equalTo: leadingAnchor), webView.trailingAnchor.constraint(equalTo: trailingAnchor), ]) hostedWebView = webView } } }