Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to separate web app module #67

Open
wants to merge 2 commits into
base: explicit-download-plugin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compose-imageviewer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Check out the repo, navigate to the project folder, and use the following comman

### Run Web version via Gradle

`./gradlew :shared:wasmRun`
`./gradlew :webApp:wasmRun`

### Run Desktop version via Gradle

Expand Down
1 change: 1 addition & 0 deletions compose-imageviewer/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ rootProject.name = "imageviewer"
include(":androidApp")
include(":shared")
include(":desktopApp")
include(":webApp")
24 changes: 3 additions & 21 deletions compose-imageviewer/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,11 @@ kotlin {
// iosSimulatorArm64()

js(IR) {
moduleName = "imageviewer"
browser()
binaries.executable()
}

wasm {
moduleName = "imageviewer"
browser {
commonWebpackConfig {
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).copy(
open = mapOf(
"app" to mapOf(
"name" to "google chrome",
)
),
)
}
}
binaries.executable()
browser()
}

// cocoapods {
Expand Down Expand Up @@ -111,10 +97,6 @@ kotlin {
}
}

compose.experimental {
web.application {}
}

android {
compileSdk = 33
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
Expand All @@ -131,5 +113,5 @@ android {
}
}

// Use a proper version of webpack, TODO remove after updating to Kotlin 1.9.
rootProject.the<NodeJsRootExtension>().versions.webpack.version = "5.76.2"
// Use a proper version of webpack, TODO remove after updating to Kotlin 1.9.
rootProject.the<NodeJsRootExtension>().versions.webpack.version = "5.76.2"
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum class ExternalImageViewerEvent {

@OptIn(ExperimentalAnimationApi::class)
@Composable
internal fun ImageViewerCommon(
fun ImageViewerCommon(
dependencies: Dependencies,
externalEvents: Flow<ExternalImageViewerEvent> = emptyFlow()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object ImageviewerColors {
}

@Composable
internal fun ImageViewerTheme(content: @Composable () -> Unit) {
fun ImageViewerTheme(content: @Composable () -> Unit) {
isSystemInDarkTheme() // todo check and change colors
MaterialTheme(
colorScheme = MaterialTheme.colorScheme.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sealed interface ToastState {
}

@Composable
internal fun Toast(
fun Toast(
state: MutableState<ToastState>
) {
val value = state.value
Expand Down
188 changes: 0 additions & 188 deletions compose-imageviewer/shared/src/wasmMain/kotlin/Main.wasm.kt

This file was deleted.

69 changes: 69 additions & 0 deletions compose-imageviewer/webApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}

val copyJsResources = tasks.create("copyJsResourcesWorkaround", Copy::class.java) {
from(project(":shared").file("src/commonMain/resources"))
into("build/processedResources/js/main")
}

val copyWasmResources = tasks.create("copyWasmResourcesWorkaround", Copy::class.java) {
from(project(":shared").file("src/commonMain/resources"))
into("build/processedResources/wasm/main")
}

afterEvaluate {
project.tasks.getByName("jsProcessResources").finalizedBy(copyJsResources)
project.tasks.getByName("wasmProcessResources").finalizedBy(copyWasmResources)
}

kotlin {
js(IR) {
moduleName = "imageviewer"
browser()
binaries.executable()
}

wasm {
moduleName = "imageviewer"
browser {
commonWebpackConfig {
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).copy(
open = mapOf(
"app" to mapOf(
"name" to "google chrome",
)
),
)
}
}
binaries.executable()
}

sourceSets {
val jsWasmMain by creating {
dependencies {
implementation(project(":shared"))
implementation(compose.runtime)
implementation(compose.ui)
implementation(compose.foundation)
implementation(compose.material)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
}
}
val jsMain by getting {
dependsOn(jsWasmMain)
}
val wasmMain by getting {
dependsOn(jsWasmMain)
}
}
}

compose.experimental {
web.application {}
}
14 changes: 14 additions & 0 deletions compose-imageviewer/webApp/src/jsMain/kotlin/Actuals.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import example.imageviewer.model.WrappedHttpClient
import io.ktor.client.*
import io.ktor.client.engine.js.*
import io.ktor.client.request.*
import io.ktor.client.statement.*

actual fun createWrappedHttpClient(): WrappedHttpClient {
return object : WrappedHttpClient {
private val ktorClient = HttpClient(JsClient())
override suspend fun getAsBytes(urlString: String): ByteArray {
return ktorClient.get(urlString).readBytes()
}
}
}
19 changes: 19 additions & 0 deletions compose-imageviewer/webApp/src/jsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import example.imageviewer.*
import example.imageviewer.model.*
import io.ktor.client.*
import io.ktor.client.engine.js.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import org.jetbrains.skiko.wasm.onWasmReady

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
onWasmReady {
CanvasBasedWindow("ImageViewer") {
ImageViewerWeb()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
<script src="skiko.js"> </script>
</head>
<body>
<center>
<div style="background-color: darkslategrey;">
<canvas id="ComposeTarget" width="800" height="1000"></canvas>
</div>
</center>
<canvas id="ComposeTarget"></canvas>
<script src="imageviewer.js"> </script>
</body>
</html>
</html>
Loading