diff --git a/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt b/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt index 8654e304..fd9fe029 100644 --- a/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt +++ b/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt @@ -83,9 +83,6 @@ fun WebView( webView?.let { wv -> LaunchedEffect(wv, navigator) { with(navigator) { - KLogger.d { - "wv.handleNavigationEvents()" - } wv.handleNavigationEvents() } } @@ -93,37 +90,7 @@ fun WebView( // Handle content loading for all platforms LaunchedEffect(wv, state) { snapshotFlow { state.content }.collect { content -> - when (content) { - is WebContent.Url -> { - state.lastLoadedUrl = content.url - wv.loadUrl(content.url, content.additionalHttpHeaders) - } - - is WebContent.Data -> { - wv.loadHtml( - content.data, - content.baseUrl, - content.mimeType, - content.encoding, - content.historyUrl, - ) - } - - is WebContent.File -> { - wv.loadHtmlFile(content.fileName, content.readType) - } - - is WebContent.Post -> { - wv.postUrl( - content.url, - content.postData, - ) - } - - is WebContent.NavigatorOnly -> { - // NO-OP - } - } + wv.loadContent(content) } } @@ -160,9 +127,6 @@ fun WebView( DisposableEffect(Unit) { onDispose { - KLogger.d { - "WebView DisposableEffect" - } webViewJsBridge?.clear() } } diff --git a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DesktopWebView.kt b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DesktopWebView.kt index be037af6..17f0ead9 100644 --- a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DesktopWebView.kt +++ b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DesktopWebView.kt @@ -75,6 +75,7 @@ class DesktopWebView( webView.loadHtml(html, baseUrl ?: KCEFBrowser.BLANK_URI) } catch (e: Exception) { KLogger.e { "DesktopWebView loadHtml error: ${e.message}" } + e.printStackTrace() } } else { KLogger.e { "DesktopWebView loadHtml: HTML content is null" } @@ -127,7 +128,7 @@ class DesktopWebView( } } - delay(200) + delay(500) webView.loadURL("file://${outFile.absolutePath}") } @@ -150,19 +151,19 @@ class DesktopWebView( for (entry in jarFile.entries()) { if (entry.name.startsWith(pathInJar.substringBeforeLast("/")) && !entry.isDirectory) { val file = - java.io.File(tempDirectory, entry.name.substringAfterLast("/")) + File(tempDirectory, entry.name.substringAfterLast("/")) file.outputStream().use { output -> jarFile.getInputStream(entry).copyTo(output) } } } - val htmlFile = java.io.File(tempDirectory, pathInJar.substringAfterLast("/")) + val htmlFile = File(tempDirectory, pathInJar.substringAfterLast("/")) if (!htmlFile.exists()) { throw Exception("Extracted HTML file not found: ${htmlFile.absolutePath}") } - delay(200) + delay(500) webView.loadURL("file://${htmlFile.absolutePath}") } } 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 18b43e68..01d4d1a7 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 @@ -2,6 +2,7 @@ package com.multiplatform.webview.web import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -78,11 +79,11 @@ actual fun defaultWebViewFactory(param: WebViewFactoryParam): NativeWebView = param.transparent, ) is WebContent.File -> { - param.client.createBrowserWithHtml( - param.fileContent, + param.client.createBrowser( KCEFBrowser.BLANK_URI, param.rendering, param.transparent, + param.requestContext, ) } else -> @@ -123,21 +124,28 @@ fun DesktopWebView( val scope = rememberCoroutineScope() val browser: KCEFBrowser? = - remember(client, state.webSettings) { + remember(client, state.webSettings, state.content) { client?.let { factory(WebViewFactoryParam(state, client, "")) } } val desktopWebView: DesktopWebView? = - remember(browser) { - browser?.let { DesktopWebView(browser, scope, webViewJsBridge) } + remember(browser, state.content) { + browser?.let { + DesktopWebView(browser, scope, webViewJsBridge) + } } + LaunchedEffect(desktopWebView) { + desktopWebView?.let { webView -> + state.webView = webView + webViewJsBridge?.webView = webView + } + } + browser?.let { SwingPanel( factory = { onCreated(it) - state.webView = desktopWebView - webViewJsBridge?.webView = desktopWebView browser.apply { addDisplayHandler(state) addLoadListener(state, navigator)