@@ -145,8 +145,9 @@ struct WebView: NSViewRepresentable {
145145 // Store a weak reference in Coordinator to break potential cycles
146146 context. coordinator. webView = webView
147147
148- // Set UI delegate
148+ // delegates
149149 webView. uiDelegate = context. coordinator
150+ webView. navigationDelegate = context. coordinator
150151
151152 // Make background transparent
152153 webView. setValue ( false , forKey: " drawsBackground " )
@@ -247,7 +248,7 @@ struct WebView: NSViewRepresentable {
247248 // you can do so. Currently, no updates are necessary.
248249 }
249250
250- class Coordinator : NSObject , WKScriptMessageHandler , WKUIDelegate {
251+ class Coordinator : NSObject , WKScriptMessageHandler , WKUIDelegate , WKNavigationDelegate {
251252 // Weak reference to avoid strong reference cycles
252253 weak var webView : WKWebView ?
253254
@@ -283,6 +284,15 @@ struct WebView: NSViewRepresentable {
283284
284285 func userContentController( _ userContentController: WKUserContentController ,
285286 didReceive message: WKScriptMessage ) { }
287+
288+ func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Void ) {
289+ if let url = navigationAction. request. url, navigationAction. navigationType == . linkActivated {
290+ NSWorkspace . shared. open ( url)
291+ decisionHandler ( . cancel)
292+ } else {
293+ decisionHandler ( . allow)
294+ }
295+ }
286296 }
287297}
288298
0 commit comments