compose-webview-multiplatform icon indicating copy to clipboard operation
compose-webview-multiplatform copied to clipboard

I am using KMP to develop a desktop application, and the webview is reloaded when the tab is switched

Open GoldTest opened this issue 11 months ago • 2 comments

How do I make the webview not dispose when I switch tabs? Kcef have those methods but it seems i cant control in the webview layer.

samples doesn't help

GoldTest avatar Dec 04 '24 10:12 GoldTest

fun rememberSaveableWebViewState( url: String, additionalHttpHeaders: Map<String, String> = emptyMap(), ): WebViewState = if (getPlatform().isDesktop()) { rememberWebViewState(url, additionalHttpHeaders) } else { rememberSaveable(saver = WebStateSaver) { WebViewState(WebContent.NavigatorOnly) } }

looks not support yet?

GoldTest avatar Dec 04 '24 11:12 GoldTest

You can cache WebViews, but there is a bug

1 You can add the following code cache // factory = { param -> // val nativeWebView = viewModel.buildWebView(param) // nativeWebView 2. When returning, his AndroidWebView is newly created every time, so it will reload LaunchedEffect(wv, state) { snapshotFlow { state.content }.collect { content -> when (content) { is WebContent.Url -> { state.lastLoadedUrl = content.url wv.loadUrl(content.url, content.additionalHttpHeaders) }

  1. code location AndroidView( factory = { context -> (factory?.invoke(context) ?: WebView(context)).apply { onCreated(this)

           this.layoutParams = layoutParams
    
           state.viewState?.let {
               this.restoreState(it)
           }
    
           chromeClient.context = context
           webChromeClient = chromeClient
           webViewClient = client
    
           // Avoid covering other components
           this.setLayerType(state.webSettings.androidWebSettings.layerType, null)
    
           settings.apply {
               state.webSettings.let {
                   javaScriptEnabled = it.isJavaScriptEnabled
                   userAgentString = it.customUserAgentString
                   allowFileAccessFromFileURLs = it.allowFileAccessFromFileURLs
                   allowUniversalAccessFromFileURLs = it.allowUniversalAccessFromFileURLs
                   setSupportZoom(it.supportZoom)
               }
    
               state.webSettings.androidWebSettings.let {
                   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                       safeBrowsingEnabled = it.safeBrowsingEnabled
                   }
                   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                       isAlgorithmicDarkeningAllowed = it.isAlgorithmicDarkeningAllowed
                   }
                   setBackgroundColor(state.webSettings.backgroundColor.toArgb())
                   allowFileAccess = it.allowFileAccess
                   textZoom = it.textZoom
                   useWideViewPort = it.useWideViewPort
                   standardFontFamily = it.standardFontFamily
                   defaultFontSize = it.defaultFontSize
                   loadsImagesAutomatically = it.loadsImagesAutomatically
                   domStorageEnabled = it.domStorageEnabled
                   mediaPlaybackRequiresUserGesture = it.mediaPlaybackRequiresUserGesture
               }
           }
           if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
               val nightModeFlags =
                   resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
               if (nightModeFlags == Configuration.UI_MODE_NIGHT_YES) {
                   WebSettingsCompat.setForceDark(
                       this.settings,
                       WebSettingsCompat.FORCE_DARK_ON,
                   )
               } else {
                   WebSettingsCompat.setForceDark(
                       this.settings,
                       WebSettingsCompat.FORCE_DARK_OFF,
                   )
               }
    
               if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
                   WebSettingsCompat.setForceDarkStrategy(
                       this.settings,
                       WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY,
                   )
               }
           }
       }.also {
           val androidWebView = AndroidWebView(it, scope, webViewJsBridge)
           state.webView = androidWebView
           webViewJsBridge?.webView = androidWebView
       }
    

    }, modifier = modifier, onReset = {}, onRelease = { onDispose(it) }, )

cgpllx avatar Feb 21 '25 07:02 cgpllx