Skip to content

Commit 27640b9

Browse files
authored
Merge pull request #84 from KevinnZou/bugfix/desktop_flickering_effect
Bugfix/desktop flickering effect
2 parents 75c80e7 + f21d202 commit 27640b9

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,40 @@ fun WebView(
5353
}
5454
}
5555

56-
LaunchedEffect(wv, state) {
57-
snapshotFlow { state.content }.collect { content ->
58-
when (content) {
59-
is WebContent.Url -> {
60-
state.lastLoadedUrl = content.url
61-
wv.loadUrl(content.url, content.additionalHttpHeaders)
62-
}
56+
// Desktop will handle the first load by itself
57+
if (!getPlatform().isDesktop()) {
58+
LaunchedEffect(wv, state) {
59+
snapshotFlow { state.content }.collect { content ->
60+
when (content) {
61+
is WebContent.Url -> {
62+
state.lastLoadedUrl = content.url
63+
wv.loadUrl(content.url, content.additionalHttpHeaders)
64+
}
6365

64-
is WebContent.Data -> {
65-
wv.loadHtml(
66-
content.data,
67-
content.baseUrl,
68-
content.mimeType,
69-
content.encoding,
70-
content.historyUrl,
71-
)
72-
}
66+
is WebContent.Data -> {
67+
wv.loadHtml(
68+
content.data,
69+
content.baseUrl,
70+
content.mimeType,
71+
content.encoding,
72+
content.historyUrl,
73+
)
74+
}
7375

74-
is WebContent.File -> {
75-
wv.loadHtmlFile(content.fileName)
76-
}
76+
is WebContent.File -> {
77+
wv.loadHtmlFile(content.fileName)
78+
}
7779

78-
is WebContent.Post -> {
79-
wv.postUrl(
80-
content.url,
81-
content.postData,
82-
)
83-
}
80+
is WebContent.Post -> {
81+
wv.postUrl(
82+
content.url,
83+
content.postData,
84+
)
85+
}
8486

85-
is WebContent.NavigatorOnly -> {
86-
// NO-OP
87+
is WebContent.NavigatorOnly -> {
88+
// NO-OP
89+
}
8790
}
8891
}
8992
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class DesktopWebView(
124124

125125
override fun initJsBridge(webViewJsBridge: WebViewJsBridge) {
126126
KLogger.d {
127-
"DesktopWebView injectJsBridge"
127+
"DesktopWebView initJsBridge"
128128
}
129129
val router = CefMessageRouter.create()
130130
val handler =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ internal fun CefBrowser.addLoadListener(
8686
) {
8787
this.client.addLoadHandler(
8888
object : CefLoadHandler {
89-
private var lastLoadedUrl = ""
89+
private var lastLoadedUrl = "null"
9090

9191
override fun onLoadingStateChange(
9292
browser: CefBrowser?,
@@ -116,7 +116,7 @@ internal fun CefBrowser.addLoadListener(
116116
transitionType: CefRequest.TransitionType?,
117117
) {
118118
KLogger.d { "Load Start ${browser?.url}" }
119-
lastLoadedUrl = "" // clean last loaded url for reload to work
119+
lastLoadedUrl = "null" // clean last loaded url for reload to work
120120
state.loadingState = LoadingState.Loading(0F)
121121
state.errorsForCurrentRequest.clear()
122122
}

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

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

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.DisposableEffect
5-
import androidx.compose.runtime.LaunchedEffect
65
import androidx.compose.runtime.getValue
76
import androidx.compose.runtime.produceState
87
import androidx.compose.runtime.remember
@@ -126,15 +125,21 @@ fun DesktopWebView(
126125
)
127126
}
128127
}
129-
}?.also {
130-
val desktopWebView = DesktopWebView(it, scope, webViewJsBridge)
131-
state.webView = desktopWebView
132-
webViewJsBridge?.webView = desktopWebView
128+
}
129+
val desktopWebView =
130+
remember(browser) {
131+
if (browser != null) {
132+
DesktopWebView(browser, scope, webViewJsBridge)
133+
} else {
134+
null
135+
}
133136
}
134137

135138
browser?.let {
136139
SwingPanel(
137140
factory = {
141+
state.webView = desktopWebView
142+
webViewJsBridge?.webView = desktopWebView
138143
browser.apply {
139144
addDisplayHandler(state)
140145
addLoadListener(state, navigator)
@@ -146,15 +151,6 @@ fun DesktopWebView(
146151
)
147152
}
148153

149-
// Handle navigation events. Workaround for navigator not working issue.
150-
LaunchedEffect(state.webView, navigator) {
151-
state.webView?.let { wv ->
152-
with(navigator) {
153-
wv.handleNavigationEvents()
154-
}
155-
}
156-
}
157-
158154
DisposableEffect(Unit) {
159155
onDispose {
160156
client?.dispose()

0 commit comments

Comments
 (0)