diff --git a/webview/src/commonMain/kotlin/com/multiplatform/webview/setting/PlatformWebSettings.kt b/webview/src/commonMain/kotlin/com/multiplatform/webview/setting/PlatformWebSettings.kt index 83cd3b3e..587121e2 100644 --- a/webview/src/commonMain/kotlin/com/multiplatform/webview/setting/PlatformWebSettings.kt +++ b/webview/src/commonMain/kotlin/com/multiplatform/webview/setting/PlatformWebSettings.kt @@ -178,6 +178,7 @@ sealed class PlatformWebSettings { data class DesktopWebSettings( var offScreenRendering: Boolean = false, var transparent: Boolean = false, + var disablePopupWindows: Boolean = false, ) : PlatformWebSettings() /** diff --git a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DisablePopupWindowsLifeSpanHandler.kt b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DisablePopupWindowsLifeSpanHandler.kt new file mode 100644 index 00000000..7639100c --- /dev/null +++ b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DisablePopupWindowsLifeSpanHandler.kt @@ -0,0 +1,19 @@ +package com.multiplatform.webview.web + +import org.cef.browser.CefBrowser +import org.cef.browser.CefFrame +import org.cef.handler.CefLifeSpanHandlerAdapter + +class DisablePopupWindowsLifeSpanHandler : CefLifeSpanHandlerAdapter() { + override fun onBeforePopup( + browser: CefBrowser?, + frame: CefFrame?, + target_url: String?, + target_frame_name: String?, + ): Boolean { + if (target_url != null) { + browser?.loadURL(target_url) + } + return true + } +} diff --git a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt index c67a983f..fc58a606 100644 --- a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt +++ b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt @@ -48,7 +48,18 @@ fun DesktopWebView( onDispose: () -> Unit, ) { val currentOnDispose by rememberUpdatedState(onDispose) - val client = remember { KCEF.newClientOrNullBlocking() } + val client = + remember(state.webSettings.desktopWebSettings.disablePopupWindows) { + KCEF.newClientOrNullBlocking()?.also { + if (state.webSettings.desktopWebSettings.disablePopupWindows) { + it.addLifeSpanHandler(DisablePopupWindowsLifeSpanHandler()) + } else { + if (it.getLifeSpanHandler() is DisablePopupWindowsLifeSpanHandler) { + it.removeLifeSpanHandler() + } + } + } + } val fileContent by produceState("", state.content) { value = if (state.content is WebContent.File) { @@ -60,7 +71,12 @@ fun DesktopWebView( } val browser: KCEFBrowser? = - remember(client, state.webSettings.desktopWebSettings, fileContent) { + remember( + client, + state.webSettings.desktopWebSettings.offScreenRendering, + state.webSettings.desktopWebSettings.transparent, + fileContent, + ) { val rendering = if (state.webSettings.desktopWebSettings.offScreenRendering) { CefRendering.OFFSCREEN