-
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.
Merge pull request #31 from mike-n-jordan/web-target
Web target
- Loading branch information
Showing
17 changed files
with
284 additions
and
18 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
20 changes: 20 additions & 0 deletions
20
shared/src/androidMain/kotlin/com/bumble/puzzyx/imageloader/ResourceImage.kt
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,20 @@ | ||
package com.bumble.puzzyx.imageloader | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.ContentScale | ||
|
||
@Composable | ||
actual fun EmbeddableResourceImage( | ||
path: String, | ||
modifier: Modifier, | ||
contentDescription: String?, | ||
contentScale: ContentScale | ||
) { | ||
ResourceImage( | ||
path = path, | ||
modifier = modifier, | ||
contentScale = contentScale, | ||
contentDescription = contentDescription, | ||
) | ||
} |
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
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
20 changes: 20 additions & 0 deletions
20
shared/src/desktopMain/kotlin/com/bumble/puzzyx/imageloader/EmbeddableResourceImage.kt
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,20 @@ | ||
package com.bumble.puzzyx.imageloader | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.ContentScale | ||
|
||
@Composable | ||
actual fun EmbeddableResourceImage( | ||
path: String, | ||
modifier: Modifier, | ||
contentDescription: String?, | ||
contentScale: ContentScale | ||
) { | ||
ResourceImage( | ||
path = path, | ||
modifier = modifier, | ||
contentScale = contentScale, | ||
contentDescription = contentDescription, | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
shared/src/jsMain/kotlin/com/bumble/puzzyx/imageloader/EmbeddableResourceImage.kt
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,23 @@ | ||
package com.bumble.puzzyx.imageloader | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.ContentScale | ||
|
||
private const val EMBED_URL = "appyx/where/hosted/sample" | ||
|
||
@Composable | ||
actual fun EmbeddableResourceImage( | ||
path: String, | ||
modifier: Modifier, | ||
contentDescription: String?, | ||
contentScale: ContentScale | ||
) { | ||
ResourceImage( | ||
path = EMBED_URL + path, | ||
fallbackUrl = path, | ||
modifier = modifier, | ||
contentScale = contentScale, | ||
contentDescription = contentDescription, | ||
) | ||
} |
8 changes: 8 additions & 0 deletions
8
shared/src/jsMain/kotlin/com/bumble/puzzyx/imageloader/toImageBitmap.kt
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,8 @@ | ||
package com.bumble.puzzyx.imageloader | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.toComposeImageBitmap | ||
|
||
actual fun ByteArray.toImageBitmap(): ImageBitmap { | ||
return org.jetbrains.skia.Image.makeFromEncoded(this).toComposeImageBitmap() | ||
} |
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,48 @@ | ||
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("jsBrowserProductionExecutableDistributeResources") { | ||
dependsOn("copyResources") | ||
} | ||
|
||
tasks.named("compileKotlinJs") { | ||
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 | ||
} |
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,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Puzzyx</title> | ||
<script src="skiko.js"></script> | ||
<link type="text/css" rel="stylesheet" href="styles.css"/> | ||
</head> | ||
<body> | ||
<div> | ||
<canvas id="ComposeTarget" width="512" height="512"></canvas> | ||
</div> | ||
<script src="webApp.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.