Yokan / rebuild
Course 1 8624561

PlatformWebView.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/webview/PlatformWebView.kt

implementation70 lines
1package eu.friedcutlet.yokan.ui.screens.webview23import androidx.compose.runtime.Composable4import androidx.compose.runtime.key5import androidx.compose.foundation.layout.Box6import androidx.compose.foundation.layout.fillMaxSize7import androidx.compose.material3.Text8import androidx.compose.ui.Alignment9import androidx.compose.ui.Modifier10import eu.friedcutlet.yokan.services.http.cookies.CookieJar11import org.jetbrains.compose.resources.stringResource12import yokan.composeapp.generated.resources.Res13import yokan.composeapp.generated.resources.browser_profile_unavailable1415/**16 * Render a fully-fledged WebView in the composition. Used by the in-app17 * WebView screen (Cloudflare cookie refresh, manual content inspection, etc.).18 * [cookieJar] seeds the page's cookies and receives the ones it sets.19 *20 * Android/iOS use compose-webview-multiplatform. Desktop embeds JavaFX WebKit21 * for safe browsing and delegates modern challenge flows to WebViewService's22 * external-browser resolver.23 */24@Composable25fun PlatformWebView(26    url: String,27    cookieJar: CookieJar,28    modifier: Modifier = Modifier,29    userAgent: String? = null,30    isObscuredByOverlay: Boolean = false,31    onNavigated: (String) -> Unit = {},32    onLoadingProgress: (Float) -> Unit = {},33    /** Navigations (including redirects) to URLs with this prefix are cancelled and reported via [onNavigated] instead of loaded. */34    interceptUrlPrefix: String? = null,35    /** A page the embedded engine cannot verify; offers the interactive browser resolver. */36    onVerificationRequired: (String) -> Unit = {},37) {38    if (!cookieJar.canUseBrowserStorage) {39        Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {40            Text(stringResource(Res.string.browser_profile_unavailable))41        }42        return43    }44    // A new session must never reuse a native page whose cookie callbacks capture an old jar.45    key(cookieJar) {46        RenderPlatformWebView(47            url, cookieJar, modifier, userAgent, isObscuredByOverlay,48            onNavigated = { if (cookieJar.canUseBrowserStorage) onNavigated(it) },49            onLoadingProgress = { if (cookieJar.canUseBrowserStorage) onLoadingProgress(it) },50            interceptUrlPrefix = interceptUrlPrefix,51            onVerificationRequired = { if (cookieJar.canUseBrowserStorage) onVerificationRequired(it) },52        )53    }54}5556@Composable57internal expect fun RenderPlatformWebView(58    url: String,59    cookieJar: CookieJar,60    modifier: Modifier,61    userAgent: String?,62    isObscuredByOverlay: Boolean,63    onNavigated: (String) -> Unit,64    onLoadingProgress: (Float) -> Unit,65    interceptUrlPrefix: String?,66    onVerificationRequired: (String) -> Unit,67)6869/** Whether [PlatformWebView] reports the pages it loads through `onNavigated` (the web build only opens a tab). */70internal expect val platformWebViewReportsNavigation: Boolean71

Search chapters, reference and source filenames.