Skip to content
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 @@ -83,47 +83,14 @@ fun WebView(
webView?.let { wv ->
LaunchedEffect(wv, navigator) {
with(navigator) {
KLogger.d {
"wv.handleNavigationEvents()"
}
wv.handleNavigationEvents()
}
}

// 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)
}
}

Expand Down Expand Up @@ -160,9 +127,6 @@ fun WebView(

DisposableEffect(Unit) {
onDispose {
KLogger.d {
"WebView DisposableEffect"
}
webViewJsBridge?.clear()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -127,7 +128,7 @@ class DesktopWebView(
}
}

delay(200)
delay(500)
webView.loadURL("file://${outFile.absolutePath}")
}

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