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

cfw: make CanvasBasedWindow apply default styles, set title #722

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions compose/mpp/demo/src/jsMain/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@
<title>compose multiplatform web demo</title>
<script src="skiko.js"> </script>
<link type="text/css" rel="stylesheet" href="styles.css">
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>

<body>
<h1>compose multiplatform web demo</h1>
<div>
<canvas id="canvas1" width="800" height="600"></canvas>
<canvas id="canvas1"></canvas>
</div>
<script src="demo.js"> </script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.createSkiaLayer
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.BrowserCursor
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.native.ComposeLayer
Expand All @@ -37,6 +36,8 @@ import org.w3c.dom.HTMLCanvasElement
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.delay
import org.w3c.dom.HTMLStyleElement
import org.w3c.dom.HTMLTitleElement

internal actual class ComposeWindow(val canvasId: String) {

Expand Down Expand Up @@ -124,13 +125,33 @@ private val defaultCanvasElementId = "ComposeTarget"
*
* It can be resized by providing [requestResize].
* By default, it will listen to the window resize events.
*
* By default, styles will be applied to use the entire inner window, disabling scrollbars.
* This can be turned off by setting [applyDefaultStyles] to false.
*/
fun CanvasBasedWindow(
title: String = "JetpackNativeWindow",
title: String? = null,
canvasElementId: String = defaultCanvasElementId,
requestResize: (suspend () -> IntSize)? = null,
applyDefaultStyles: Boolean = true,
content: @Composable () -> Unit = { }
) {
if (title != null) {
val htmlTitleElement = (
document.head!!.getElementsByTagName("title").item(0)
?: document.createElement("title").also { document.head!!.appendChild(it) }
) as HTMLTitleElement
htmlTitleElement.textContent = title
}

if (applyDefaultStyles) {
document.head!!.appendChild(
(document.createElement("style") as HTMLStyleElement).apply {
type = "text/css"
appendChild(document.createTextNode("body { margin: 0; overflow: hidden; }"))
}
)
}

val actualRequestResize: suspend () -> IntSize = if (requestResize != null) {
requestResize
Expand Down Expand Up @@ -176,4 +197,4 @@ fun CanvasBasedWindow(
}

private fun setCursor(elementId: String, value: String): Unit =
js("document.getElementById(elementId).style.cursor = value")
js("document.getElementById(elementId).style.cursor = value")