@ -92,15 +89,14 @@ fun main() = singleWindowApplication {
.wrapContentSize(Alignment.Center)
.wrapContentSize(Alignment.Center)
.fillMaxSize()
.fillMaxSize()
.background(color = color)
.background(color = color)
.pointerMoveFilter(
.onPointerEvent(PointerEventType.Move) {
onMove = {
val position = it.changes.first().position
color = Color(it.x.toInt() % 256, it.y.toInt() % 256, 0)
color = Color(position.x.toInt() % 256, position.y.toInt() % 256, 0)
false
}
}
)
)
)
}
}
```
```
*Note that onPointerEvent is experimental and can be changed in the future. For more stable API look at [Modifier.pointerInput](#listenining-raw-events-in-commonmain-via-modifierpointerinput)*.
@ -138,16 +135,8 @@ fun main() = singleWindowApplication {
modifier = Modifier
modifier = Modifier
.fillMaxWidth()
.fillMaxWidth()
.background(color = if (active) Color.Green else Color.White)
.background(color = if (active) Color.Green else Color.White)
.pointerMoveFilter(
.onPointerEvent(PointerEventType.Enter) { active = true }
onEnter = {
.onPointerEvent(PointerEventType.Exit) { active = false },
active = true
false
},
onExit = {
active = false
false
}
),
fontSize = 30.sp,
fontSize = 30.sp,
fontStyle = if (active) FontStyle.Italic else FontStyle.Normal,
fontStyle = if (active) FontStyle.Italic else FontStyle.Normal,
text = "Item $index"
text = "Item $index"
@ -156,8 +145,44 @@ fun main() = singleWindowApplication {
}
}
}
}
```
```
*Note that onPointerEvent is experimental and can be changed in the future. For more stable API look at [Modifier.pointerInput](#listenining-raw-events-in-commonmain-via-modifierpointerinput)*.
Text("Scroll to change the number: $number", fontSize = 30.sp)
}
}
```
*Note that onPointerEvent is experimental and can be changed in the future. For more stable API look at [Modifier.pointerInput](#listenining-raw-events-in-commonmain-via-modifierpointerinput)*.
### Mouse right/middle clicks and keyboard modifiers
### Mouse right/middle clicks and keyboard modifiers
Compose for Desktop contains desktop-only `Modifier.mouseClickable`, where data about pressed mouse buttons and keyboard modifiers is available. This is an experimental API, which means that it's likely to be changed before release.
Compose for Desktop contains desktop-only `Modifier.mouseClickable`, where data about pressed mouse buttons and keyboard modifiers is available. This is an experimental API, which means that it's likely to be changed before release.
@ -208,11 +233,54 @@ fun main() = singleWindowApplication {
*Note that onPointerEvent is experimental and can be changed in the future. For more stable API look at [Modifier.pointerInput](#listenining-raw-events-in-commonmain-via-modifierpointerinput)*.
### Swing interoperability
### Swing interoperability
Compose for Desktop uses Swing underneath and allows to access raw AWT events:
Compose for Desktop uses Swing underneath and allows to access raw AWT events:
val event = awaitPointerEventScope { awaitPointerEvent() }
.onPointerEvent(PointerEventType.Press) {
val awtEvent = event.mouseEvent
text = it.awtEvent.locationOnScreen?.toString().orEmpty()
if (event.type == PointerEventType.Press) {
},
text = awtEvent?.locationOnScreen?.toString().orEmpty()
}
}
},
contentAlignment = Alignment.Center
contentAlignment = Alignment.Center
) {
) {
Text(text)
Text(text)
}
}
}
}
```
```
*Note that onPointerEvent is experimental and can be changed in the future. For more stable API look at [Modifier.pointerInput](#listenining-raw-events-in-commonmain-via-modifierpointerinput)*.
### Listenining raw events in commonMain via Modifier.pointerInput
In the snippets above we use `Modifier.onPointerEvent`, which is a helper function to subscribe to some type of pointer events. It is a shorter variant of `Modifier.pointerInput`. For now it is experimental, and desktop-only (you can't use it in commonMain code). If you need to subscribe to events in commonMain or you need stable API, you can use `Modifier.pointerInput`: