SPB Git

spb/zyquo-atlas Public License

The AI-native macOS web browser — every surface, intelligent.

Swift 75.2% JavaScript 22% Shell 2% Makefile 0.9%
1.8 KB · 53 lines swift
Raw Blame History
1//2//  WebView.swift3//  Zyquo Atlas4//5//  Author: Simon-Pierre Boucher6//  Mail: contact@spboucher.ai7//8//  SwiftUI host for a Tab's WKWebView. Kept intentionally thin: navigation9//  state, delegates, and KVO live on the Tab; this only mounts the existing10//  web view into the view tree (WebKit's own out-of-process rendering keeps the11//  page off the main thread). Swapping the active tab swaps which web view is12//  hosted, so background tabs keep their live web views.13//1415import SwiftUI16import WebKit1718struct WebView: NSViewRepresentable {19    let tab: Tab2021    func makeNSView(context: Context) -> ContainerView {22        let container = ContainerView()23        container.mount(tab.webView)24        return container25    }2627    func updateNSView(_ container: ContainerView, context: Context) {28        // Re-mount only when the hosted web view actually changed (tab switch).29        if container.hostedWebView !== tab.webView {30            container.mount(tab.webView)31        }32    }3334    /// A plain container that pins a single WKWebView to its bounds.35    final class ContainerView: NSView {36        private(set) weak var hostedWebView: WKWebView?3738        func mount(_ webView: WKWebView) {39            guard hostedWebView !== webView else { return }40            hostedWebView?.removeFromSuperview()41            webView.translatesAutoresizingMaskIntoConstraints = false42            addSubview(webView)43            NSLayoutConstraint.activate([44                webView.topAnchor.constraint(equalTo: topAnchor),45                webView.bottomAnchor.constraint(equalTo: bottomAnchor),46                webView.leadingAnchor.constraint(equalTo: leadingAnchor),47                webView.trailingAnchor.constraint(equalTo: trailingAnchor),48            ])49            hostedWebView = webView50        }51    }52}53