-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Jordan
committed
Oct 13, 2023
1 parent
e981fa1
commit 541d8ca
Showing
6 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ rootProject.name = "puzzyx" | |
|
||
include(":androidApp") | ||
include(":desktopApp") | ||
include(":webApp") | ||
include(":shared") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |