desktopMain.com.multiplatform.webview.web.WebEngineExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compose-webview-multiplatform-desktop Show documentation
Show all versions of compose-webview-multiplatform-desktop Show documentation
WebView for JetBrains Compose Multiplatform
package com.multiplatform.webview.web
import com.multiplatform.webview.util.KLogger
import org.cef.CefSettings
import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.handler.CefDisplayHandler
import org.cef.handler.CefLoadHandler
import org.cef.network.CefRequest
import kotlin.math.abs
import kotlin.math.ln
/**
* Created By Kevin Zou On 2023/9/12
*/
internal fun CefBrowser.getCurrentUrl(): String? {
return this.url
}
internal fun CefBrowser.addDisplayHandler(state: WebViewState) {
this.client.addDisplayHandler(
object : CefDisplayHandler {
override fun onAddressChange(
browser: CefBrowser?,
frame: CefFrame?,
url: String?,
) {
state.lastLoadedUrl = getCurrentUrl()
}
override fun onTitleChange(
browser: CefBrowser?,
title: String?,
) {
// https://magpcss.org/ceforum/viewtopic.php?t=11491
// https://github.com/KevinnZou/compose-webview-multiplatform/issues/46
val givenZoomLevel = state.webSettings.zoomLevel
val realZoomLevel =
if (givenZoomLevel >= 0.0) {
ln(abs(givenZoomLevel)) / ln(1.2)
} else {
-ln(abs(givenZoomLevel)) / ln(1.2)
}
KLogger.d { "titleProperty: $title $realZoomLevel" }
zoomLevel = realZoomLevel
state.pageTitle = title
}
override fun onTooltip(
browser: CefBrowser?,
text: String?,
): Boolean {
return false
}
override fun onStatusMessage(
browser: CefBrowser?,
value: String?,
) {
}
override fun onConsoleMessage(
browser: CefBrowser?,
level: CefSettings.LogSeverity?,
message: String?,
source: String?,
line: Int,
): Boolean {
return false
}
override fun onCursorChange(
browser: CefBrowser?,
cursorType: Int,
): Boolean {
return false
}
},
)
}
internal fun CefBrowser.addLoadListener(
state: WebViewState,
navigator: WebViewNavigator,
) {
this.client.addLoadHandler(
object : CefLoadHandler {
override fun onLoadingStateChange(
browser: CefBrowser?,
isLoading: Boolean,
canGoBack: Boolean,
canGoForward: Boolean,
) {
if (isLoading) {
state.loadingState = LoadingState.Initializing
} else {
state.loadingState = LoadingState.Finished
}
navigator.canGoBack = canGoBack
navigator.canGoForward = canGoForward
}
override fun onLoadStart(
browser: CefBrowser?,
frame: CefFrame?,
transitionType: CefRequest.TransitionType?,
) {
KLogger.d { "Load Start ${browser?.url}" }
state.loadingState = LoadingState.Loading(0F)
}
override fun onLoadEnd(
browser: CefBrowser?,
frame: CefFrame?,
httpStatusCode: Int,
) {
KLogger.d { "Load End ${browser?.url}" }
state.loadingState = LoadingState.Finished
navigator.canGoBack = canGoBack()
navigator.canGoBack = canGoForward()
state.lastLoadedUrl = getCurrentUrl()
}
override fun onLoadError(
browser: CefBrowser?,
frame: CefFrame?,
errorCode: CefLoadHandler.ErrorCode?,
errorText: String?,
failedUrl: String?,
) {
state.loadingState = LoadingState.Finished
KLogger.e {
"Failed to load url: ${failedUrl}\n$errorText"
}
state.errorsForCurrentRequest.add(
WebViewError(
code = errorCode?.code ?: 404,
description = "Failed to load url: ${failedUrl}\n$errorText",
),
)
}
},
)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy