Skip to content

Commit c57db28

Browse files
committed
Refactor: Improve Desktop WebView loading and stability
This commit introduces several improvements to the desktop WebView implementation: - **Increased delay for file loading:** The delay before loading local HTML files (both from assets and Compose resources) has been increased from 200ms to 500ms. This provides more time for file extraction and setup, potentially improving reliability. - **Error handling for HTML loading:** Added `e.printStackTrace()` in the `catch` block of `DesktopWebView.loadHtml` for better debugging of HTML loading errors. - **Unified content loading:** Refactored `WebView.kt` to use a common `wv.loadContent(content)` call within the `LaunchedEffect` for all content types, simplifying the logic. - **Refined Desktop WebView initialization:** - The `KCEFBrowser` instance is now recreated if `state.content` changes, ensuring the browser is correctly initialized for different content types (e.g., when switching from URL to File). - `WebViewFactoryParam` for `WebContent.File` now passes `param.requestContext` to `createBrowser`. - `state.webView` and `webViewJsBridge?.webView` are now set within a `LaunchedEffect` dependent on `desktopWebView` to ensure they are assigned after the `DesktopWebView` instance is fully created. - **Removed verbose logging:** Removed some KLogger debug statements related to navigation events and disposal. - **Standardized File import:** Changed `java.io.File` to `File` for consistency.
1 parent 09e8e83 commit c57db28

File tree

3 files changed

+21
-48
lines changed

3 files changed

+21
-48
lines changed

webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,47 +83,14 @@ fun WebView(
8383
webView?.let { wv ->
8484
LaunchedEffect(wv, navigator) {
8585
with(navigator) {
86-
KLogger.d {
87-
"wv.handleNavigationEvents()"
88-
}
8986
wv.handleNavigationEvents()
9087
}
9188
}
9289

9390
// Handle content loading for all platforms
9491
LaunchedEffect(wv, state) {
9592
snapshotFlow { state.content }.collect { content ->
96-
when (content) {
97-
is WebContent.Url -> {
98-
state.lastLoadedUrl = content.url
99-
wv.loadUrl(content.url, content.additionalHttpHeaders)
100-
}
101-
102-
is WebContent.Data -> {
103-
wv.loadHtml(
104-
content.data,
105-
content.baseUrl,
106-
content.mimeType,
107-
content.encoding,
108-
content.historyUrl,
109-
)
110-
}
111-
112-
is WebContent.File -> {
113-
wv.loadHtmlFile(content.fileName, content.readType)
114-
}
115-
116-
is WebContent.Post -> {
117-
wv.postUrl(
118-
content.url,
119-
content.postData,
120-
)
121-
}
122-
123-
is WebContent.NavigatorOnly -> {
124-
// NO-OP
125-
}
126-
}
93+
wv.loadContent(content)
12794
}
12895
}
12996

@@ -160,9 +127,6 @@ fun WebView(
160127

161128
DisposableEffect(Unit) {
162129
onDispose {
163-
KLogger.d {
164-
"WebView DisposableEffect"
165-
}
166130
webViewJsBridge?.clear()
167131
}
168132
}

webview/src/desktopMain/kotlin/com/multiplatform/webview/web/DesktopWebView.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class DesktopWebView(
7575
webView.loadHtml(html, baseUrl ?: KCEFBrowser.BLANK_URI)
7676
} catch (e: Exception) {
7777
KLogger.e { "DesktopWebView loadHtml error: ${e.message}" }
78+
e.printStackTrace()
7879
}
7980
} else {
8081
KLogger.e { "DesktopWebView loadHtml: HTML content is null" }
@@ -127,7 +128,7 @@ class DesktopWebView(
127128
}
128129
}
129130

130-
delay(200)
131+
delay(500)
131132
webView.loadURL("file://${outFile.absolutePath}")
132133
}
133134

@@ -150,19 +151,19 @@ class DesktopWebView(
150151
for (entry in jarFile.entries()) {
151152
if (entry.name.startsWith(pathInJar.substringBeforeLast("/")) && !entry.isDirectory) {
152153
val file =
153-
java.io.File(tempDirectory, entry.name.substringAfterLast("/"))
154+
File(tempDirectory, entry.name.substringAfterLast("/"))
154155
file.outputStream().use { output ->
155156
jarFile.getInputStream(entry).copyTo(output)
156157
}
157158
}
158159
}
159160

160-
val htmlFile = java.io.File(tempDirectory, pathInJar.substringAfterLast("/"))
161+
val htmlFile = File(tempDirectory, pathInJar.substringAfterLast("/"))
161162
if (!htmlFile.exists()) {
162163
throw Exception("Extracted HTML file not found: ${htmlFile.absolutePath}")
163164
}
164165

165-
delay(200)
166+
delay(500)
166167
webView.loadURL("file://${htmlFile.absolutePath}")
167168
}
168169
}

webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.multiplatform.webview.web
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.DisposableEffect
5+
import androidx.compose.runtime.LaunchedEffect
56
import androidx.compose.runtime.getValue
67
import androidx.compose.runtime.remember
78
import androidx.compose.runtime.rememberCoroutineScope
@@ -78,11 +79,11 @@ actual fun defaultWebViewFactory(param: WebViewFactoryParam): NativeWebView =
7879
param.transparent,
7980
)
8081
is WebContent.File -> {
81-
param.client.createBrowserWithHtml(
82-
param.fileContent,
82+
param.client.createBrowser(
8383
KCEFBrowser.BLANK_URI,
8484
param.rendering,
8585
param.transparent,
86+
param.requestContext,
8687
)
8788
}
8889
else ->
@@ -123,21 +124,28 @@ fun DesktopWebView(
123124

124125
val scope = rememberCoroutineScope()
125126
val browser: KCEFBrowser? =
126-
remember(client, state.webSettings) {
127+
remember(client, state.webSettings, state.content) {
127128
client?.let { factory(WebViewFactoryParam(state, client, "")) }
128129
}
129130

130131
val desktopWebView: DesktopWebView? =
131-
remember(browser) {
132-
browser?.let { DesktopWebView(browser, scope, webViewJsBridge) }
132+
remember(browser, state.content) {
133+
browser?.let {
134+
DesktopWebView(browser, scope, webViewJsBridge)
135+
}
133136
}
134137

138+
LaunchedEffect(desktopWebView) {
139+
desktopWebView?.let { webView ->
140+
state.webView = webView
141+
webViewJsBridge?.webView = webView
142+
}
143+
}
144+
135145
browser?.let {
136146
SwingPanel(
137147
factory = {
138148
onCreated(it)
139-
state.webView = desktopWebView
140-
webViewJsBridge?.webView = desktopWebView
141149
browser.apply {
142150
addDisplayHandler(state)
143151
addLoadListener(state, navigator)

0 commit comments

Comments
 (0)