Skip to content

Instantly share code, notes, and snippets.

@andreasnilsson
Created September 27, 2022 21:26
Show Gist options
  • Save andreasnilsson/e88cffbd87c0ab558101f5d7db03cc89 to your computer and use it in GitHub Desktop.
Save andreasnilsson/e88cffbd87c0ab558101f5d7db03cc89 to your computer and use it in GitHub Desktop.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the
// Apache 2.0 license that can be found in the LICENSE file.
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
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.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.application
@Composable
@Preview
fun App(modifier: Modifier, mouseX: Float) {
println("App: mouseX=$mouseX")
var showNewWindow by remember { mutableStateOf(false) }
val text by remember(mouseX) { mutableStateOf("Hello, World! x: $mouseX") }
MaterialTheme {
Box(modifier.fillMaxSize()) {
Button(onClick = {
showNewWindow = true
}) { Text(text) }
}
if (showNewWindow) {
Window(
onCloseRequest = {showNewWindow = false},
state = WindowState(width = 1000.dp, height = 1000.dp)
) {
Text("mouseX=$mouseX")
}
}
}
}
@OptIn(ExperimentalComposeUiApi::class)
fun main() = application {
var mouseX by remember { mutableStateOf(0f) }
Window(
onCloseRequest = ::exitApplication,
state = WindowState(width = 1000.dp, height = 1000.dp)
) {
App(
Modifier.onPointerEvent(PointerEventType.Move) {
mouseX = currentEvent.changes.first().position.x
},
mouseX
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment