@@ -10,15 +10,15 @@ import com.multiplatform.webview.util.KLogger
1010import com.multiplatform.webview.util.getPlatform
1111import kotlinx.coroutines.flow.filter
1212import kotlinx.coroutines.flow.merge
13- import org.jetbrains.compose.resources.ExperimentalResourceApi
1413
1514/* *
1615 * Created By Kevin Zou On 2023/8/31
1716 */
1817
1918/* *
20- *
21- * A wrapper around the Android View WebView to provide a basic WebView composable.
19+ * Provides a basic WebView composable.
20+ * This version of the function is provided for backwards compatibility by using the older
21+ * onCreated and onDispose callbacks and is missing the factory parameter.
2222 *
2323 * @param state The webview state holder where the Uri to load is defined.
2424 * @param modifier A compose modifier
@@ -30,7 +30,6 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi
3030 * @param onDispose Called when the WebView is destroyed.
3131 * @sample sample.BasicWebViewSample
3232 */
33- @OptIn(ExperimentalResourceApi ::class )
3433@Composable
3534fun WebView (
3635 state : WebViewState ,
@@ -40,6 +39,42 @@ fun WebView(
4039 webViewJsBridge : WebViewJsBridge ? = null,
4140 onCreated : () -> Unit = {},
4241 onDispose : () -> Unit = {},
42+ ) {
43+ WebView (
44+ state = state,
45+ modifier = modifier,
46+ captureBackPresses = captureBackPresses,
47+ navigator = navigator,
48+ webViewJsBridge = webViewJsBridge,
49+ onCreated = { _ -> onCreated() },
50+ onDispose = { _ -> onDispose() },
51+ )
52+ }
53+
54+ /* *
55+ * Provides a basic WebView composable.
56+ *
57+ * @param state The webview state holder where the Uri to load is defined.
58+ * @param modifier A compose modifier
59+ * @param captureBackPresses Set to true to have this Composable capture back presses and navigate
60+ * the WebView back.
61+ * @param navigator An optional navigator object that can be used to control the WebView's
62+ * navigation from outside the composable.
63+ * @param onCreated Called when the WebView is first created.
64+ * @param onDispose Called when the WebView is destroyed.
65+ * @param factory A function that creates a platform-specific WebView object.
66+ * @sample sample.BasicWebViewSample
67+ */
68+ @Composable
69+ fun WebView (
70+ state : WebViewState ,
71+ modifier : Modifier = Modifier ,
72+ captureBackPresses : Boolean = true,
73+ navigator : WebViewNavigator = rememberWebViewNavigator(),
74+ webViewJsBridge : WebViewJsBridge ? = null,
75+ onCreated : (NativeWebView ) -> Unit = {},
76+ onDispose : (NativeWebView ) -> Unit = {},
77+ factory : ((WebViewFactoryParam ) -> NativeWebView )? = null,
4378) {
4479 val webView = state.webView
4580
@@ -119,6 +154,7 @@ fun WebView(
119154 webViewJsBridge = webViewJsBridge,
120155 onCreated = onCreated,
121156 onDispose = onDispose,
157+ factory = factory ? : ::defaultWebViewFactory,
122158 )
123159
124160 DisposableEffect (Unit ) {
@@ -131,6 +167,24 @@ fun WebView(
131167 }
132168}
133169
170+ /* *
171+ * Platform specific parameters given to the WebView factory function. This is a
172+ * data class containing one or more platform-specific values necessary to
173+ * create a platform-specific WebView:
174+ * - On Android, this contains a `Context` object
175+ * - On iOS, this contains a `WKWebViewConfiguration` object created from the
176+ * provided WebSettings
177+ * - On Desktop, this contains the WebViewState, the KCEFClient, and the
178+ * loaded file content (if a file, otherwise, an empty string)
179+ */
180+ expect class WebViewFactoryParam
181+
182+ /* *
183+ * Platform specific default WebView factory function. This can be called from
184+ * a custom factory function for any platforms that don't need to be customized.
185+ */
186+ expect fun defaultWebViewFactory (param : WebViewFactoryParam ): NativeWebView
187+
134188/* *
135189 * Expect API of [WebView] that is implemented in the platform-specific modules.
136190 */
@@ -141,6 +195,7 @@ expect fun ActualWebView(
141195 captureBackPresses : Boolean = true,
142196 navigator : WebViewNavigator = rememberWebViewNavigator(),
143197 webViewJsBridge : WebViewJsBridge ? = null,
144- onCreated : () -> Unit = {},
145- onDispose : () -> Unit = {},
198+ onCreated : (NativeWebView ) -> Unit = {},
199+ onDispose : (NativeWebView ) -> Unit = {},
200+ factory : (WebViewFactoryParam ) -> NativeWebView = ::defaultWebViewFactory,
146201)
0 commit comments