|
1 | 1 | import AppKit |
2 | 2 | import SwiftCrossUI |
| 3 | +import WebKit |
3 | 4 |
|
4 | 5 | extension App { |
5 | 6 | public typealias Backend = AppKitBackend |
@@ -1481,6 +1482,27 @@ public final class AppKitBackend: AppBackend { |
1481 | 1482 |
|
1482 | 1483 | widget.needsDisplay = true |
1483 | 1484 | } |
| 1485 | + |
| 1486 | + public func createWebView() -> Widget { |
| 1487 | + let webView = CustomWKWebView() |
| 1488 | + webView.navigationDelegate = webView.strongNavigationDelegate |
| 1489 | + return webView |
| 1490 | + } |
| 1491 | + |
| 1492 | + public func updateWebView( |
| 1493 | + _ webView: Widget, |
| 1494 | + environment: EnvironmentValues, |
| 1495 | + onNavigate: @escaping (URL) -> Void |
| 1496 | + ) { |
| 1497 | + let webView = webView as! CustomWKWebView |
| 1498 | + webView.strongNavigationDelegate.onNavigate = onNavigate |
| 1499 | + } |
| 1500 | + |
| 1501 | + public func navigateWebView(_ webView: Widget, to url: URL) { |
| 1502 | + let webView = webView as! CustomWKWebView |
| 1503 | + let request = URLRequest(url: url) |
| 1504 | + webView.load(request) |
| 1505 | + } |
1484 | 1506 | } |
1485 | 1507 |
|
1486 | 1508 | final class NSCustomTapGestureTarget: NSView { |
@@ -1908,3 +1930,20 @@ final class NSDisabledScrollView: NSScrollView { |
1908 | 1930 | self.nextResponder?.scrollWheel(with: event) |
1909 | 1931 | } |
1910 | 1932 | } |
| 1933 | + |
| 1934 | +final class CustomWKWebView: WKWebView { |
| 1935 | + var strongNavigationDelegate = CustomWKNavigationDelegate() |
| 1936 | +} |
| 1937 | + |
| 1938 | +final class CustomWKNavigationDelegate: NSObject, WKNavigationDelegate { |
| 1939 | + var onNavigate: ((URL) -> Void)? |
| 1940 | + |
| 1941 | + func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { |
| 1942 | + guard let url = webView.url else { |
| 1943 | + print("warning: Web view has no URL") |
| 1944 | + return |
| 1945 | + } |
| 1946 | + |
| 1947 | + onNavigate?(url) |
| 1948 | + } |
| 1949 | +} |
0 commit comments