@@ -34,6 +34,12 @@ class WryWebViewPanel(
3434 private var pendingBounds: Bounds ? = null
3535 private var boundsTimer: Timer ? = null
3636
37+ private val handlers = mutableListOf< (String ) -> Boolean > ()
38+
39+ private val handler = object : NavigationHandler {
40+ override fun handleNavigation (url : String ): Boolean = handlers.any { it(url) }
41+ }
42+
3743 init {
3844 layout = BorderLayout ()
3945 add(host, BorderLayout .CENTER )
@@ -71,6 +77,14 @@ class WryWebViewPanel(
7177 scheduleCreateIfNeeded()
7278 }
7379
80+ fun addNavigateListener (data : (String ) -> Boolean ) {
81+ handlers.add(data)
82+ }
83+
84+ fun removeNavigateListener (data : (String ) -> Boolean ) {
85+ handlers.remove(data)
86+ }
87+
7488 fun loadUrl (url : String ) {
7589 loadUrl(url, emptyMap())
7690 }
@@ -332,9 +346,16 @@ class WryWebViewPanel(
332346 return try {
333347 webviewId =
334348 if (userAgent == null ) {
335- NativeBindings .createWebview(handleSnapshot, width, height, initialUrl)
349+ NativeBindings .createWebview(handleSnapshot, width, height, initialUrl, handler )
336350 } else {
337- NativeBindings .createWebviewWithUserAgent(handleSnapshot, width, height, initialUrl, userAgent)
351+ NativeBindings .createWebviewWithUserAgent(
352+ handleSnapshot,
353+ width,
354+ height,
355+ initialUrl,
356+ userAgent,
357+ handler
358+ )
338359 }
339360 updateBounds()
340361 startGtkPumpIfNeeded()
@@ -350,6 +371,7 @@ class WryWebViewPanel(
350371 pendingHtml = null
351372 NativeBindings .loadHtml(id, html)
352373 }
374+
353375 urlWithHeaders != null && headers.isNotEmpty() -> {
354376 pendingUrlWithHeaders = null
355377 pendingHeaders = emptyMap()
@@ -370,9 +392,16 @@ class WryWebViewPanel(
370392 thread(name = " wry-webview-create" , isDaemon = true ) {
371393 val createdId = try {
372394 if (userAgent == null ) {
373- NativeBindings .createWebview(handleSnapshot, width, height, initialUrl)
395+ NativeBindings .createWebview(handleSnapshot, width, height, initialUrl, handler )
374396 } else {
375- NativeBindings .createWebviewWithUserAgent(handleSnapshot, width, height, initialUrl, userAgent)
397+ NativeBindings .createWebviewWithUserAgent(
398+ handleSnapshot,
399+ width,
400+ height,
401+ initialUrl,
402+ userAgent,
403+ handler
404+ )
376405 }
377406 } catch (e: RuntimeException ) {
378407 System .err.println (" Failed to create Wry webview: ${e.message} " )
@@ -406,11 +435,13 @@ class WryWebViewPanel(
406435 pendingHtml = null
407436 NativeBindings .loadHtml(createdId, html)
408437 }
438+
409439 urlWithHeaders != null && headers.isNotEmpty() -> {
410440 pendingUrlWithHeaders = null
411441 pendingHeaders = emptyMap()
412442 NativeBindings .loadUrlWithHeaders(createdId, urlWithHeaders, headers)
413443 }
444+
414445 pendingUrl != initialUrl -> {
415446 NativeBindings .loadUrl(createdId, pendingUrl)
416447 }
@@ -557,7 +588,13 @@ class WryWebViewPanel(
557588 }
558589 } else if (IS_MAC ) {
559590 if (contentHandle != 0L && contentHandle != windowHandle) {
560- log(" resolveParentHandle skiko content=0x${contentHandle.toString(16 )} window=0x${windowHandle.toString(16 )} (macOS content)" )
591+ log(
592+ " resolveParentHandle skiko content=0x${contentHandle.toString(16 )} window=0x${
593+ windowHandle.toString(
594+ 16
595+ )
596+ } (macOS content)"
597+ )
561598 return ParentHandle (contentHandle.toULong(), false )
562599 }
563600 if (windowHandle != 0L ) {
@@ -570,7 +607,13 @@ class WryWebViewPanel(
570607 }
571608 } else {
572609 if (contentHandle != 0L ) {
573- log(" resolveParentHandle skiko content=0x${contentHandle.toString(16 )} window=0x${windowHandle.toString(16 )} " )
610+ log(
611+ " resolveParentHandle skiko content=0x${contentHandle.toString(16 )} window=0x${
612+ windowHandle.toString(
613+ 16
614+ )
615+ } "
616+ )
574617 return ParentHandle (contentHandle.toULong(), false )
575618 }
576619 if (windowHandle != 0L ) {
@@ -642,8 +685,8 @@ class WryWebViewPanel(
642685}
643686
644687private object NativeBindings {
645- fun createWebview (parentHandle : ULong , width : Int , height : Int , url : String ): ULong {
646- return io.github.kdroidfilter.webview.wry.createWebview(parentHandle, width, height, url)
688+ fun createWebview (parentHandle : ULong , width : Int , height : Int , url : String , handler : NavigationHandler ): ULong {
689+ return io.github.kdroidfilter.webview.wry.createWebview(parentHandle, width, height, url, handler )
647690 }
648691
649692 fun createWebviewWithUserAgent (
@@ -652,13 +695,15 @@ private object NativeBindings {
652695 height : Int ,
653696 url : String ,
654697 userAgent : String ,
698+ handler : NavigationHandler ,
655699 ): ULong {
656700 return io.github.kdroidfilter.webview.wry.createWebviewWithUserAgent(
657701 parentHandle,
658702 width,
659703 height,
660704 url,
661705 userAgent,
706+ handler,
662707 )
663708 }
664709
0 commit comments