punchnet-macos/punchnet/Views/Privacy/PunchNetWebView.swift
2026-03-20 21:49:59 +08:00

64 lines
1.7 KiB
Swift

//
// 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
}
}
}
}
}