Skip to content

Commit

Permalink
Setup default project
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Jordan committed Oct 13, 2023
1 parent e981fa1 commit 541d8ca
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 4 deletions.
4 changes: 0 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ plugins {
id("org.jetbrains.compose") version libs.versions.composePlugin.get() apply false
id("com.google.devtools.ksp") version libs.versions.ksp.get() apply false
}

tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android.nonTransitiveRClass=true
#MPP
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2
org.jetbrains.compose.experimental.jscanvas.enabled=true

#Compose
org.jetbrains.compose.experimental.uikit.enabled=true
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ rootProject.name = "puzzyx"

include(":androidApp")
include(":desktopApp")
include(":webApp")
include(":shared")
6 changes: 6 additions & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ kotlin {
}
}

// js(IR) {
// // Adding moduleName as a workaround for this issue: https://youtrack.jetbrains.com/issue/KT-51942
// moduleName = "puzzyx-common"
// browser()
// }

sourceSets {
val commonMain by getting {
dependencies {
Expand Down
45 changes: 45 additions & 0 deletions webApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import org.jetbrains.compose.desktop.application.dsl.TargetFormat

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

kotlin {
js(IR) {
moduleName = "puzzyx-web"
browser()
binaries.executable()
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
// implementation(project(":shared"))
implementation(libs.appyx.navigation)
implementation(libs.appyx.components.backstack)
}
}
}
}

compose.experimental {
web.application {}
}

tasks.register<Copy>("copyResources") {
// Dirs containing files we want to copy
from("../shared/src/commonMain/resources")

// Output for web resources
into("$buildDir/processedResources/js/main")

include("**/*")
}

tasks.named("jsMainClasses") {
dependsOn("copyResources")
}

93 changes: 93 additions & 0 deletions webApp/src/jsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Surface
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.unit.dp
import com.bumble.appyx.navigation.integration.ScreenSize
import com.bumble.appyx.navigation.integration.WebNodeHost
import com.bumble.puzzyx.node.app.PuzzyxAppNode
import com.bumble.puzzyx.ui.PuzzyxTheme
import com.bumble.puzzyx.ui.appyx_dark
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import org.jetbrains.skiko.wasm.onWasmReady

fun main() {
val events: Channel<Unit> = Channel()
onWasmReady {
BrowserViewportWindow() {
PuzzyxTheme {
val requester = remember { FocusRequester() }
var hasFocus by remember { mutableStateOf(false) }

var screenSize by remember { mutableStateOf(ScreenSize(0.dp, 0.dp)) }
val eventScope = remember { CoroutineScope(SupervisorJob() + Dispatchers.Main) }

Surface(
modifier = Modifier
.fillMaxSize()
.onSizeChanged { screenSize = ScreenSize(it.width.dp, it.height.dp) }
.onKeyEvent { event ->
onKeyEvent(event, events, eventScope)
}
.focusRequester(requester)
.focusable()
.onFocusChanged { hasFocus = it.hasFocus },
color = appyx_dark,
) {
WebNodeHost(
screenSize = screenSize,
onBackPressedEvents = events.receiveAsFlow(),
) { buildContext ->
PuzzyxAppNode(
buildContext = buildContext,
)
}
}


if (!hasFocus) {
LaunchedEffect(Unit) {
requester.requestFocus()
}
}
}
}
}
}


@OptIn(ExperimentalComposeUiApi::class)
private fun onKeyEvent(
keyEvent: KeyEvent,
events: Channel<Unit>,
coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main),
): Boolean =
when {
keyEvent.type == KeyEventType.KeyUp && keyEvent.key == Key.Backspace -> {
coroutineScope.launch { events.send(Unit) }
true
}

else -> false
}

0 comments on commit 541d8ca

Please sign in to comment.