File tree Expand file tree Collapse file tree 4 files changed +43
-44
lines changed
commonMain/kotlin/com/multiplatform/webview/web
desktopMain/kotlin/com/multiplatform/webview/web Expand file tree Collapse file tree 4 files changed +43
-44
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 =
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ package com.multiplatform.webview.web
22
33import androidx.compose.runtime.Composable
44import androidx.compose.runtime.DisposableEffect
5- import androidx.compose.runtime.LaunchedEffect
65import androidx.compose.runtime.getValue
76import androidx.compose.runtime.produceState
87import 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()
You can’t perform that action at this time.
0 commit comments