// // PunchNetWebView.swift // punchnet // // Created by 安礼成 on 2026/3/20. // import SwiftUI import WebKit // MARK: - 1. 核心 WebView 包装器 (带进度监听) struct PunchNetWebView: NSViewRepresentable { let url: URL @Binding var progress: Double @Binding var isLoading: Bool func makeCoordinator() -> Coordinator { Coordinator(self) } func makeNSView(context: Context) -> WKWebView { let webView = WKWebView() webView.navigationDelegate = context.coordinator // 添加进度监听 (KVO) context.coordinator.observation = webView.observe(\.estimatedProgress, options: [.new]) { wv, _ in DispatchQueue.main.async { self.progress = wv.estimatedProgress } } let request = URLRequest(url: url) webView.load(request) return webView } func updateNSView(_ nsView: WKWebView, context: Context) { } // 协调器处理加载状态 class Coordinator: NSObject, WKNavigationDelegate { var parent: PunchNetWebView var observation: NSKeyValueObservation? init(_ parent: PunchNetWebView) { self.parent = parent } func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { DispatchQueue.main.async { self.parent.isLoading = true } } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { DispatchQueue.main.async { withAnimation(.easeInOut(duration: 0.5)) { self.parent.isLoading = false } } } } }