From 3ca3aefe4ec9537c14b7b12911485afd2ec0ec5b Mon Sep 17 00:00:00 2001 From: DatLag Date: Thu, 16 Nov 2023 19:53:51 +0100 Subject: [PATCH 1/2] disable popup windows on desktop --- .../webview/setting/PlatformWebSettings.kt | 1 + .../web/DisablePopupWindowsLifeSpanHandler.kt | 19 +++++++++++++++++++ .../webview/web/WebView.desktop.kt | 19 +++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DisablePopupWindowsLifeSpanHandler.kt 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..056a9f6d 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..de921861 --- /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 + } +} \ No newline at end of file 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..8e9f8dcd 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,17 @@ 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 +70,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 From e8cc031cf1a7cddce294bc9aff5b0083c699e15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E9=93=AD=E6=9D=B0?= Date: Thu, 23 Nov 2023 09:37:07 +0800 Subject: [PATCH 2/2] feat:Ktlint format the code --- .../webview/setting/PlatformWebSettings.kt | 2 +- .../web/DisablePopupWindowsLifeSpanHandler.kt | 4 ++-- .../webview/web/WebView.desktop.kt | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) 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 056a9f6d..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,7 +178,7 @@ sealed class PlatformWebSettings { data class DesktopWebSettings( var offScreenRendering: Boolean = false, var transparent: Boolean = false, - var disablePopupWindows: 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 index de921861..7639100c 100644 --- a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DisablePopupWindowsLifeSpanHandler.kt +++ b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DisablePopupWindowsLifeSpanHandler.kt @@ -9,11 +9,11 @@ class DisablePopupWindowsLifeSpanHandler : CefLifeSpanHandlerAdapter() { browser: CefBrowser?, frame: CefFrame?, target_url: String?, - target_frame_name: String? + target_frame_name: String?, ): Boolean { if (target_url != null) { browser?.loadURL(target_url) } return true } -} \ No newline at end of file +} 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 8e9f8dcd..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,17 +48,18 @@ fun DesktopWebView( onDispose: () -> Unit, ) { val currentOnDispose by rememberUpdatedState(onDispose) - 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 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) { @@ -74,7 +75,7 @@ fun DesktopWebView( client, state.webSettings.desktopWebSettings.offScreenRendering, state.webSettings.desktopWebSettings.transparent, - fileContent + fileContent, ) { val rendering = if (state.webSettings.desktopWebSettings.offScreenRendering) {