Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable popup windows on desktop #45

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ sealed class PlatformWebSettings {
data class DesktopWebSettings(
var offScreenRendering: Boolean = false,
var transparent: Boolean = false,
var disablePopupWindows: Boolean = false,
) : PlatformWebSettings()

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down