From 4cd4cd3c0d326fb1435284956408c0f77edc13cb Mon Sep 17 00:00:00 2001 From: kevinzou Date: Sat, 27 Apr 2024 11:13:21 +0800 Subject: [PATCH 1/9] feat:Upgrade deps version to support WASM --- gradle.properties | 13 +++++++++++++ sample/shared/build.gradle.kts | 19 ++++++++++++++++++- webview/build.gradle.kts | 16 +++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index cd047c61..85ea4984 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,34 +1,47 @@ #Gradle org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M" + #Kotlin kotlin.code.style=official + #MPP kotlin.mpp.stability.nowarn=true kotlin.mpp.enableCInteropCommonization=true kotlin.mpp.androidSourceSetLayoutVersion=2 + +#Compose +org.jetbrains.compose.experimental.uikit.enabled=true +org.jetbrains.compose.experimental.wasm.enabled=true + #Android android.useAndroidX=true android.compileSdk=34 android.targetSdk=34 android.minSdk=21 + #Versions kotlin.version=1.9.23 agp.version=8.1.1 compose.version=1.6.1 coroutines.version=1.8.0 + GROUP=io.github.kevinnzou POM_ARTIFACT_ID=compose-webview-multiplatform VERSION_NAME=1.9.7 + POM_NAME=Compose WebView Multiplatform POM_INCEPTION_YEAR=2023 POM_DESCRIPTION=WebView for JetBrains Compose Multiplatform POM_URL=https://github.com/KevinnZou/compose-webview-multiplatform + POM_SCM_URL=https://github.com/KevinnZou/compose-webview-multiplatform POM_SCM_CONNECTION=scm:git:git://github.com/KevinnZou/compose-webview-multiplatform.git POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/KevinnZou/compose-webview-multiplatform.git + POM_LICENCE_NAME=The Apache Software License, Version 2.0 POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENCE_DIST=repo + POM_DEVELOPER_ID=kevinzou POM_DEVELOPER_NAME=Kevin Zou POM_DEVELOPER_URL=https://github.com/KevinnZou/ diff --git a/sample/shared/build.gradle.kts b/sample/shared/build.gradle.kts index a1b190d9..a9cf3d84 100644 --- a/sample/shared/build.gradle.kts +++ b/sample/shared/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl + plugins { kotlin("multiplatform") kotlin("plugin.serialization") @@ -10,6 +12,17 @@ plugins { kotlin { targetHierarchy.default() + @OptIn(ExperimentalWasmDsl::class) + wasmJs { + moduleName = "composeWebViewSample" + browser { + commonWebpackConfig { + outputFileName = "composeWebViewSample.js" + } + } + binaries.executable() + } + androidTarget { compilations.all { kotlinOptions { @@ -45,7 +58,7 @@ kotlin { implementation("cafe.adriel.voyager:voyager-navigator:$voyagerVersion") implementation("cafe.adriel.voyager:voyager-tab-navigator:$voyagerVersion") api(project(":webview")) - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") implementation("org.jetbrains.kotlinx:atomicfu:0.23.2") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") implementation("org.jetbrains.androidx.navigation:navigation-compose:2.7.0-alpha03") @@ -91,3 +104,7 @@ android { jvmToolchain(17) } } + +compose.experimental { + web.application {} +} diff --git a/webview/build.gradle.kts b/webview/build.gradle.kts index 5731a505..fff5e41f 100644 --- a/webview/build.gradle.kts +++ b/webview/build.gradle.kts @@ -1,5 +1,8 @@ @file:Suppress("UNUSED_VARIABLE", "OPT_IN_USAGE") +import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl + + plugins { kotlin("multiplatform") id("com.android.library") @@ -14,6 +17,17 @@ kotlin { targetHierarchy.default() + @OptIn(ExperimentalWasmDsl::class) + wasmJs { + moduleName = "composeWebView" + browser { + commonWebpackConfig { + outputFileName = "composeWebView.js" + } + } + binaries.executable() + } + androidTarget { publishLibraryVariants("release") } @@ -42,7 +56,7 @@ kotlin { implementation(compose.components.resources) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") implementation("co.touchlab:kermit:2.0.3") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") } } val androidMain by getting { From 6a15b80afee35b417d29f2dd9fe5d352e98268f2 Mon Sep 17 00:00:00 2001 From: kevinzou Date: Sat, 27 Apr 2024 11:14:06 +0800 Subject: [PATCH 2/9] feat:main file for WASM --- .../wasmJsMain/kotlin/com/kevinnzou/sample/main.kt | 13 +++++++++++++ sample/shared/src/wasmJsMain/resources/index.html | 12 ++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt create mode 100644 sample/shared/src/wasmJsMain/resources/index.html diff --git a/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt b/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt new file mode 100644 index 00000000..99481b7f --- /dev/null +++ b/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt @@ -0,0 +1,13 @@ +package com.kevinnzou.sample + +import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.ui.window.CanvasBasedWindow + +@OptIn(ExperimentalComposeUiApi::class) +fun main() { + CanvasBasedWindow(canvasElementId = "ComposeTarget") { App() } +} + +actual fun getPlatformName(): String { + return "JS/WASM" +} diff --git a/sample/shared/src/wasmJsMain/resources/index.html b/sample/shared/src/wasmJsMain/resources/index.html new file mode 100644 index 00000000..906014e6 --- /dev/null +++ b/sample/shared/src/wasmJsMain/resources/index.html @@ -0,0 +1,12 @@ + + + + + Compose App + + + + + + + \ No newline at end of file From 41c163d62b78d2db2ed2fca6d0b64d40149ef37a Mon Sep 17 00:00:00 2001 From: kevinzou Date: Sat, 27 Apr 2024 11:14:24 +0800 Subject: [PATCH 3/9] feat:empty implementation for WASM --- .../webview/web/WebView.wasmJs.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt diff --git a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt new file mode 100644 index 00000000..635416c7 --- /dev/null +++ b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt @@ -0,0 +1,20 @@ +package com.multiplatform.webview.web + +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import com.multiplatform.webview.jsbridge.WebViewJsBridge + +/** + * Expect API of [WebView] that is implemented in the platform-specific modules. + */ +@Composable +actual fun ActualWebView( + state: WebViewState, + modifier: Modifier, + captureBackPresses: Boolean, + navigator: WebViewNavigator, + webViewJsBridge: WebViewJsBridge?, + onCreated: () -> Unit, + onDispose: () -> Unit, +) { +} From 5723b8a99f72d2d1882792290e91496c7606697d Mon Sep 17 00:00:00 2001 From: kevinzou Date: Mon, 13 May 2024 14:30:32 +0800 Subject: [PATCH 4/9] feat:Empty Implementation for WASM/JS --- sample/shared/build.gradle.kts | 2 +- .../assets/index.html | 0 .../assets/script.js | 0 .../assets/styles.css | 0 .../kotlin/com/kevinnzou/sample/main.kt | 2 +- .../src/wasmJsMain/resources/index.html | 2 +- .../multiplatform/webview/util/Platform.kt | 10 +++++ .../webview/cookie/WasmJsCookieManager.kt | 38 +++++++++++++++++++ .../webview/util/Platform.wasmJs.kt | 21 ++++++++++ .../webview/web/WasmJsWebViewBundle.kt | 3 ++ .../webview/web/WebView.wasmJs.kt | 6 +++ 11 files changed, 81 insertions(+), 3 deletions(-) rename sample/shared/src/commonMain/{composeResources => resources}/assets/index.html (100%) rename sample/shared/src/commonMain/{composeResources => resources}/assets/script.js (100%) rename sample/shared/src/commonMain/{composeResources => resources}/assets/styles.css (100%) create mode 100644 webview/src/wasmJsMain/kotlin/com/multiplatform/webview/cookie/WasmJsCookieManager.kt create mode 100644 webview/src/wasmJsMain/kotlin/com/multiplatform/webview/util/Platform.wasmJs.kt create mode 100644 webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WasmJsWebViewBundle.kt diff --git a/sample/shared/build.gradle.kts b/sample/shared/build.gradle.kts index a9cf3d84..1b57135b 100644 --- a/sample/shared/build.gradle.kts +++ b/sample/shared/build.gradle.kts @@ -45,7 +45,7 @@ kotlin { sourceSets { val coroutinesVersion = extra["coroutines.version"] as String - val voyagerVersion = "1.0.0-rc10" + val voyagerVersion = "1.1.0-alpha03" val commonMain by getting { dependencies { diff --git a/sample/shared/src/commonMain/composeResources/assets/index.html b/sample/shared/src/commonMain/resources/assets/index.html similarity index 100% rename from sample/shared/src/commonMain/composeResources/assets/index.html rename to sample/shared/src/commonMain/resources/assets/index.html diff --git a/sample/shared/src/commonMain/composeResources/assets/script.js b/sample/shared/src/commonMain/resources/assets/script.js similarity index 100% rename from sample/shared/src/commonMain/composeResources/assets/script.js rename to sample/shared/src/commonMain/resources/assets/script.js diff --git a/sample/shared/src/commonMain/composeResources/assets/styles.css b/sample/shared/src/commonMain/resources/assets/styles.css similarity index 100% rename from sample/shared/src/commonMain/composeResources/assets/styles.css rename to sample/shared/src/commonMain/resources/assets/styles.css diff --git a/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt b/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt index 99481b7f..0c80fc9d 100644 --- a/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt +++ b/sample/shared/src/wasmJsMain/kotlin/com/kevinnzou/sample/main.kt @@ -5,7 +5,7 @@ import androidx.compose.ui.window.CanvasBasedWindow @OptIn(ExperimentalComposeUiApi::class) fun main() { - CanvasBasedWindow(canvasElementId = "ComposeTarget") { App() } + CanvasBasedWindow(canvasElementId = "ComposeTarget") { WebViewApp() } } actual fun getPlatformName(): String { diff --git a/sample/shared/src/wasmJsMain/resources/index.html b/sample/shared/src/wasmJsMain/resources/index.html index 906014e6..ee01e676 100644 --- a/sample/shared/src/wasmJsMain/resources/index.html +++ b/sample/shared/src/wasmJsMain/resources/index.html @@ -4,7 +4,7 @@ Compose App - + diff --git a/webview/src/commonMain/kotlin/com/multiplatform/webview/util/Platform.kt b/webview/src/commonMain/kotlin/com/multiplatform/webview/util/Platform.kt index 2bd56ca7..ee83de38 100644 --- a/webview/src/commonMain/kotlin/com/multiplatform/webview/util/Platform.kt +++ b/webview/src/commonMain/kotlin/com/multiplatform/webview/util/Platform.kt @@ -23,6 +23,11 @@ internal sealed class Platform { */ data object IOS : Platform() + /** + * The WasmJs platform. + */ + data object WasmJs : Platform() + /** * Whether the current platform is Android. */ @@ -37,6 +42,11 @@ internal sealed class Platform { * Whether the current platform is iOS. */ fun isIOS() = this is IOS + + /** + * Whether the current platform is WasmJs. + */ + fun isWasmJs() = this is WasmJs } /** diff --git a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/cookie/WasmJsCookieManager.kt b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/cookie/WasmJsCookieManager.kt new file mode 100644 index 00000000..fa519cbd --- /dev/null +++ b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/cookie/WasmJsCookieManager.kt @@ -0,0 +1,38 @@ +package com.multiplatform.webview.cookie + +/** + * WasmJs implementation of [CookieManager] + */ +class WasmJsCookieManager : CookieManager { + override suspend fun setCookie( + url: String, + cookie: Cookie, + ) { + // TODO + } + + override suspend fun getCookies(url: String): List { + return emptyList() + } + + override suspend fun removeAllCookies() { + // TODO + } + + override suspend fun removeCookies(url: String) { + // TODO + } +} + +/** + * Creates a [CookieManager] instance. + */ +@Suppress("FunctionName") +actual fun WebViewCookieManager(): CookieManager { + return WasmJsCookieManager() +} + +actual fun getCookieExpirationDate(expiresDate: Long): String { + // TODO + return "0" +} diff --git a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/util/Platform.wasmJs.kt b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/util/Platform.wasmJs.kt new file mode 100644 index 00000000..98b736b0 --- /dev/null +++ b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/util/Platform.wasmJs.kt @@ -0,0 +1,21 @@ +package com.multiplatform.webview.util + +/** + * Get the current platform. + */ +internal actual fun getPlatform(): Platform { + return Platform.WasmJs +} + +internal actual fun getPlatformVersion(): String { + // TODO + return "0.0.0" +} + +internal actual fun getPlatformVersionDouble(): Double { + val systemVersion = getPlatformVersion() + val components = systemVersion.split(".") + val major = components.getOrNull(0)?.toDoubleOrNull() ?: 0.0 + val minor = components.getOrNull(1)?.toDoubleOrNull() ?: 0.0 + return major + (minor / 10.0) +} diff --git a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WasmJsWebViewBundle.kt b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WasmJsWebViewBundle.kt new file mode 100644 index 00000000..c73b4d47 --- /dev/null +++ b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WasmJsWebViewBundle.kt @@ -0,0 +1,3 @@ +package com.multiplatform.webview.web + +actual class WebViewBundle diff --git a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt index 635416c7..3c4e234b 100644 --- a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt +++ b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt @@ -1,8 +1,11 @@ package com.multiplatform.webview.web +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import com.multiplatform.webview.jsbridge.WebViewJsBridge +import org.w3c.dom.Text /** * Expect API of [WebView] that is implemented in the platform-specific modules. @@ -17,4 +20,7 @@ actual fun ActualWebView( onCreated: () -> Unit, onDispose: () -> Unit, ) { + Box(modifier = Modifier.fillMaxSize()) { + Text("WebView is not supported on this platform yet.") + } } From 5d809d72c8209deefee75ac8cdf6ba79ebbed4a4 Mon Sep 17 00:00:00 2001 From: kevinzou Date: Wed, 15 May 2024 08:11:02 +0800 Subject: [PATCH 5/9] feat:Empty implementation for WASM --- gradle.properties | 2 +- webview/build.gradle.kts | 1 + .../com/multiplatform/webview/web/WebView.wasmJs.kt | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index 85ea4984..7cdefaaf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,7 +22,7 @@ android.minSdk=21 #Versions kotlin.version=1.9.23 agp.version=8.1.1 -compose.version=1.6.1 +compose.version=1.6.10-rc01 coroutines.version=1.8.0 GROUP=io.github.kevinnzou diff --git a/webview/build.gradle.kts b/webview/build.gradle.kts index fff5e41f..ffb003fa 100644 --- a/webview/build.gradle.kts +++ b/webview/build.gradle.kts @@ -52,6 +52,7 @@ kotlin { dependencies { implementation(compose.runtime) implementation(compose.foundation) + implementation(compose.material3) @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) implementation(compose.components.resources) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") diff --git a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt index 3c4e234b..4f4a66d6 100644 --- a/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt +++ b/webview/src/wasmJsMain/kotlin/com/multiplatform/webview/web/WebView.wasmJs.kt @@ -2,10 +2,11 @@ package com.multiplatform.webview.web import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import com.multiplatform.webview.jsbridge.WebViewJsBridge -import org.w3c.dom.Text /** * Expect API of [WebView] that is implemented in the platform-specific modules. @@ -20,7 +21,10 @@ actual fun ActualWebView( onCreated: () -> Unit, onDispose: () -> Unit, ) { - Box(modifier = Modifier.fillMaxSize()) { + Box( + modifier = Modifier.fillMaxSize(), + contentAlignment = Alignment.Center, + ) { Text("WebView is not supported on this platform yet.") } } From 52ee252df8f21db8dbd4de980493018fdca3e157 Mon Sep 17 00:00:00 2001 From: kevinzou Date: Wed, 15 May 2024 08:12:24 +0800 Subject: [PATCH 6/9] feat:Empty implementation for WASM gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 228a7a77..062c0158 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ sample/desktopApp/cache sample/desktopApp/DawnCache sample/desktopApp/GPUCache sample/desktopApp/*.log -site \ No newline at end of file +site +kotlin-js-store/yarn.lock \ No newline at end of file From d050bc6e962f0ef3d3c40288131c8b169d993407 Mon Sep 17 00:00:00 2001 From: kevinzou Date: Thu, 16 May 2024 08:50:13 +0800 Subject: [PATCH 7/9] feat:WASM experimental support --- webview/build.gradle.kts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/webview/build.gradle.kts b/webview/build.gradle.kts index ffb003fa..683eb3f8 100644 --- a/webview/build.gradle.kts +++ b/webview/build.gradle.kts @@ -125,3 +125,7 @@ mavenPublishing { publishToMavenCentral(com.vanniktech.maven.publish.SonatypeHost.S01, automaticRelease = true) signAllPublications() } + +compose.experimental { + web.application {} +} From c971fa13e01b7914752aec93a4006a40a29157d5 Mon Sep 17 00:00:00 2001 From: kevinzou Date: Thu, 16 May 2024 14:43:37 +0800 Subject: [PATCH 8/9] bugfix:upgrade the macos version to 14.2 --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e2d51b20..64e787b6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,8 +11,8 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-20.04, macos-12, windows-2022] - gradle: [8.3] + os: [ ubuntu-20.04, macos-14.2, windows-2022 ] + gradle: [ 8.3 ] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 From b3dee75c8ca649d8daee7997c1fe2653b52fc779 Mon Sep 17 00:00:00 2001 From: kevinzou Date: Thu, 16 May 2024 20:39:46 +0800 Subject: [PATCH 9/9] bugfix:upgrade the macos version to 14 --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 64e787b6..4b43298d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu-20.04, macos-14.2, windows-2022 ] + os: [ ubuntu-20.04, macos-14, windows-2022 ] gradle: [ 8.3 ] runs-on: ${{ matrix.os }} steps: